Automatically check for worksheet names and make adjustments

smiley3141

Board Regular
Joined
Aug 13, 2010
Messages
77
Hello, everyone. I am trying to check have VBA check to see if a worksheet name already exists before adding a worksheet. If it does, I want to make an Adjustment to the name and then check the new name (The adjustment adds " - 2" and then " - 3" and so on to the original name each time through the loop). This seemed pretty simple but I cannot make it work. If the original name is not one of the sheet names, it works fine, but it does not add a worksheet with the adjusted names even if they are not already one of the named sheets. Can someone help me? I am using Excel 2010.

My code is the following:

Sub AddNewWorksheet()
Dim NewName As String
Dim NumSheets As Long
Dim i As Long
Dim WorksheetsExists As Boolean

NumSheets = Sheets.Count

For i = 1 To NumSheets + 1

NewName = Range("MyTabName")


For Each Sht In ThisWorkbook.Worksheets
'Following line ignores case in comparison
If UCase(Sht.Name) = UCase(NewName) Then
WorksheetExists = True
Exit For
End If
Next Sht

If WorksheetExists = False Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = NewName
i = NumSheets + 1
Else
Range("TabNameAdj").Value = Range("TabNameAdj").Value + 1
End If

Next i
End Sub

Thanks for any help you can give me.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I would do something like this:

Code:
'Can be used to check if named object exists
' i.e. If Contains(Sheets, "Sheet1") Then .....
Function Contains(objCollection As Object, strName As String) As Boolean
    Dim o As Object
    On Error Resume Next
    Set o = objCollection(strName)
    Contains = (Err.Number = 0)
End Function
Sub AddSheet()


Dim Exists As Boolean
Dim Name As String
Dim NewName As String
Dim i As Long


Exists = True
Name = "Sheet1"
NewName = Name
i = 1


Do While Exists = True
Exists = Contains(Sheets, NewName)
NewName = Name & "-" & i
i = i + 1
Loop


Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = NewName




End Sub
 
Upvote 0
Thank you Comfy. That works well after one minor change is made. In the Do While Loop, the line "NewName = Name & "-" & i" must be placed above "Exists = Contains(Sheets, NewName)." Thanks for your help.
 
Upvote 0
No it doesn't.

If you move it above Exists = Contains(Sheets, NewName) you will not check to see if the first name exists.


 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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