Expected end of Statement Error on Simple Formula Insert - vba

###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"})"
I want VBA to do this simple formula but getting "Expected: end of Statement" error.
It seems that I need to define something as VBA doesn't recognize character "{}" the brackets.

Assuming that ### actually symbolizes a cell object (otherwise you would get a compile error):
###.Value = "=LOOKUP(LEFT(W2),{""C"",""A"",""B""},{""Pick Up"",""Collect"",""Prepaid""})"
Also, I thought that you would have to change .Value to .Formula, but I tested and both ways work.

It might be requiring you to end the script like this
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"});"
OR
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"})";
NOTICE: the Semi-colon at the end ';'.
I'm not a VBA user for a long time. but just try. Delete this answer if its not good enough.

Related

Where is the error in this simple macro?

I have this code:
Sub ADD_PEDIDO_AtualizaPagamentos()
If M6 = erro Then
MsgBox ("Erro!")
Else
Sheets("ADD_PEDIDO").[M9].Value = "Done!"
End If
End Sub
Without the "IF" the code works fine. But after I insert IF and Else, I always receive the Error code.
And as you can see in the above code, the M6 cell giver the "OK" to insert, but macro gives me error.
Any help?
You have some severe syntax issues in your coding.
You should refer to a range using the worksheet.Range method.
Sub ADD_PEDIDO_AtualizaPagamentos()
If Worksheets("ADD_PEDIDO").Range("M6") = "erro" Then
MsgBox "Erro!"
Else
Worksheets("ADD_PEDIDO").Range("M9").Value = "Done!"
End If
End Sub
I don't speak Spanish so I don't know if erro is a key term in VBA in another language, but if you're checking for the actual string erro, you should change your if statement line to. Also surround M6 with Range("") so that it knows you aren't referring to a variable called M6.
If Range("M6") = "erro" Then
UNTESTED:
try replacing:
If M6 = erro Then
with:
If Range("M6").Text = "erro" Then
I think you should first learn how to refer to a particular cell or range of cells in VBA by reading this up β€” then I think you should be able to answer your own question.
Without the if...then in your line 2, it becomes an initialization (assignment) of an hitherto undeclared variable M6, just like `x = ...’.
It may be a good practice for you to add Option Explicit to every module of vba codes you write; then VBA would be more generous in telling you what went wrong.

How to fix this error 1004? Rewrite the line

I have the following line of code in VBA:
Range("A40:A64").Formula = "=IF(index(optional_processes,row()-39)=0;"";optional_processes,row()-39))"
Without the if-statement, some cells display 0. I want these 0to be blanks. That's what the if-statement should do. I think it gives the application or object-defined error because of the double quotation marks in the statement. How can I this get blank cells instead of 0?
Even if you local Excel uses ; (semicolon) as parameter separator in formulas, when writing a formula with VBA, you have to use , (comma)
Beside this, there are some more issues with the formula:
You are missing a index at the else-part
Use """" rather than "" like Kresimir suggests
Use an ISERROR like Kiran writes in his first suggestion
I think this will work:
Range("A40:A64").Formula = "=IF(isError(index(optional_processes,row()-39)),"""",index(optional_processes,row()-39))"
Try this:
Range("A40:A64").Formula = "=IF(ISERROR(index(optional_processes,row()-39)),'',index(optional_processes,row()-39))"
OR
Range("A40:A64").Formula = "=IF(index(optional_processes,row()-39)=0,"""",index(optional_processes,row()-39))"

IsNa Sub or Function Not Defined

I Know this is silly but I still can't find the answer after an hour of searching.
I'm trying to determine if a value(Customername) is an NA; I've tried the following 2 methods
Customername = Worksheets("Request").Range("E5")
If IsNA(Customername.value) = True Then CustomerN = 1
CustomerN = Ifna(Customername,1)
Both lines return errors stating "Sub of Function not defined". Whats frustrating is it recognizes the functions because it changes the capitalization, but it still breaks. I've tried dimming CustomerName as different types but doesn't seem to matter.
If you could explain the error in my thought process I'd appreciate it.
It is a worksheet function, so you have to explain this to vba by adding Application.worksheetfunction - like this:
Application.WorksheetFunction.IsNA(Customername.value)
Unless your need is to check specifically for the #N/A error, you can use the natively-available ISERROR function. The caveat is that ISERROR will check for any error, not just #N/A.
(If you need to check specifically for #N/A, Pavel_V's answer is spot on.)
From the link:
Syntax
ISERROR(value)
Function Returns TRUE if
ISERROR Value refers to any error value (#N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL!).
You can use it like this:
If IsError(Customer.Value) Then
Additional information at TechOnNet.

VBA for Dummies example, display message box giving syntax error

I am trying to run this code from an example in 'VBA for Dummies'
It is giving me 'Syntax Error' at the message box line, not very encouraging
Sub AddEmUp()
Sum = 1 + 1
MsgBox β€œThe answer is β€œ & Sum
End Sub
Why does this not work?
As pointed by L42, it is the quotation usage; While you might have problems to copy-paste, it could be the source that have wrong formatting as well.
Try to always type your code when learning, it will still more to you :)

Text Format in Excel using VBA

When I use WinSQL to run a SQL statement, the result is 20100803000001812. However, when I incorporate the SQL as a macro, the result is 2.01008E+16. What should I do in the macro in order to maintain the result as 20100803000001812 ?
Thanks,
Bob
According to this article ActiveCell.NumberFormat = "#" should do the trick.
ActiveCell.NumberFormat = "0" works for me (not what I expected, but so it goes)
You might want to throw in a Cells.Columns.AutoFit to resize the columns as necessary.