VBA for Dummies example, display message box giving syntax error - vba

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

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.

[VBA][excel] Occurred error When Using 'Japanese - Katakana' in 'inStr'

I'm Korean Data analyst. I Used Korean version Windows 7 and OS.
I have a some problem about instr in VBA (Excel).
In normal case,
instr is generate accurate number at "C2" indicating starting point number at "A2" string
Here is normal sample:
Even Korean, and Japanese (hiragana) have no problem too.
Please excuse me I can't more sample
because I have not enough reputation point this in this site.
But, there is always occur error when I use Japanese-Katakana
The error message is below sentence.
'7' runtime error has occurred, Not enough memory
Refer to this sample:
I have embarrassed about this only one error
and trying to solve this problem,
but still don't know how.
I hope Anyone advice me good opinion and solution.
Thanks
I'm not able to replicate this issue, in my English Excel 2013/Win7.
However, I have seen such issue before in old excel version, and most probably, below should work.
Please change function "InStr" to "InStrb", and see whether it works
Sub instr()
Range("C2") = InStrB(1, Range("A2"), Range("B2"), 1)
End Sub
Or you might want to do StrConv(string, vbUnicode) first before you call InStr function.
Sub instr()
Range("C2") = InStr(1, StrConv(Range("A2"), vbUnicode), StrConv(Range("B2"), vbUnicode), 1)
End Sub
Whatever approach you selected, the return position value need be divided by two, if the input string is not Half-width chars. (Half-width chars are already gone in recent excel, and I suppose your excel is too old. Upgrading it is the best choice.)

"Sub or Function not defined" when trying to run a VBA script in Outlook

As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:
Sub Test()
MsgBox ("Hello world")
End Sub
The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).
How can I make the VBA script 'accessible' to Outlook?
Figure 1 Running the "Test" macro in Microsoft Outlook
Figure 2 "Sub or Function not defined" error, with module tree in the background
I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).
I had a similar situation with this issue. In this case it would have looked like this
Sub Test()
MsqBox ("Hello world")
End Sub
The problem was, that I had a lot more code there and couldn't recognize, that there was a misspelling in "MsqBox" (q instead of g) and therefore I had an error, it was really misleading, but since you can get on this error like that, maybe someone else will notice that it was cause by a misspelling like this...
This error “Sub or Function not defined”, will come every time when there is some compile error in script, so please check syntax again of your script.
I guess that is why when you used msqbox instead of msgbox it throws the error.
This probably does not answer your question, but I had the same question and it answered mine.
I changed Private Function to Public Function and it worked.
I need to add that, if the Module name and the sub name is the same you have such issue.
Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.
I think you need to update your libraries so that your VBA code works, your using ms outlook

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.