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

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
573
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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