Edit this macro so it checks if sheet name already exists

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I have macro that works great but every now and then a sheet name is chosen that is already in use.

Is it posible to add i a part that checks if the names in use and if so adds a number at the end
so if sheet name was "Tony" it would do "Tony 2"
starting at 2 so it shows its the second one?

heres my code:

Sub Copy_Sheet()
Application.EnableEvents = False
Sheets("Template").Copy After:=ActiveSheet

ActiveSheet.Name = ActiveSheet.Range("G7")

Application.EnableEvents = True
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi what about below, Feedback PLZ (y)
VBA Code:
 On Error Resume Next
    Set NewShe = ThisWorkbook.Sheets(Nm)
    On Error GoTo 0
    If ActiveSheet.Range("G7").Value = "" Then MsgBox "Range(""G7"")  is  Blank"
    If NewShe Is Nothing Then
        ActiveSheet.Name = ActiveSheet.Range("G7")
    Else
        For Each Sht In ThisWorkbook.Sheets
            If InStr(1, Sht.Name, Nm) <> 0 Then
                N = N + 1
            End If
        Next
        ActiveSheet.Name = ActiveSheet.Range("G7") & N
    End If
    
Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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