Share via

Problem looping through controls on a Userform

OssieMac 48,001 Reputation points Volunteer Moderator
2019-04-29T11:28:58+00:00

For the following the reference  for both workbooks have "Microsoft Visual Basic for Applications Extensibility 5.3" and also "Trust Access to the VBA Project"

  1. I have a situation the User selects another workbook via FileDialog and assigns it to Public Workbook variable wbDestin  (Works perfectly)
  2. The code then loops through the Userforms and assigns the Userform Names to a ComboBox List. (Works perfectly)
  3. User selects a Userform from the ComboBox.   (Works perfectly)
  4. The code assigns the selected Userform to a VBComponent variable. (Works perfectly and Debug.Print establishes the correct Userform Name)
  5. The code then goes to the simple loop (between the asterisk lines below) and it will not loop through the controls in the Userform.

I will very much appreciate it if anyone can help me out with the code that is failing. I have no problem looping through controls in a Userform when I can use the actual Userform Name. My problem is when I am trying to use a variable for the Userform after selecting the required Userform from a ComboBox.

Private Sub cboUserForms_Change()   'ComboBox Change Event

    Dim vbCompFrm As VBComponent       'Used for the Userform Component

    Dim Ctrl As MSForms.Control                 'Used in the for Each Loop where I am having the problem

   'Following line assigns the Userform to a VBComponent Variable  (Me.cboUserForms.Value holds the Userform Name)

    Set vbCompFrm = wbDestin.VBProject.VBComponents(Me.cboUserForms.Value)

    Debug.Print vbCompFrm.Name      'This returns the correct Userform Name

    '***********************************

    For Each Ctrl In vbCompFrm.Controls     '<---This line fails. See Screen Shot below

        Debug.Print Ctrl.Name                       'This line used to simplify the code example for this forum

    Next Ctrl

    '***********************************

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2019-04-29T12:11:47+00:00

The error message is right, a VBComponent does not have the Controls collection. ;-)

But the Designer (which is the Userform) of that VBComponent has

For Each Ctrl In vbCompFrm.Designer.Controls

Andreas.

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2019-04-30T08:31:53+00:00

    I always try all of the Intellisence DropDown

    Hint: Use the watch window and explore the type of the variables, then you can find it easier:

    Andreas.

    Was this answer helpful?

    0 comments No comments
  2. OssieMac 48,001 Reputation points Volunteer Moderator
    2019-04-29T20:01:59+00:00

    Thank you Andreas. Exactly what I required and your reply is really appreciated.

    I feel sure that I tried the Designer option because when I can't find the correct syntax, I always try all of the Intellisence DropDown options. However, I have to assume that it was me and I had another typo error or something which caused it to fail and therefore thought that it was not the required option.

    Was this answer helpful?

    0 comments No comments