Sunday, 2 July 2017

Can I simultaneously declare and assign a variable in VBA?



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 = clientsToTest(i)


or




Dim clientString As Variant
clientString = Split(clientToTest)

Answer



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"

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...