larinda4
Board Regular
- Joined
- Nov 15, 2021
- Messages
- 74
- Office Version
- 365
- Platform
- 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.
any help would be greatly appreciated.
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.