VB - Clear a named selection in a newly created sheet when it's created?

andyreloaded

Board Regular
Joined
Aug 1, 2006
Messages
80
I have a worksheet that gets copied frequently within the same workbook via the CTRL+CLICK and drag feature. In this worksheet I have the macro below to clear the contents of the range when the macro button is pressed. Is there any way to make this macro fire automatically only on the new sheet, and only when a new sheet is created?

Sub Clear_WSMESSAGEALL()
' Select named range WSMESSAGEALL and clear contents
Application.Goto Reference:="WSMESSAGEALL"
Selection.ClearContents
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Application.Goto Reference:="HOME"
End Sub

Thanks,
Andy
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
There is a Workbook_NewSheet event procedure.

I found some examples of that event, but I can't get them to work. This one should move the new sheet to the end of the book, which if I could get it to work I would try to modify it. Should code like this be put in This Workbook section of VB?

Sub ThisWorkbook_NewSheet(ByVal Sh As Object) _
Handles Me.NewSheet

Dim newSheet As Excel.Worksheet = CType(Sh, Excel.Worksheet)
newSheet.Move(After:=Me.Sheets(Me.Sheets.Count))
End Sub
 
Upvote 0
That's not VBA. Try this in the ThisWorkbook module:

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Sh.Move After:=Me.Sheets(Me.Sheets.Count)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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