Can I simultaneously declare and assign a variable in VBA? Can I simultaneously declare and assign a variable in VBA? vba vba

Can I simultaneously declare and assign a variable in VBA?


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"


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

Dim w As New Widget

But not with strings or variants.


You can define and assign value as shown below in one line. I have given an example of two variables declared and assigned in single line. if the data type of multiple variables are same

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