antaeusguy
Board Regular
- Joined
- Mar 8, 2010
- Messages
- 81
I'm experienting with the VBa function CVar() to test how it works.
I wrote this code:
When this code is run, I got an error message: 13 - Type mismatch.
I guess this means an Integer can't be compared with a String data type.
So I modified the code with CVar() -> converting the x to a variant:
And the code works, a message box "Not match." poped-up, just like what I expected.
My understanding is it converted the Integer x of 0 value to a variant, then to a string -> "0" to compare with variable y, with a string value of "JP". There is no type mismatch error.
I then tried to modify the code with CVar() -> but this time converting the y to a variant:
It works as well, giving the message box "Not match."
However, I couldn't understand how it works.
x remains as Integer, with value 0. y is converted from string to variant, with value "JP". How does CVar() converts y from a variant to a Integer for comparison?
I wrote this code:
Code:
Sub TestVar()
Dim x As Integer
Dim y As String
x = 0
y = "JP"
If x = y Then
MsgBox "It matches."
Else
MsgBox "Not match."
End If
End Sub
When this code is run, I got an error message: 13 - Type mismatch.
I guess this means an Integer can't be compared with a String data type.
So I modified the code with CVar() -> converting the x to a variant:
Code:
Sub TestVar()
Dim x As Integer
Dim y As String
x = 0
y = "JP"
If CVar(x) = y Then
MsgBox "It matches."
Else
MsgBox "Not match."
End If
End Sub
And the code works, a message box "Not match." poped-up, just like what I expected.
My understanding is it converted the Integer x of 0 value to a variant, then to a string -> "0" to compare with variable y, with a string value of "JP". There is no type mismatch error.
I then tried to modify the code with CVar() -> but this time converting the y to a variant:
Code:
Sub TestVar()
Dim x As Integer
Dim y As String
x = 0
y = "JP"
If x = CVar(y) Then
MsgBox "It matches."
Else
MsgBox "Not match."
End If
End Sub
It works as well, giving the message box "Not match."
However, I couldn't understand how it works.
x remains as Integer, with value 0. y is converted from string to variant, with value "JP". How does CVar() converts y from a variant to a Integer for comparison?
Last edited: