我可以在 VBA 中同时声明和分配变量吗?
我可以将以下声明和赋值转换为一行:
Dim clientToTest As String
clientToTest = clientsToTest(i)
或者
Dim clientString As Variant
clientString = Split(clientToTest)
Can I convert the following declaration and assignment into one line:
Dim clientToTest As String
clientToTest = clientsToTest(i)
or
Dim clientString As Variant
clientString = Split(clientToTest)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,VBA 中没有速记,如果您希望将其放在一行以提高可读性,您将得到的最接近的是使用
:
连续字符的纯粹视觉效果;提示(其他答案/评论的摘要):也适用于对象(Excel 2010):
There is no shorthand in VBA unfortunately, The closest you will get is a purely visual thing using the
:
continuation character if you want it on one line for readability;Hint (summary of other answers/comments): Works with objects too (Excel 2010):
您可以使用对象来做到这一点,如下所示。
但不适用于字符串或变体。
You can sort-of do that with objects, as in the following.
But not with strings or variants.
事实上,你可以,但不是那样。
您可以在调用子函数时以不同的方式设置变量,或者让它们保持默认值。
in fact, you can, but not that way.
And you can set the variables differently when calling the sub, or let them at their default values.
您可以在一行中定义并分配一个值,如下所示。我给出了在一行中声明和分配两个变量的示例。如果多个变量的数据类型相同:
You can define and assign a value in one line, as shown below. I have given an example of two variables declared and assigned in a single line. If the data type of multiple variables are the same:
在某些情况下,可以通过使用
With
语句。例如,
这可以重写为
In some cases the whole need for declaring a variable can be avoided by using
With
statement.For example,
this can be rewritten as