vba 如何将长字符串分成多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16624550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to break long string to multiple lines
提问by DougM
I'm using this insert statement in my code in vba excel but i'm not able to break it into more than one line
我在 vba excel 的代码中使用了这个插入语句,但我无法将它分成多行
SqlQueryString = "Insert into Employee values(" & txtEmployeeNo.Value & " _
,'" & txtContractStartDate.Value & "' _
,'" & txtSeatNo.Value & "' _
,'" & txtFloor.Value & "','" & txtLeaves.Value & "')"
It is giving error "Expected end of statement". Plz help.
它给出了错误“预期的语句结束”。请帮忙。
回答by DougM
You cannot use the VB line-continuation character inside of a string.
不能在字符串中使用 VB 行继续符。
SqlQueryString = "Insert into Employee values(" & txtEmployeeNo.Value & _
"','" & txtContractStartDate.Value & _
"','" & txtSeatNo.Value & _
"','" & txtFloor.Value & "','" & txtLeaves.Value & "')"
回答by Georges Brisset
you may simply create your string in multiple steps, a bit redundant but it keeps the code readable and maintain sanity while debugging or editing
您可以简单地在多个步骤中创建您的字符串,有点多余,但它可以在调试或编辑时保持代码可读并保持理智
SqlQueryString = "Insert into Employee values("
SqlQueryString = SqlQueryString & txtEmployeeNo.Value & " ,"
SqlQueryString = SqlQueryString & " '" & txtEmployeeNo.Value & "',"
SqlQueryString = SqlQueryString & " '" & txtContractStartDate.Value & "',"
SqlQueryString = SqlQueryString & " '" & txtSeatNo.Value & "',"
SqlQueryString = SqlQueryString & " '" & txtContractStartDate.Value & "',"
SqlQueryString = SqlQueryString & " '" & txtSeatNo.Value & "',"
SqlQueryString = SqlQueryString & " '" & txtFloor.Value & "',"
SqlQueryString = SqlQueryString & " '" & txtLeaves.Value & "' )"
回答by Santosh
If the long string to multiple lines confuses you. Then you may install mz-tools addin which is a freeware and has the utility which splits the line for you.
如果多行的长字符串让您感到困惑。然后您可以安装 mz-tools 插件,它是一个免费软件,并具有为您拆分线路的实用程序。
If your string looks like below
如果您的字符串如下所示
SqlQueryString = "Insert into Employee values(" & txtEmployeeNo.Value & "','" & txtContractStartDate.Value & "','" & txtSeatNo.Value & "','" & txtFloor.Value & "','" & txtLeaves.Value & "')"
Simply select the string > right click on VBA IDE > Select MZ-tools > Split Lines
只需选择字符串>右键单击VBA IDE>选择MZ-tools>分割线
回答by dgo
I know that this is super-duper old, but on the off chance that someone comes looking for this, as of Visual Basic 14, Vb supports interpolation. Sooooo cool!
我知道这已经很老了,但是如果有人来寻找这个的可能性很小,从 Visual Basic 14 开始,Vb 支持插值。太酷了!
Example:
例子:
SQLQueryString = $"
Insert into Employee values(
{txtEmployeeNo},
{txtContractsStartDate},
{txtSeatNo},
{txtFloor},
{txtLeaves}
)"
It works. Documentation Here
有用。 文档在这里
Edit: After writing this, I realized that the OP was talking about VBA. This will not work in VBA!!!However, I will leave this up here, because as someone new to VB, I stumbled upon this question looking for a solution to just this problem in VB.net. If this helps someone else, great.
编辑:写完这篇文章后,我意识到 OP 正在谈论 VBA。 这在 VBA 中不起作用!!!但是,我将把它留在这里,因为作为 VB 的新手,我偶然发现了这个问题,正在 VB.net 中寻找解决此问题的方法。如果这对其他人有帮助,那就太好了。