Russ Urquhart
Russ Urquhart

Reputation: 329

Execute code whenever a worksheet is added

I have a list box in a form that on initialization fills that list box with all the names of the current worksheets. I use code like this:

For Each wCs In ActiveWorkbook.Worksheets
    If Not Right(wCs.Name, 10) = "Eng_Labels" Then
        ListBox3.AddItem wCs.Name
    End If
Next wCs

(Eng_Labels is a worksheet we use for calculations so we don't want to show that.)

Is there a way to have this code, or something like it, run when a user adds a worksheet? Is there a worksheet_added type of event I can tie this to?

Upvotes: 2

Views: 89

Answers (2)

Our Man in Bananas
Our Man in Bananas

Reputation: 5977

yes, in VBA for the workbook, you can use the Workbook_NewSheet event that is executed by Excel every time a new sheet is added to the workbook

Upvotes: 3

Werner
Werner

Reputation: 15105

Add your details to the workbook's NewSheet event:

enter image description here

It is executed every time a new sheet is created.

Upvotes: 5

Related Questions