I am looking for a method of comparing two string variables in VBA. The current statement I am using (which is not producing the desired result) is:
If Variable1 = Variable2 Then...
I have a VBA macro that assigns two variables in the first part of my code:
Dim Variable1 as String Variable1 = Left(Range("$F$3").Value, 4)
Dim Variable2 as String Variable2 = ("SheetName.B15")
I have tried using the following statement, but it still does not work correctly:
If Variable1 Like Variable2 Then...
Previously, the statement was working with a string comparison to the first variable, but it required that I hard-coded the "ABC" string. It looked like the below statement:
If Variable1 = "ABC" Then...
Does anybody know the syntax of a statement to compare two string variables, or could recommend a method for comparing them in a sub and return a binary result, which I could then compare?
Thanks!
Running VBA for Excel 2013 on Windows 8
Debug.Print
the value of each variable what do you see? – Tim Williams Jun 5 '13 at 22:28Debug.Print
shows that I was incorrectly assigning Variable2. Variable2 came back as"SheetName.B15"
. Using that information, I corrected my variable assignment to:Dim Variable2 As String Variable2 = Worksheets("SheetName").Cells(15,"B").Value
Thanks for the push! – zacc206 Jun 5 '13 at 22:51