-
1
- #1
To all,
It becomes a common programming error to declare variables in a group. VBA does not support this feature.
Dim String1 , String2 as String will declare String1 as a Variant type and String2 as a String type.
Using the Variant type when you know the type is not Variant is inefficient.
Paste the code below in a standard module:
Dim mytype As Variant
Sub DeclareCorrect()
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Str4 As String
mytype = TypeName(Str1)
Debug.Print "Str1 " & "is " & mytype
mytype = TypeName(Str2)
Debug.Print "Str2 " & "is " & mytype
mytype = TypeName(Str3)
Debug.Print "Str3 " & "is " & mytype
mytype = TypeName(Str4)
Debug.Print "Str4 " & "is " & mytype
End Sub
Sub DeclareIncorrect()
Dim Str1, Str2, Str3, Str4 As String
mytype = TypeName(Str1)
Debug.Print "Str1 " & "is " & mytype
mytype = TypeName(Str2)
Debug.Print "Str2 " & "is " & mytype
mytype = TypeName(Str3)
Debug.Print "Str3 " & "is " & mytype
mytype = TypeName(Str4)
Debug.Print "Str4 " & "is " & mytype
End Sub
PS: My apologies if this info has been posted before. I have checked and didn't find any similar posting.
It becomes a common programming error to declare variables in a group. VBA does not support this feature.
Dim String1 , String2 as String will declare String1 as a Variant type and String2 as a String type.
Using the Variant type when you know the type is not Variant is inefficient.
Paste the code below in a standard module:
Dim mytype As Variant
Sub DeclareCorrect()
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Str4 As String
mytype = TypeName(Str1)
Debug.Print "Str1 " & "is " & mytype
mytype = TypeName(Str2)
Debug.Print "Str2 " & "is " & mytype
mytype = TypeName(Str3)
Debug.Print "Str3 " & "is " & mytype
mytype = TypeName(Str4)
Debug.Print "Str4 " & "is " & mytype
End Sub
Sub DeclareIncorrect()
Dim Str1, Str2, Str3, Str4 As String
mytype = TypeName(Str1)
Debug.Print "Str1 " & "is " & mytype
mytype = TypeName(Str2)
Debug.Print "Str2 " & "is " & mytype
mytype = TypeName(Str3)
Debug.Print "Str3 " & "is " & mytype
mytype = TypeName(Str4)
Debug.Print "Str4 " & "is " & mytype
End Sub
PS: My apologies if this info has been posted before. I have checked and didn't find any similar posting.