Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用MsgBox创建和提取名称变量时出现语法错误_Vba_Syntax Error - Fatal编程技术网

Vba 使用MsgBox创建和提取名称变量时出现语法错误

Vba 使用MsgBox创建和提取名称变量时出现语法错误,vba,syntax-error,Vba,Syntax Error,我正在尝试执行一个宏来使用MsgBox创建和提取名称变量。我尝试在MsgBox代码中不使用“u”的情况下运行它,但它不起作用。它显示了一条消息,即定义名称为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”

我正在尝试执行一个宏来使用MsgBox创建和提取名称变量。我尝试在MsgBox代码中不使用“u”的情况下运行它,但它不起作用。它显示了一条消息,即定义名称为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

您使用的引号看起来很奇怪,这样就可以了,您所指的
\ucode>是一个行连续字符

此外,你文章中的引号是不正确的——尽管这可能是你如何将文本复制到此网站的结果(也许你必须在这个过程中“通过电子邮件发送给自己”)。一旦引号从
更改为
”,则其中任何一个都将正常运行:

带行延续:

Msgbox “Surname is” & fname & “” & “ the number of characters are” & _
    len(fname)
Msgbox “Surname is” & fname & “” & “ the number of characters are” & len(fname)
Debug.Print "Surname is """ & lName & """ and the number of characters is " & Len(lName)
不带行延续:

Msgbox “Surname is” & fname & “” & “ the number of characters are” & _
    len(fname)
Msgbox “Surname is” & fname & “” & “ the number of characters are” & len(fname)
Debug.Print "Surname is """ & lName & """ and the number of characters is " & Len(lName)
我想这就是你想要做的:

Msgbox “Surname is” & fname & “” & “ the number of characters are” & _
    len(fname)
Msgbox “Surname is” & fname & “” & “ the number of characters are” & len(fname)
Debug.Print "Surname is """ & lName & """ and the number of characters is " & Len(lName)

在您的示例中,您对名字和姓氏都使用了
fName
。此外,我们可以在变量周围加引号。

这可能是因为您的引号看起来很有趣吗?例如
”“
我想您知道的另一件事是,由于您定义lname和fname的方式,这段代码对于几乎任何其他名称都无法正常工作。这是有效的。为什么在MsgBox行中的“与”后面放一个下划线?谢谢。@KBh它是一个空格,后跟一个下划线和一个换行符。这意味着下一行中的代码是空的。”在同一行中进行处理。这是一种软换行,只是为了提高代码的可视性并避免很长的代码行。这与在一行中编写所有代码相同。@KBh,如果此答案为您的问题提供了解决方案,那么您应该将其标记为accepted@Kbh阿什利道格在下面的回答中很好地描述了这件事对我的回答满意,你可以选择它作为一个被接受的答案,这就是你在StackOverflow上说谢谢的方式。