Rename Excel Worksheet By Auto Increment

Mohsin110

New Member
Joined
Aug 26, 2023
Messages
23
Office Version
  1. 2021
Platform
  1. Windows
Hi, I have a workbook where I have to copy a worksheet several time with the same name so please suggest me a VBA code, if I rename any worksheet and if the name already exists it should auto increment the name by "-1" and if two sheets available already it should auto increment to "-3" and so on.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Excel renames a copied sheet automatically in the format Sheet1(2), Sheet1(3), etc.
By VBA :
VBA Code:
Sub v()
ActiveSheet.Copy After:=ActiveSheet
End Sub
 
Upvote 0
try below code:
VBA Code:
Option Explicit
Sub test()
Dim shname As String, sp, ws As Worksheet, max&
sp = Split(ActiveSheet.Name, "-")
For Each ws In Sheets
    If ws.Name Like sp(0) & "-*" Then
        If Split(ws.Name, "-")(1) > max Then max = Split(ws.Name, "-")(1)
    End If
Next
shname = sp(0) & "-" & max + 1
Sheets.Add
ActiveSheet.Name = shname
End Sub
 
Upvote 0
Excel renames a copied sheet automatically in the format Sheet1(2), Sheet1(3), etc.
By VBA :
VBA Code:
Sub v()
ActiveSheet.Copy After:=ActiveSheet
End Sub
Yes, if you copy the sheet of same name it will automatically in the format what you mentioned, but if you copy a random sheet then try to rename and if the name already exist it will popup a message that "The name is already exist" instead it should automatically make increment by 1, so please let me know if it's possible with VBA.
 

Attachments

  • img.png
    img.png
    16.4 KB · Views: 5
Upvote 0
You need 2 macros. One for copying sheets, and one for renaming sheets.

Assuming you will use the macro in post #2 for copying a sheet, try the following for renaming :

VBA Code:
Sub Rename_Sheet()
Dim sh$, temp As Range, x$, tempName$
On Error Resume Next
sh = InputBox("Enter revised sheet name.")
If sh = ActiveSheet.Name Or sh = "" Then Exit Sub
Set temp = Worksheets(sh).[A1]
If temp Is Nothing Then
    ActiveSheet.Name = sh
    Exit Sub
Else
    x = 1
    If InStr(sh, " (") > 0 Then tempName = Left(sh, InStr(sh, " (") - 1)
    Do
        x = x + 1
        If ActiveSheet.Name = tempName & " (" & x & ")" Then Exit Sub
    Loop Until Evaluate("ISREF('" & tempName & " (" & x & ")'!A1)") = False
End If
If InStr(sh, " (") > 0 Then
    tempName = Left(sh, InStr(sh, " (") - 1)
    ActiveSheet.Name = tempName & " (" & x & ")"
Else: ActiveSheet.Name = sh
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,822
Messages
6,181,164
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