Can I simultaneously declare and assign a variable in VBA?

后端 未结 5 1803
[愿得一人]
[愿得一人] 2020-12-01 02:24

I\'m new to VBA and want to know if I can convert the following declaration and assignment into one line:

Dim clientToTest As String
clientToTest = clientsTo         


        
5条回答
  •  情书的邮戳
    2020-12-01 03:02

    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"
    

提交回复
热议问题