1

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
1
  • 2
    Could it be because your quotes look funny? e.g. " versus ” “ Commented Apr 3, 2018 at 13:39

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

5 Comments

One more thing that I assume you know is that this code isn't going to work properly for almost any other name because of the way you defined lname and fname.
This works. Why is an underscore put after ampersand in the MsgBox line? Thanks.
@KBh it is a space followed by an underscore and a linebreak. This means the code in the next line is handled as it was in the same line. It's kind a soft line break just to improve code visibility and avoid very long code lines. It is the same as writing all in one line.
@KBh, if this answer provided you a solution to your question, then you should mark it as accepted
@Kbh That matter is described well by ashleedawg in the answer below. If you are satisfied with my answer you may select it as an accepted answer, that's how you say thanks on StackOverflow.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.