When adding controls to a userform, what is the difference between the following. I am confused as to when it is appropriate to use any particular one of these.
Dim aButton1 as MSFORMS.CommandButton
Dim aButton2 as Control.CommandButton
Dim aButton3 as CommandButton
Add UserForm first. Then in VBA IDE press F2, the object browser appears. In the upper left corner is combo box, select MSForms. In the classes list you can see the classes which belongs to MSForms object library.
You can see CommandButton and Control in that list:
To declare a variable of type CommandButton in your code:
Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton
The variable button1 is of type CommandButton from MSForms for sure. The button2 could be your own class defined in your local VBA Project ... but if your local VBA project doesn't contain any class with such name it will be considered from MSForms as well. However if you reference another object library say 'MSFoo' which will contain class CommandButton as well you will have to declare them fully qualified like this:
Dim button1 As MSForms.CommandButton
Dim button2 As MSFoo.CommandButton
To declare a variable of type Control in your code:
Dim controlObject As MSForms.Control
Use variable of type Control like kind of a base class for controls. E.g. to enumarate Controls collection:
For Each controlObject In Me.Controls
Debug.Print VBA.TypeName(controlObject)
Next controlObject
Or as a parameter in function which expects not just only one type of control:
Private Sub PrinControlName(ByRef c As MSForms.Control)
Debug.Print c.Name
End Sub
So using fully qualified names like MSForms.CommandButton is in general approprite i think. Using Control.CommandButton is wrong and won't compile until you reference some object library named 'Control' with class CommandButton in it.
EDIT:
Here an example of locally created class with same name like MSForm.CommandButton:
And how TypeName and TypeOf work in this case:
Option Explicit
Private m_buttonMsForms As MSForms.CommandButton
Private m_buttonLocal As CommandButton
Private Sub UserForm_Initialize()
Set m_buttonMsForms = Me.Controls.Add( _
"Forms.CommandButton.1", "testMsButton", True)
Set m_buttonLocal = New CommandButton
m_buttonLocal.Name = "testLocalButton"
Debug.Print "We have two instances of two different button types: " & _
m_buttonLocal.Name & " and " & m_buttonMsForms.Name
' Check instances with TypeName function
' TypeName function returns same results
If VBA.TypeName(m_buttonMsForms) = VBA.TypeName(m_buttonLocal) Then
Debug.Print "TypeName of both buton types returns same result"
End If
' Check instances with TypeOf operator
' TypeOf doen't work with not initialised objects
If m_buttonLocal Is Nothing Or m_buttonMsForms Is Nothing Then _
Exit Sub
' TypeOf operator can distinguish between
' localy declared CommandButton type and MSForms CommandButton
If TypeOf m_buttonLocal Is MSForms.CommandButton Then _
Debug.Print "m_buttonLocal Is MSForms.CommandButton"
If TypeOf m_buttonMsForms Is CommandButton Then _
Debug.Print "m_buttonMsForms Is CommandButton"
If TypeOf m_buttonLocal Is CommandButton Then _
Debug.Print "m_buttonLocal Is CommandButton"
If TypeOf m_buttonMsForms Is MSForms.CommandButton Then _
Debug.Print "m_buttonMsForms Is MSForms.CommandButton"
End Sub
Output:
We have two instances of two different button types: testLocalButton and testMsButton TypeName of both buton types returns same result m_buttonLocal Is CommandButton m_buttonMsForms Is MSForms.CommandButton
Numbers 1 and 3 create the same type of control. The first statement is just fully qualified - equivalent to using Dim ws as WorkSheet or Dim ws as Excel.WorkSheet.
I'm not familiar with the 2nd type - "Control.CommandButton" - and it doesn't compile for me inside a userform in Excel 2010.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With