How do you update a public constant

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

I need to update a public constant every time I add a sheet.

Is there a better alternative?
 
Greg,

That should work. There is a NewSheet event that you can use

Private Sub Workbook_NewSheet(ByVal Sh As Object)
mySheets = mySheets + 1
End Sub

You will need to initialise it every time you open the workbook.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Greg,

You can't change the value of a constant; that's why they're
called constants. Create a public variable in a regular code
module:

Public NumSheets As Long

Then in the ThisWorkbook code module

Private Sub Workbook_NewSheet(ByVal Sh As Object)
NumSheets = ThisWorkbook.Worksheets.Count
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Or just use

ThisWorkbook.Worksheets.Count

directly. Just a thought.
 
Chip Pearson said:
Greg,

You can't change the value of a constant; that's why they're
called constants. Create a public variable in a regular code
module:

Public NumSheets As Long

Then in the ThisWorkbook code module

Private Sub Workbook_NewSheet(ByVal Sh As Object)
NumSheets = ThisWorkbook.Worksheets.Count
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

Thanks Chip,

I am curious to know why the body of the code needs to be in the
ThisWorkbook code module. Chip, what other circumstances would this
be required, and why would it used in the ThisWorkbook code module?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top