Capturing the name of each optionbutton and checkbox in userform

R

red6000

Hi

I have a userform (called 'ReqsFrm') that contains loads of optionbuttons
and checkboxes

I would like to create some code that writes the name/caption of each of the
elements of the form. Now I can do it by manually producing the code, but
I'm sure there must be some sort of looping code that will do it for me. So
I'm trying do to something like:

Dim OB as OptionButton
For each OB in ReqsFrm
Type name of OptionButton
Type caption of OtionButton
Next OB

and then the same for the checkboxes

Can anyone help.

Thanks
 
G

Greg Maxey

Red6000,

While I am not exactly sure I had to code the way I did, this may work:

Private Sub CommandButton1_Click()
Dim oButtonArray() As OptionButton
Dim oCheckboxArray() 'As CheckBox
Dim i As Long
Dim j As Long
Dim oControl As Control
For Each oControl In Me.Controls
If TypeOf oControl Is MSForms.OptionButton Then
ReDim Preserve oButtonArray(i)
Set oButtonArray(i) = oControl
i = i + 1
GoTo SkipRest
End If
If TypeOf oControl Is MSForms.CheckBox Then
ReDim Preserve oCheckboxArray(j)
Set oCheckboxArray(j) = oControl
j = j + 1
End If
SkipRest:
Next oControl
For i = 0 To UBound(oButtonArray)
ActiveDocument.Range.InsertAfter oButtonArray(i).Name & " " &
oButtonArray(i).Caption & vbCr
Next i
For j = 0 To UBound(oCheckboxArray)
ActiveDocument.Range.InsertAfter oCheckboxArray(j).Name & " " &
oCheckboxArray(j).Caption & vbCr
Next j
Me.Hide
End Sub
 
R

red6000

Thanks, that works great, what I would now like to do is expand the code to
run for each loaded UserForm (I have over 20 Userforms).

I did try the code below, but it seems to repeating he opitonbuttons and
checkboxes for each new UserForm it moves to:

Dim oButtonArray() As OptionButton
Dim oCheckboxArray() 'As CheckBox
Dim i As Long
Dim j As Long
Dim oControl As Control
Dim UF As Object
Load xxFrm
Load yyFrm
Load zzFrm

For Each UF In UserForms

For Each oControl In UF.Controls
If TypeOf oControl Is MSForms.OptionButton Then
ReDim Preserve oButtonArray(i)
Set oButtonArray(i) = oControl
i = i + 1
GoTo SkipRest
End If
If TypeOf oControl Is MSForms.CheckBox Then
ReDim Preserve oCheckboxArray(j)
Set oCheckboxArray(j) = oControl
j = j + 1
End If
SkipRest:
Next oControl
For i = 0 To UBound(oButtonArray)
ActiveDocument.Range.InsertAfter UF.Name & "," & oButtonArray(i).Name &
"," & oButtonArray(i).Caption & vbCr
Next i
For j = 0 To UBound(oCheckboxArray)
ActiveDocument.Range.InsertAfter UF.Name & "," & oCheckboxArray(j).Name
& "," & oCheckboxArray(j).Caption & vbCr
Next j

Next UF
 
R

red6000

Right I've got it to work by moving NextUF to aftert NextControl, but I've
had to lose prefixing the text with the UserForm Name.
 
R

red6000

Okay, I want to go back to having the NextUF at the end, but just before
that I need code to reset and clear the original created array, is this
possibl?

Thanks for the help.
 
R

red6000

Okay, all sorted now.

red6000 said:
Okay, I want to go back to having the NextUF at the end, but just before
that I need code to reset and clear the original created array, is this
possibl?

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top