Prevent renaming worksheets without locking the workbook structure

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,806
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have seen this question asked before and the usual workaround is to restore the worksheet name after the sheet has been renamed.. This is often done in the Worksheet Selection Change or deactivate events as follows :
Code:
Private Sub worksheet_SelectionChange(ByVal Target As Excel.Range)
    If ActiveSheet.Name <> "SheetOldName" Then
        ActiveSheet.Name = "SheetOldName"
    End If
End Sub

For a more defensive programming style and for a bit of fun, here is a pseudo-event alternative that actually fires the code before the user attempts to rename the worksheet...


Code in the ThisWorkbook Module:
Code:
Option Explicit

Private WithEvents CmndBrs As CommandBars

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
    Private Declare PtrSafe Function DestroyWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private hwnd As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private hwnd As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Private Sub Workbook_Open()
    Set CmndBrs = Application.CommandBars
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If CmndBrs Is Nothing Then Set CmndBrs = Application.CommandBars
End Sub

Private Sub CmndBrs_OnUpdate()
    Dim bCancel As Boolean
    
    hwnd = FindWindowEx(FindWindowEx(Application.hwnd, 0, "XLDESK", vbNullString), 0, "EXCEL=", vbNullString)
    
    If hwnd Then
        Call Workbook_SheetBeforeRename(ActiveSheet, bCancel)
        If bCancel Then DestroyWindow hwnd
    End If
End Sub

[B][COLOR=#008000]'======================================================================
'SheetBeforeRename Pseudo-event - Example : Prevents renaming Sheet1
'======================================================================[/COLOR][/B]
Private Sub Workbook_SheetBeforeRename(ByVal Sh As Object, ByRef Cancel As Boolean)
    If Sh Is Sheet1 Then
        [COLOR=#0000ff][B]Cancel = True[/B][/COLOR]
        MsgBox "You can't rename sheet:  '" & Sh.Name & "'", vbCritical
    End If
End Sub
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

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