Search code examples
exceluserformvba

How do I list/print control names and/or properties on a VBA form?


I am updating a userform, and have added many more controls on separate tabs. I am getting ready to update my Initialize sub, and was wondering if there is a feature that will allow me to list and/or print all the control-objects on the form?

Having their other properties would be swell as well, since it would give me a map of what I need to set up, as well as use it as a checklist to make sure I complete everything that's needed. It would be more efficient to do that than run through them all, hope I have the right names and cell-references, wash/rinse/repeat.

Thanks


Solution

  • Sub ListControls() 
        Dim lCntr As Long 
        Dim aCtrls() As Variant 
        Dim ctlLoop As MSForms.Control 
    
         'Change UserForm Name In The Next Line
        For Each ctlLoop In MyUserForm.Controls 
            lCntr = lCntr + 1: Redim Preserve aCtrls(1 To lCntr) 
            'Gets Type and name of Control  
            aCtrls(lCntr) = TypeName(ctlLoop)&":"&ctlLoop.Name 
        Next ctlLoop 
         'Change Worksheet Name In The Next Line
        Worksheets("YrSheetName").Range("A1").Resize(UBound(aCtrls)).Value = Application.Transpose(aCtrls) 
    End Sub 
    

    This worked perfectly, adding all controls to a manually created sheet. Make sure to read comments and make changes required for individual projects.

    Thanks to the folks at OzGrid who answered this question many moons ago. Lesson: keep trying different words in Google as long as you have options.