Count controls in a VBA UserForm

drom

Well-known Member
Joined
Mar 20, 2005
Messages
543
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and thanks in advance!

If I want to check how many UseForms I have in my VBA, I can Use:

Code:
Sub mToListUFs()
 Dim Obj As Object
Dim X As Integer
  For Each Obj In ThisWorkbook.VBProject.VBComponents
    If Obj.Type = 3 Then
      Debug.Print Obj.Name
      X = X + 1
    End If
  Next Obj
  Debug.Print X
End Sub

But How can I check if the UserForms in my VBA are UserForms with CheckBoxes, or UserForms with Buttons etc ?
or probably How can I check if my Userform is Empty with nothing on it yet
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
To count the number of controls in a userform named "UserForm1"...

Code:
Debug.Print UserForm1.Controls.Count

To loop through each control in a userform named "UserForm1"...

Code:
[COLOR=darkblue]Dim[/COLOR] Ctrl [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object
[/COLOR]
[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] Ctrl [COLOR=darkblue]In[/COLOR] UserForm1.Controls
    Debug.Print TypeName(Ctrl)
[COLOR=darkblue]Next[/COLOR] Ctrl

To loop through each userform that is already loaded and part of the UserForms collections...

Code:
[COLOR=darkblue]Dim[/COLOR] UF [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object
[/COLOR]
[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] UF [COLOR=darkblue]In[/COLOR] UserForms
    Debug.Print UF.Name & ", " & UF.Controls.Count
[COLOR=darkblue]Next[/COLOR] UF

To loop through each userform that is not already loaded and part of the UserForms collections...

Code:
[COLOR=darkblue]Dim[/COLOR] Obj [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
[COLOR=darkblue]Dim[/COLOR] UF [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]

[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] Obj [COLOR=darkblue]In[/COLOR] ThisWorkbook.VBProject.VBComponents
  [COLOR=darkblue]If[/COLOR] Obj.Type = 3 [COLOR=darkblue]Then[/COLOR]
    [COLOR=darkblue]Set[/COLOR] UF = UserForms.Add(Obj.Name)
    Debug.Print UF.Name & ", " & UF.Controls.Count
    Unload UF
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] Obj

Hope this helps!
 
Upvote 0
Hi Domenic, and Thanks !

is there any way of replacing within this code:

Code:
Dim Obj As Object
Dim UF As Object

For Each Obj In ThisWorkbook.VBProject.VBComponents
  If Obj.Type = 3 Then
    Set UF = UserForms.Add(Obj.Name)
    Debug.Print UF.Name & ", " & UF.Controls.Count
    Unload UF
  End If
Next Obj

ThisWorkbook.VBProject.VBComponents with Workbooks("eeee.xls").VBProject.VBComponents
or
ThisWorkbook.VBProject.VBComponents with activeworkbook.VBProject.VBComponents

I am gonna have to run me macro from a workbook other than the one with the UserForms

Thanks!
 
Last edited:
Upvote 0
Sorry, unfortunately I'm not aware of a way to accomplish what you want...
 
Upvote 0
Sorry for the delay.

This does the job:

Code:
Obj.Designer.Controls.Count

Thanks again
 
Upvote 0
Thanks drom, I completely overlooked the Designer property...
 
Upvote 0

Forum statistics

Threads
1,223,922
Messages
6,175,384
Members
452,639
Latest member
RMH2024

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