Last Sheet Name - VBA

shemayisroel

Well-known Member
Joined
Sep 11, 2008
Messages
1,867
Hi,

I found this code below that extracts the last sheet name. I need the code modified that it auto updates when a sheet is inserted as now you require to go to teh formula and refresh it in order to update the Last Sheet Name. Is it possible to build it in the code?

http://www.cpearson.com/excel/excelm.htm

Rich (BB code):
Public Function LastSheetName()
LastSheetName = Sheets(Sheets.Count).Name
End Function
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,

Maybe this

Paste the code below in ThisWorkbook code-page

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim shName As String
    
    shName = LastSheetName
    MsgBox shName

End Sub

M.
 
Upvote 0
Hi,

Maybe this

Paste the code below in ThisWorkbook code-page

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim shName As String
 
    shName = LastSheetName
    MsgBox shName
 
End Sub

M.

Thanks, got a #NAME error when putting =LastSheetName() in the formula bar.

What am I doing wring?

Pasted the code in "ThisWorkBook"
 
Upvote 0
The problem is that adding a sheet does not trigger a calculate event.
This will fix that
Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Calculate
End Sub
 
Upvote 0
Not sure which code you pasted in ThisWorkbook. Marcelo's code needs to be in ThisWorkbook.

But...
Code:
Public Function LastSheetName()
LastSheetName = Sheets(Sheets.Count).Name
End Function
...needs to be in a Standard Module.
 
Upvote 0
If you are putting the last sheet name in, say, A1 of Sheet1 (adjust to suit), try

1. Copy/Paste the function LastSheetName in a Standard Module

Code:
Public Function LastSheetName() As String
    LastSheetName = Sheets(Sheets.Count).Name
End Function

2. Put this code in ThisWorkbook code-page

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    
    Sheets("Sheet1").Range("A1") = LastSheetName
    
End Sub
 
Upvote 0
Thank you to

Marcelo Branco,

GTO and

mikerickson

... for your help. I've now resolved it.

Very greatful for your help and swift response to my questions.
 
Upvote 0
In my version of Excel the Last sheet name does not change after adding a sheet.

Unless you mean the name of the most recently added sheet.

But the code I see always grabs the last sheet name, which never changes...
 
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