Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Worksheet Event in Excel

Tags:

excel

events

vba

What is the best way to get some VBA code to run when a excel sheet is renamed?

like image 539
macleojw Avatar asked Dec 21 '09 12:12

macleojw


People also ask

How do you rename a worksheet based on cell values in Excel?

(2) In the Rename Options section, check the Replace original sheet name option; (3) In the New Worksheet Name section, check the From Specific range option, click the button to open the second Rename Multiple Worksheets dialog box, select the cells that you will rename by their values, and click the OK button.

What is the use of rename worksheet?

These names (Sheet1, Sheet2, Sheet3, and so on) are not very descriptive; you might want to rename your worksheets to reflect what they contain. For instance, if your workbook contains your annual budget, each worksheet within the workbook might contain budgets for individual months.

Can you have 2 worksheet change events?

You cannot have more than one Worksheet_Change event procedure in the same worksheet module.


1 Answers

There apparently is no Event to handle this, even using the Application object. How annoying.

I'd probably try to capture it by storing the startup value of the Worksheet and checking it on as many events as possible - which is admittedly a hack.

The following seemed to work for me, Hope it helps.

In the ThisWorkbook module:

Private strWorksheetName As String

Private Sub Workbook_Open()
    strWorksheetName = shtMySheet.Name
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Call CheckWorksheetName
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Call CheckWorksheetName
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    Call CheckWorksheetName
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Call CheckWorksheetName
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Call CheckWorksheetName
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call CheckWorksheetName
End Sub

Private Sub CheckWorksheetName()
    'If the worksheet has changed name'
    If shtMySheet.Name <> strWorksheetName Then

        DoSomething

    End If
End Sub
like image 162
Nossidge Avatar answered Sep 29 '22 03:09

Nossidge