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"
- I have a situation the User selects another workbook via FileDialog and assigns it to Public Workbook variable wbDestin (Works perfectly)
- The code then loops through the Userforms and assigns the Userform Names to a ComboBox List. (Works perfectly)
- User selects a Userform from the ComboBox. (Works perfectly)
- The code assigns the selected Userform to a VBComponent variable. (Works perfectly and Debug.Print establishes the correct Userform Name)
- 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
