VBA CVar() function question

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:

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:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
While this thread is old, it remains unanswered. Nevertheless, an answer may be helpful to others that come across this post. View a variant is as a container that can hold various data types, e.g. Integers, Floating Point Numbers, Text Strings, etc. Thus if one of the operands in a comparison is a variant, then no type mismatch error will be thrown by the procedure.
Regards,
WHG
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top