Looping through option buttons

cstrong

New Member
Joined
Jun 4, 2015
Messages
1
Super noob here and looking for a helping hand. Below is what I have written thus far and should give a rough idea of what Im trying to accomplish. I have well over 200 option buttons which work in pairs and instead of copying and pasting and switching around letters and numbers I figure there must be a way to loop through them and save myself boat loads of time.

Code:
Private Sub new1_Click()
If new1.Value = True Then Range("X11").Value = (Range("X9") / Range("X13"))
If new1.Value = True Then Range("X12").Value = (Range("X9") / Range("X57"))
End Sub
Private Sub resink1_Click()
If resink1.Value = True Then Range("x11").Value = (Range("x10") / Range("x13"))
If resink1.Value = True Then Range("x12").Value = (Range("x10") / Range("x57"))
End Sub
Private Sub new2_Click()
If new2.Value = True Then Range("y11").Value = (Range("y9") / Range("y13"))
If new2.Value = True Then Range("y12").Value = (Range("y9") / Range("y57"))
End Sub
Private Sub resink2_Click()
If resink2.Value = True Then Range("y11").Value = (Range("y10") / Range("y13"))
If resink2.Value = True Then Range("y12").Value = (Range("y10") / Range("y57"))
End Sub

Notice: The option buttons increase by 1 and Columns move to the right by 1.

Im sure there are 100 ways to skin this cat and probably a great number that are faster than what Ive got here.
Please Help!

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Welcome to the Board

- The code below will generate the code you want.
- To use it, add a reference to the Microsoft Visual Basic Extensibility Library at the VBE. Also, go to Excel options/trust centre/settings/macro settings and check the box for “trust access to the VBA Project”

Code:
Sub CreateCode()
Dim vbc As VBComponent, code$, i%
Set vbc = ThisWorkbook.VBProject.VBComponents("Sheet1") ' module to place code
With vbc.CodeModule
    code = ""
    For i = 1 To 2      ' adjust maximum index
        code = code & "Private Sub new" & i & "_Click" & vbCr
        code = code & "if new" & i & ".value=true then cells(11," & (23 + i) & _
        ")=cells(9," & (23 + i) & ")/cells(13," & (23 + i) & ")" & vbCr
        code = code & "if new" & i & ".value=true then cells(12," & (23 + i) & _
        ")=cells(9," & (23 + i) & ")/cells(57," & (23 + i) & ")" & vbCr
        code = code & "end sub" & vbCr
        code = code & "private sub resink" & i & "_click" & vbCr
        code = code & "if resink" & i & ".value=true then cells(11," & (23 + i) & _
        ")=cells(10," & (23 + i) & ")/cells(13," & (23 + i) & ")" & vbCr
        code = code & "if resink" & i & ".value=true then cells(12," & (23 + i) & _
        ")=cells(10," & (23 + i) & ")/cells(57," & (23 + i) & ")" & vbCr
        code = code & "end sub" & vbCr
    Next
    .InsertLines .CountOfLines + 1, code
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

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