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)
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)
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.
:
. There are some limitations as you can not have multiple value declarations on the same line (ie var1 = val1: var2 = val2
). It will bug out speradically and allow you to do this type of assignment sometimes but as a whole not suggested by this notation. –
Vocabulary Dim x As New T
syntax, which only works with objects. –
Sharasharai dim str as String: str = "value"
and dim str as Worksheet: set str = ActiveWorkbook.worksheets("Sheet1")
both work repeatedly. Although, if i do an Object instantiation dim ws as New Worksheet: set ws = ActiveWorkbook.Worksheets("Sheet1")
would error out like a any other invalid operation in VBA. –
Vocabulary New
keyword doesn't. That's all I'm saying. –
Sharasharai dim ws as New Worksheet: set ws = ActiveWorkbook.Worksheets("Sheet1")
works in Excel 2010! So New
causes no issues there. (of course it does not make much sense to initialize something that will be overridden immediately again) –
Territoriality 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.
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
recordStart
has type Variant
here before the assignment, not Integer
. Explicitly-specified data types only apply to one variable at a time in a Dim
statement. –
Meeting 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
© 2022 - 2024 — McMap. All rights reserved.