[RESOLVED] Looping through controls
I'm sure this has been asked many a time, and I'm sure for what is probably a repeat post, but after much attempt I'm still failing at simply looping through all the controls on my form and outputting the name and caption of that object if it is an optionButton, Frame, CheckBox, or Label.
I currently have the code just for frame looking like this...
vb Code:
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
MsgBox (ctl.Caption)
End If
Next ctl
This loop appears to iterate but never see a msgbox... The above code works for a frame, but not for option buttons.... Any help would be appreciated! thanks!
Re: Looping through controls
Try this
Code:
Private Sub CommandButton1_Click()
Dim wks As Worksheet
Dim OLEObj As OLEObject
Set wks = Worksheets("Sheet1")
For Each OLEObj In wks.OLEObjects
If TypeOf OLEObj.Object Is MSForms.OptionButton Then
MsgBox OLEObj.Name
End If
Next
End Sub
Re: Looping through controls
Quote:
I'm still failing at simply looping through all the controls on my form
When you are using controls in a Form then you need to try this...
vb Code:
Private Sub CommandButton1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
MsgBox ctl.Name
ElseIf TypeName(ctl) = "CommandButton" Then
MsgBox ctl.Name
ElseIf TypeName(ctl) = "Frame" Then
MsgBox ctl.Name
End If
Next
End Sub
Re: Looping through controls
Great Thanks! Used the latter, works perfectly... Thanks to both of you.