Need to attach a macro to buttons in multiple worksheets

sbar7

New Member
Joined
Jan 14, 2015
Messages
9
I've got a big ole excel file with 504 sheets, 500 of which are identical with tab names of 1 through 500 and have two buttons on each sheet. Unfortunately, those buttons were supposed to have macros attached to them but it didn't work. Can anyone help me attach macros to the two buttons across all worksheets?

Button 1 should have this:
Sub Print_PO()
ActiveSheet.PageSetup.PrintArea = "A3:I47"
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End Sub

And Button 2 should have this:
Sub Print_Receipt()
ActiveSheet.PageSetup.PrintArea = "K3:T52"
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Since it looks like you'll only need to do this once, here's a quick little macro you can use, which doesn't include error checking. Note that it assumes the workbook containing the buttons is the active workbook.

Code:
Sub AssignMacrosToButtons()

    Dim i As Long
    
    For i = 1 To 500
        With Worksheets(i)
            .Buttons(1).OnAction = "Print_PO"
            .Buttons(2).OnAction = "Print_Receipt"
        End With
    Next i
    
End Sub

Hope this helps!
 
Upvote 0
I get the following error:
2PjVT.jpg
 
Upvote 0
The error means that a referenced worksheet does not contain a button (or a second button, depending on which line gave you the error). My understanding is that...

1) those buttons are Form controls, not ActiveX controls, and that

2) your sheets are named 1, 2, 3, and so on until 500.

Is this correct?
 
Upvote 0
Your assumptions are correct, but the first four sheets of the workbook do not have buttons, so I would just want this applied to the next 500 sheets which are named 1 - 500 like you said. I'm guessing it's those first four sheets that are causing the problem?

UPDATE:
Aha! That was it. I changed the "For i = 1 To 500" to "For i = 5 To 504" and it worked. I initially figured those numbers were referring to the actual sheet names and not the order of the sheets (I'm not good with VBA).

Thank you, Domenic!
 
Last edited:
Upvote 0
Sorry, my mistake, replace...

Code:
[COLOR=#333333]With Worksheets(i)[/COLOR]

with

Code:
With Worksheets(CStr(i))
 
Upvote 0
UPDATE:
Aha! That was it. I changed the "For i = 1 To 500" to "For i = 5 To 504" and it worked. I initially figured those numbers were referring to the actual sheet names and not the order of the sheets (I'm not good with VBA).

Actually, I originally meant to refer to those sheets by name, not by index. And so my last post fixes it. Again, sorry about that.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,593
Members
452,654
Latest member
mememe101

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