Having Big problems getting Started Learning Event Programming

I_Batman

Board Regular
Joined
Oct 14, 2011
Messages
62
I am trying to create an Workbook Level Event using the Workbook_NewSheet event.

I am trying some very simple stuff to begin with.
Sheet 1 has 3 Names associated with it.
They are Duty, HST, and Profit.
It has headers in Row 1, like Column 1 is Description

I am trying to duplicate this information associated with sheet 1 every time I create a new sheet, plus add a new Name called Freight, with a value of $0.36.

The code below is the very, very basic start.

I don't even believe the code is running, because when I click on Newsheet (low left icon on the actual workbook), it creates a new worksheet but when I look under the Names manager the new Name Freight does not exist, plus there is no value of "Description" in Cell A1.

How do I get this to even initiated?

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
 Names.Add Name:="Duty", RefersTo:=0.05
 Names.Add Name:="HST", RefersTo:=0.13
 Names.Add Name:="Profit", RefersTo:=0.12
 Names.Add Name:="Freight", RefersTo:=0.36
 
 Cells(1, 1).Value = "Description"
 
End Sub
thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Works for me:
Maybe events have been disabled.

Type in the immediate window:
Code:
Application.EnableEvents = True
(or restart Excel)

It doesn't matter but I almost always qualify code that is not inside it's parent container:
Code:
Cells(1, 1).Value = "Description"
I would write:
Code:
Sh.Cells(1, 1).Value = "Description"

Adding the names is mildly redundant since they are workbook-level and therefore exist for all sheets (though the code works, either to add the name if they don't exist, or re-add them if the do).
 
Upvote 0
That works fine for me but do you really want to redefine those names every time a sheet is added. Perhaps they should be worksheet level names like

Code:
 ActiveSheet.Names.Add Name:="Duty", RefersTo:=0.05

Are events enabled? Press CTRL + G, type

Application.EnableEvents=True

then press Enter.
 
Upvote 0
Thanks for the help folks.
I think I may have had the debugger locked.
It does appear to be working now, at least the start does.
I will certainly look to the advice about renaming the variables so they apply to the specific sheets.

Thanks again. I am sure there will be many, many more questions.
 
Upvote 0

Forum statistics

Threads
1,222,415
Messages
6,165,895
Members
451,993
Latest member
rowebca

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