Tab name change issue

cpg84

Active Member
Joined
Jul 16, 2007
Messages
264
Platform
  1. Windows
Hi All,
As a follow on from my previous post a few weeks ago, this is the code I have at the moment. It works perfectly when I change the value in cell G7.

1666842688333.png


What I didn't account for was when you clear the contents of G7.


1666842769564.png

1666842822506.png


I want to be able to either have the tab name to stay as it was before G7's contents was cleared, or change to some other name (which I can specify in another cell that won't change its value).

I hope I have explained this well enough.

Thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
like this?

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) <> "G7" Then Exit Sub ' check if cell G7 changed
If Evaluate("=ISREF('" & Target.Value & "'!A1)") Then Exit Sub ' check if sheet exists
ActiveSheet.Name = Target.Value
End Sub
 
Upvote 0
Edit:
add G7="" to exit sub:

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) <> "G7" Or Target.Value = "" Or Target.Count > 1 Then Exit Sub ' check if cell G7 changed
If Evaluate("=ISREF('" & Target.Value & "'!A1)") Then Exit Sub ' check if sheet exists
ActiveSheet.Name = Target.Value
End Sub
 
Upvote 0
I think I have done something wrong here. It doesn't like the code for some reason.

1666844757343.png
 
Upvote 0
@cpg84
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.CountLarge > 1 Then Exit Sub

If Target.Address(0, 0) = "G7" And Target.Value <> "" Then
    On Error Resume Next
    If Not Evaluate("ISREF('" & Target.Value & "'!A1)") Then
        If Err.Number > 0 Then
        On Error GoTo 0
            MsgBox "Invalid sheet name" 'in case the new name doesn't meet a sheetname criteria, for example: it has ? or \ in it
        Exit Sub
        End If
            ActiveSheet.Name = Target.Value
    Else
        MsgBox "Sheet already exists"

    End If
End If
End Sub
 
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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