Macro to add buttons to every sheet except first sheet

holt3130

New Member
Joined
Jul 23, 2013
Messages
14
I have an excel file where the first sheet is an index of all the sheets in the workbook.
I need a macro that adds three buttons to every sheet except for the first sheet (the index)

I will need to run this macro again and again as new sheets are added. Rather than searching for the new sheets and adding the buttons the macro would just run on every sheet again so it will need to delete any buttons previously added and add the buttons again so that it is not adding buttons on top of buttons for the sheets that were already in the workbook.

This is what I have so far but I'm not sure how to skip the first sheet and how to delete all buttons before adding the buttons again.

Code:
Sub NextSheet()
ActiveSheet.Next.Select
End Sub
Sub PrevSheet()
ActiveSheet.Previous.Select
End Sub
Sub firstsheet()
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets("All Specs").Select
End Sub
Sub addButton()


Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
wks.Activate
ActiveSheet.Buttons.Add(650, 18, 60, 25).Select
Selection.OnAction = "PrevSheet"
Selection.Caption = "Previous"
ActiveSheet.Buttons.Add(730, 18, 60, 25).Select
Selection.OnAction = "NextSheet"
Selection.Caption = "Next"
ActiveSheet.Buttons.Add(810, 18, 60, 25).Select
Selection.OnAction = "firstsheet"
Selection.Caption = "To Index"
Next wks




End Sub
 
The Workbook_NewSheet event doesn't care where the new sheet is added, or by whom. It runs automatically.
It doesn't have to be called, it just runs.

Take a new workbook, add the Workbook_NewSheet code to the ThisWorkbook module. Add some sheets.
Try the code. Try to make it fail.

(Note that the linked routines won't be in the new workbook, so the buttons will do nothing.)

After the set up routine is run, you will never have to do anything, the buttons will be there on every new sheet.

Ah yes, now I understand.

works great!

Thank you!
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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