Always use Option Explicit
(as Tony points out), and be careful declaring your variables in a single Dim statement.
Always set the option to require variable declaration to true. (VBA Editor: Tools --> Options --> check "Require Variable Declaration") This will add the Option Explicit
statement for you automatically in all new modules.
I suspect that you intended to declare two variants and a string, because you use the str
prefix for the strStore
variable. Regardless, note that declaring variables in a single dim statement without explicitly setting the type for each will create variants. Consider the following:
Option Explicit
Sub DeclaringVariables()
Dim int1, int2, int3 As Integer
' int1 and int2 are variants, NOT integers!!!
int1 = "my name"
int2 = 4.23424
int3 = 5
MsgBox "int1: " & int1 & vbCrLf & "int2: " & int2 & vbCrLf & "int1: " & int3
End Sub
To declare the ints as integers, you must specify the type for each variable, whether you use one dim statement for all your variables or separate dim statements.
If you try the following, you'll get a type mismatch, as you would expect:
Sub MoreVariables()
Dim int1 As Integer, int2 As Integer, int3 As Integer
int1 = "my name"
int2 = 4.23424
int3 = 5
MsgBox "int1: " & int1 & vbCrLf & "int2: " & int2 & vbCrLf & "int1: " & int3
End Sub
One last thing: set your variable names with Camel or Pascal case. Whenever you use your variables in code, type them in all lower case. When you move to the next line, the editor will replace your lower-case variable name with the one you set in your declaration. This is a handy way to make sure you didn't type the name wrong so you can fix it immediately rather than wait for the compiler to complain when you run the code.