Where is the error in this simple macro? - vba

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.

Related

Run-time error '3265': Item not found in this collection. VBA module in Access

I am trying to create a macro that will copy a table and its associated queries for each year's equipment audit. I copied some code that I found on another forum, but I receive the error in the title on one line. I'm not familiar enough with VBA to understand what the problem is, so I'm hoping I can get some help.
This code is supposed to replace the source table on a copied query so that the queries don't have to be remade each year. Here is the code:
Sub UpdateQuery(QueryName, CurrentSourceTable, NewSourceTable)
Dim strQryName, strCTbl, strNTbl, strCsql, strNsql As String
Dim defqry As DAO.QueryDef
strQryName = QueryName
strCTbl = CurrentSourceTable
strNTbl = NewSourceTable
Set defqry = CurrentDb.QueryDefs(strQryName)
strCsql = defqry.SQL
strNsql = Replace(strCsql, strCTbl, strNTbl)
defqry.SQL = strNsql
defqry.Close
End Sub
The error occurs on the "Set defqry" line. Can anyone tell me what is causing the error?
ETA:
When I try to run the code above, I'm also using the following code to fill in QueryName/CurrentSourceTable/NewSourceTable:
Sub Proc1()
Call UpdateQuery(YYYY_Count_of_items_by_floor, Building_Audit_2021, Building_Audit_YYYY)
End Sub
Again, I did not write this code, I copied code that someone else had written and am attempting to use it for what I need.
Thanks, HansUp! YYYY_Count_of_items_by_floor is indeed the name of the query, so enclosing it in quotes was all that I needed to do.

Insert formula in cell using VBA issue

i am having issues inserting the below formula in a cell. I doubled the " already. Any idea what i am doing wrong?
Thanks a lot
Sub insert_formulas()
Worksheets("Parsing").Range("H2").Formula = "=IF(E2=""New"";OFFSET(H2,-1;0)-H2;""N/A"")"
End Sub
Replaced it for: (but still error 400)
Sub insert_formulas()
Worksheets("Parsing").Range("H2").Formula = "=IF(E2=""New"",OFFSET(H2,-1,0)-H2,""N/A"")"
End Sub
Its solved. Thanks - the above is correct.
The VBA statement shown in your post (see the one marked with "...the above is correct"):
Worksheets("Parsing").Range("H2").Formula = "=IF(E2=""New"",OFFSET(H2,-1;0)-H2,""N/A"")"
contains error in OFFSET() Function: it should be corrected as following:
Worksheets("Parsing").Range("H2").Formula = "=IF(E2=""New"",OFFSET(H2,-1,0)-H2,""N/A"")"
Best regards,

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 :)

Compile Error Expected Function Or Variable

I'm new and trying to learn VBA. When I'm typing in the code I get Compile Error Expected Function Or Variable.
Is something regarding the activecell, but can't figure it out.
Sub Testare()
Dim FilmName As String
Dim FilmLenght As Integer
Dim FilmDescription As String
Range("b10").Select
FilmName = ActiveCell.Value
FilmLenght = ActiveCell.Offset(0, 2).Value
If FilmLenght < 100 Then
FilmDescription = "Interesant"
Else
FilmDescription = "Suficient"
End If
MsgBox FilmName & " is " & FilmDescription
End Sub
This error happens also when a Sub is called the same as variable (i.e. in one Sub you have for loop with iterator "a", whilst another Sub is called "a").
This gives an error that fits to your description.
Regards
It is possible to make your code fail in two different ways:
Place a very large value in D10
Place a text value in D10
This will result in either an overflow error or type mismatch error.
I know this was asked a while ago, but I ended up with the same error when I created a Sub within a worksheet with the Sub Name the same as the Worksheet name.
Unfortunately when this happens, no error is highlighted by a compile; the error only appears at run time. Also, the offending line is not highlighted, which makes it a bit difficult to find.
Changing the Sub name solves it though.
Here is what caused this error in my case:
Inside a new Public Function that I was starting to write, I began to type a statement, but needed to copy a long varname from another location, so I just inserted the letter a to prevent Excel from popping-up the uber-annoying Compile Error: Expected Expression dialog. So I had this:
myVarName = a
I then attempted to step-through my code to get to that point, and when Excel entered that function I received the message "Compile Error: Expected Function Or Variable". To resolve, I just changed the placeholder to a number:
myVarName = 1
And all continued just fine.

Expected end of Statement Error on Simple Formula Insert

###.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.