Check if Sheet Exists - if it does, add a 1,2,3, etc to the end of the sheet name

larinda4

Board Regular
Joined
Nov 15, 2021
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hi Excel folks,

I've been trying to modify my code to check if the sheet name already exists. If it does exist, I want it to add a -1, -2, etc if the sheetname already exists. There could be up to 5, so if the sheetname already exists, I want it to add the next number available.

VBA Code:
  Dim wsOriginal As Worksheet
    Dim wsNew As Worksheet
    Dim wsOrderInput As Worksheet
    Dim sheetName As String


    ' Set references to the relevant worksheets
    Set wsOriginal = ThisWorkbook.Sheets("MR#") ' Original "MR#" sheet
    Set wsOrderInput = ThisWorkbook.Sheets("ORDER INPUT") ' ORDER INPUT sheet
    
    ' Create a copy of the "MR#" sheet and move it to the far right
    wsOriginal.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set wsNew = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' New copied sheet
    
   
    'Rename the new sheet
    sheetName = "MR#" & ThisWorkbook.ActiveSheet.Range("N3").Value
    ThisWorkbook.ActiveSheet.Name = sheetName

any help would be greatly appreciated.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Maybe something like
VBA Code:
Sub larinda()
    Dim wsOriginal As Worksheet
    Dim wsNew As Worksheet
    Dim wsOrderInput As Worksheet
    Dim sheetName As String
    Dim i As Long


    ' Set references to the relevant worksheets
    Set wsOriginal = ThisWorkbook.Sheets("MR#")  ' Original "MR#" sheet
    Set wsOrderInput = ThisWorkbook.Sheets("ORDER INPUT") ' ORDER INPUT sheet
 
    ' Create a copy of the "MR#" sheet and move it to the far right
    wsOriginal.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set wsNew = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' New copied sheet
 
 
    'Rename the new sheet
    sheetName = "MR#" & ThisWorkbook.ActiveSheet.Range("N3").Value
 
    If Not Evaluate("isref('" & sheetName & "'!A1)") Then
        ThisWorkbook.ActiveSheet.Name = sheetName
        Exit Sub
    Else
        For i = 1 To 6
            sheetName = "MR#" & ThisWorkbook.ActiveSheet.Range("N3").Value & "-" & i
            If Not Evaluate("isref('" & sheetName & "'!A1)") Then
                ThisWorkbook.ActiveSheet.Name = sheetName
                Exit Sub
            End If
        Next
    End If

End Sub

Please note I think you'll need to redefine the
VBA Code:
sheetName = "MR#" & ThisWorkbook.ActiveSheet.Range("N3").Value
line but not sure exactly what sheet you want the name from at that time (I suspect wsOrderInput), at the moment the new sheet will be the activesheet at that time
 
Last edited:
Upvote 0
When adding the new Sheet use this Function. I think it will deliver the required results. Remember to test on a copy of your Workbook.

Example:
VBA Code:
Set NewSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
With NewSheet
      .Name = CheckSheetName(SomeCellValueHere)
End With

The Function:
VBA Code:
Function CheckSheetName(ByVal sheet_name As String) As String
Dim sht As Worksheet
Dim i As Integer
For Each sht In ThisWorkbook.Sheets
    If sheet_name = Left(sht.Name, Len(sheet_name)) Then
        i = i + 1
    End If
Next sht
CheckSheetName = sheet_name & "-" & i
End Function
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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