how could implement code for multiple sheets instead of repeat many times for each sheet

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hi
I want implement this code for four sheets based on select option buttons . the optionbuttons' name are the same sheets names . so any match name for the optionbuttion is same tfor each sheet name should copy data to spicific sheet is matched with optionbutton . optionbutton1.caption="PURCHASE",optionbutton2.caption="SALES" ,optionbutton3.caption="STOCK",optionbutton4.caption="DATA" I don't want repeat code many times for each sheet. if there is procedure to make code is short for each sheet will be great.

VBA Code:
Private Sub CommandButton4_Click()
 Dim i As Long
   
   With Sheets("PURCHASE")
      .Range("e5").MergeArea = Me.TextBox1.Value
      .Range("e8").MergeArea = Me.TextBox2.Value

      For i = 1 To 11
         .Cells(i + 1, 1).Value = Me.Controls("TextBox" & i)
      Next i
      For i = 12 To 44
         .Cells(((i - 11) Mod 11) + 1, Int((i - 11) / 11) + 6).Value = Me.Controls("Textbox" & i)
      Next i
      For i = 2 To 45
         .Cells(((i - 1) Mod 11) + 1, Int((i - 1) / 11) + 2).Value = Me.Controls("ComboBox" & i)
      Next i
   End With
   TextBox34.Value = Me.TextBox12.Value * Me.TextBox23.Value
 TextBox35.Value = Me.TextBox13.Value * Me.TextBox24.Value
 TextBox36.Value = Me.TextBox14.Value * Me.TextBox25.Value
 
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,
You check which optionbutton has been selected in your code & use caption for your sheet name

VBA Code:
Private Sub CommandButton4_Click()
    Dim i           As Long
    Dim sheetname   As String
   
    For i = 1 To 4
        With Me.Controls("OptionButton" & i)
            If .Value Then sheetname = .Caption: Exit For
        End With
    Next i
   
    With Worksheets(sheetname)

'rest of code

Solution assumes that you have retained default names for your optionbuttons

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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