vba difference between type of and type name?

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Here are a few differences to get the ball rolling:


  • TypeOf can only be used on objects. TypeName can be used on objects and literals.
  • TypeOf will return a runtime error if the object variable is nothing. TypeName will return the string "Nothing".
  • TypeOf returns a boolean result. TypeName returns a string result.
  • TypeOf is faster than TypeName.
  • TypeOf returns true for a given type if the object inherits from that given type, as demonstated in example code below. A common situation in Excel VBA where it's important to understand this feature is when you're using TypeOf to identify MSForms library controls (checkbox, option button, toggle button).


Class Module called cTest
Code:
Option Explicit
Implements ITest

Class Module called ITest
Code:
Option Explicit

Standard Module called mProgram
Code:
Option Explicit

Sub main()
    Dim clsTest As cTest
    
    Set clsTest = New cTest
    
    Debug.Print TypeOf clsTest Is cTest
    Debug.Print TypeOf clsTest Is ITest
    Debug.Print TypeName(clsTest)
    
End Sub
 
Last edited:
Upvote 0
Hi,

what do you mean by string result

TypeOf returns a boolean result. TypeName returns a string result.


So which one do you recommend to use?
 
Upvote 0
what do you mean by string result
I mean it returns a word describing the variable type. For example, in the below code TypeName returns the string "Range".
Code:
Sub foo()
    Dim r As Range
    Set r = Range("A1")
    MsgBox TypeName(r)
End Sub


So which one do you recommend to use?
Each one has situations where it is better to use than the other (a number of which I have already listed), but if you have a situation where you could equally use either then it would be better to use TypeOf because it is faster. There are also some differences in relation to use against arrays and user defined types, which I didn't list in my first reply.
 
Last edited:
Upvote 0
Hey Colin,

Regarding Implements (which I've never used):

Can it appear more than once, if a class implements more than one interface? Or does it explicitly describe the totality of the interface?
 
Last edited:
Upvote 0
Hey shg,

I've never used multiple interface inheritance in VBA, but I just did a quick test and it looks like it does support it.

Class I1
Code:
Sub foo()
End Sub

Class I2
Code:
Sub foo()
End Sub

Class cTest
Code:
Implements I1
Implements I2

Private Sub I1_foo()
End Sub
Private Sub I2_foo()
End Sub


Standard code module
Code:
Sub main()
    Dim clsTest As cTest
    
    Set clsTest = New cTest
    
    Debug.Print TypeOf clsTest Is cTest 'true
    Debug.Print TypeOf clsTest Is I1 'true
    Debug.Print TypeOf clsTest Is I2 'true
    
End Sub
 
Upvote 0
Thanks, Colin.

How does one define the interface I1 or I2?
 
Upvote 0
When you create the class you also create the interface.
 
Upvote 0
Howdy, Rory.

Okay ... how does the interface name get bound? Can you show a simple example?

EDIT: Interface name == class name?
 
Upvote 0
Howdy, Rory.Okay ... how does the interface name get bound? Can you show a simple example?EDIT: Interface name == class name?
Yes - same name. Convention is to prefix the class name with I if just using it as an interface
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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