Run Macro "If" the number of sheets in a document change.

mikedevt

New Member
Joined
Feb 5, 2018
Messages
6
How do I code a macro to run "If" a new sheet is added? My code works with a button, I am trying to make it automated when a sheet is added or deleted.

I appreciate any help on this topic. Thanks in advance.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
'your macro here
End Sub


and....

Code:
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
call yourmacro
End Sub
 
Last edited:
Upvote 0
That did not work for me. I have a button that adds new sheets and a separate code to sort a table of contents.

My macro for the table of contents works when it adds a new sheet, I need to get it to run when a sheets gets deleted as well.

Thanks
 
Upvote 0
this is what I have right now and it is not working, it needs to go to Table of Contents before it runs the macro:

Code:
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
Sheets("Table of Contents").Select
Call UpdateTOC
End Sub




This code works (button click) when a sheet is added:

Code:
Private Sub AddNewJob_Click()

CopyRename

Sheets("Table of Contents").Select

UpdateTOC


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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