1004 Error. That name is taken

qamex

New Member
Joined
Apr 16, 2018
Messages
5
Hi team,
Code below. I
t is meant to run through the work book and format the Tab name with the name in D6. It has been working fine for a couple months but now it refuses to budge from this error on this line.
There are on 10 names that appear repeatedly.
Rich (BB code):
Sub Tab_name()
Dim ws As Worksheet, wsName$, wsCount%, dict   ' $ is short for " As String" and % for " As Integer"
    Set dict = CreateObject("Scripting.Dictionary")
    For Each ws In Sheets
        wsName = ws.Range("D6").Value
        wsCount = IIf(dict.Exists(wsName), dict(wsName) + 1, 1)
        dict(wsName) = wsCount
        If wsCount = 1 Then
            ws.Name = wsName
        Else
            If wsCount = 2 Then Sheets(wsName).Name = wsName & 1 ' optional to rename the previous Ohio sheet to Ohio1
            ws.Name = wsName & wsCount
        End If
    Next ws
End Sub
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You will get that error if a sheet to the right of the one the code is working on already has the name of the D6 value in your current sheet. For example if the left hand sheet has "Ohio" in D6 but the second sheet from the left is called "Ohio" then you will be trying to rename the first sheet the same as the second - not allowed.
You need to go right through all the sheets first and put their names in to the dictionary and then work through again dealing with what to do about re-naming.
 
Last edited:
Upvote 0
Thanks mate. That advice helped get it working. If only you could eliminate the human element that creates most of the error.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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