我可以在 VBA 中同时声明和分配变量吗?

发布于 2024-09-09 06:10:07 字数 206 浏览 9 评论 0原文

我可以将以下声明和赋值转换为一行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

音栖息无 2024-09-16 06:10:07

不幸的是,VBA 中没有速记,如果您希望将其放在一行以提高可读性,您将得到的最接近的是使用 : 连续字符的纯粹视觉效果;

Dim clientToTest As String:  clientToTest = clientsToTest(i)
Dim clientString As Variant: clientString = Split(clientToTest)

提示(其他答案/评论的摘要):也适用于对象(Excel 2010):

Dim ws  As Worksheet: Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim ws2 As New Worksheet: ws2.Name = "test"

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;

Dim clientToTest As String:  clientToTest = clientsToTest(i)
Dim clientString As Variant: clientString = Split(clientToTest)

Hint (summary of other answers/comments): Works with objects too (Excel 2010):

Dim ws  As Worksheet: Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim ws2 As New Worksheet: ws2.Name = "test"
听不够的曲调 2024-09-16 06:10:07

您可以使用对象来做到这一点,如下所示。

Dim w As New Widget

但不适用于字符串或变体。

You can sort-of do that with objects, as in the following.

Dim w As New Widget

But not with strings or variants.

撧情箌佬 2024-09-16 06:10:07

事实上,你可以,但不是那样。

Sub MySub( Optional Byval Counter as Long=1 , Optional Byval Events as Boolean= True)

'code...

End Sub

您可以在调用子函数时以不同的方式设置变量,或者让它们保持默认值。

in fact, you can, but not that way.

Sub MySub( Optional Byval Counter as Long=1 , Optional Byval Events as Boolean= True)

'code...

End Sub

And you can set the variables differently when calling the sub, or let them at their default values.

是你 2024-09-16 06:10:07

您可以在一行中定义并分配一个值,如下所示。我给出了在一行中声明和分配两个变量的示例。如果多个变量的数据类型相同:

Dim recordStart, recordEnd As Integer: recordStart = 935: recordEnd = 946

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:

Dim recordStart, recordEnd As Integer: recordStart = 935: recordEnd = 946
初心 2024-09-16 06:10:07

在某些情况下,可以通过使用 With 语句

例如,

    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    If fd.Show Then
        'use fd.SelectedItems(1)
    End If

这可以重写为

    With Application.FileDialog(msoFileDialogSaveAs)
      If .Show Then
        'use .SelectedItems(1)
      End If
    End With

In some cases the whole need for declaring a variable can be avoided by using With statement.

For example,

    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    If fd.Show Then
        'use fd.SelectedItems(1)
    End If

this can be rewritten as

    With Application.FileDialog(msoFileDialogSaveAs)
      If .Show Then
        'use .SelectedItems(1)
      End If
    End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文