Reputation: 385
I am trying to execute a macro to create and extract name variable using MsgBox. I have tried to run it without '_' in the MsgBox code, it didn't work. It shows a message that there is a syntax error in the code defining name equal to Steve Jobs.
Sub test1()
Dim name as string, lname as string, fname as string
name = “Steve Jobs”
lname = right(name, 4)
fname = left(name,5)
Msgbox “First name is” & fname & “” & “ the number of characters are” & len(fname)
Msgbox “Surname is” & fname & “” & “ the number of characters are” _ & len(fname)
End Sub
Upvotes: 1
Views: 66
Reputation: 21657
The _
you're referring to is a line continuation character.
Also, the quotation marks in your post are incorrect - although it may be a result of how you copied the text to this site (perhaps you had to "email it to yourself" in the process). Once the quotation marks are changed from “
and ”
to "
. then either of these will run fine:
with line continuation:
Msgbox “Surname is” & fname & “” & “ the number of characters are” & _
len(fname)
without line continuation:
Msgbox “Surname is” & fname & “” & “ the number of characters are” & len(fname)
I think this is what you're trying to do:
Debug.Print "Surname is """ & lName & """ and the number of characters is " & Len(lName)
In your example, you're using fName
for both first name & last name. Also, this is how we can put quotes around a variable.
Upvotes: 0
Reputation: 574
Sub test1()
Dim name As String, lname As String, fname As String
name = "Steve Jobs"
lname = Right(name, 4)
fname = Left(name, 5)
MsgBox "First name is " & fname & "" & " the number of characters is " & _
Len(fname)
MsgBox "Surname is " & lname & "" & " the number of characters is " & _
Len(lname)
End Sub
The quote mark you use looks weird, this way it should be fine
Upvotes: 1