Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data type handling 1

Status
Not open for further replies.

ddcroit

Technical User
Jul 17, 2008
12
US
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.
 
you can also add Str1, Str2, Str3 and Str4 to "Watch" to view their true "Type
 


This is the kind of post that belongs in a FAQ.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
It seems a very common error in the code posted here. I agree it should be a FAQ. If nothing else, we can point to the FAQ when the error shows up.

The quick bottom line:
WRONG: Dim String1, String2 as String
(results in String1 as variant, String 2 as string)

RIGHT: Dim String1 as String , String2 as String
(results in both variables declared as string)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top