Loop to condense code

Chuck Moran

New Member
Joined
May 12, 2011
Messages
7
Good morning,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Currently using: Excel 2007
<o:p> </o:p>
I use a spreadsheet with forms for selecting specific information. There are multiple frames and many OptionButtons on each the frames.
<o:p> </o:p>
The OptionButton.Captions are set by the following code and it works OK:
<o:p> </o:p>
<o:p> </o:p>
Sub SetupWireInspectorSheet()
If Range("LISTS!a2") = "" Then
UserForm2.OptionButton131.Visible = False
Else: UserForm2.OptionButton131.Caption = Range("LISTS!a2")
End If
If Range("LISTS!a3") = "" Then
UserForm2.OptionButton132.Visible = False
Else: UserForm2.OptionButton132.Caption = Range("LISTS!a3")
End If

Exit Sub
<o:p> </o:p>
There are over 100 OptionButtons and the code is very lengthy. I’d like to shorten the code.
<o:p> </o:p>
Can someone tell me how to create a LOOP that will:
1 – increment the # of the OptionButton portion of the above code
2 – increment the # of the Range portion of the above code – is it different since it is inside quotes
<o:p> </o:p>
Thank you,
Chuck
</SPAN></SPAN>
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Well. I haven't tried it but
something like

Code:
For i = 2 To 3
    If Range("LISTS!a" & i).Value = "" Then
        .....
Next i

should work as well as optionbuttons hopefully.
 
Upvote 0
kpark91,

Your code worked well for the "Caption" portion.

I can't see how to make it work for the "Optionbutton" portion. I don't know how to separate the number from the UserForm2.OptionButtonxxx. I do realize I'll have to create another variable to increment the number.

Thanks again,
Chuck
 
Upvote 0
Hi,
I'm having difficulty (as in I can't make it work after reading many other posts) in separating the number (132) portion of the Optionbutton from the line:

UserForm2.OptionButton132.Caption = Range("LISTS!a3")

Since I have many OptionButtons, my intent is to create a loop increment the number portion of the line to make all the changes.

An help is greatly appeciated.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top