SOLVED

I want to rename all the sheet tabs of an excel

Brass Contributor
Hi
If there are sheet 1, sheet 2, sheet3 , sheet4 and sheet5 and i have deleted the sheet 4 tab...now the sheets in excel would be sheet1, sheet2, sheet3 and sheet5..( please observe there would be no sheet4 as i have deleted)

I have a VBA code and used to rename (renamed as 'Raj') all the sheets at one go but the result i am getting is Raj1,Raj2,Raj3,Raj4 however, i want the result as Raj1,Raj2,Raj3,Raj5.

How to do this?
10 Replies

@Raj_123  without the VBA code we can't tell you exactly what lines to change but in the VBA code there must be something like a loop something like For i = 1 to Activeworkbook.sheets.count

and then an assignment like

   Activeworkbook.sheet(i)="Raj"&i

you need to instead do something like

   temp=mid(Activeworkbook.sheet(i).name,6)

   Activeworkbook.sheet(i)="Raj"&temp

 

 

May i please have entire VBA code that you are refering to? Instead of Raj, i want macro need to ask me a name for all the time

@Raj_123  Here is a routine that will do what you ask.  It needs to be added to a module in the VBA and will ONLY properly work to rename from the original sheet names of Sheet1, Sheet2, ... and it will maintain the original sheet numbers.  But if you rename everything Raj1, Raj2, ... and change your mind and want RAJA instead, this macro would need to be adjusted.

Sub renameSheets()
    shtName = InputBox("Enter the new name to use for each sheet instead of 'sheet'", "New Sheet Name")
    If shtName = "" Then
        Exit Sub
    End If
    For i = 1 To ActiveWorkbook.Sheets.Count
        ActiveWorkbook.Sheets(i).Name = shtName & Mid(ActiveWorkbook.Sheets(i).Name, 6)
    Next i
End Sub

 

Yes i have to change the sheet names multiple times may i request you to adjust the macro like that.. also, may i please know the purpose of number 6? I actually have n number of sheet tabs..

@Raj_123  that 6 was because there are 5 characters in "Sheet" so it added the characters 6 and after.

Here is a different version that lets you pick how many characters to remove before adding the new default name.  

Sub renameSheets()
    chrRemove = InputBox("Enter the number of characters to remove from the existing sheet names. E.g. if you want to remove the default name: 'sheet' then enter 5", "Remove X characters")
    If chrRemove = "" Then
        Exit Sub
    End If
    shtName = InputBox("Enter the new name to insert on each sheet name", "New Sheet Name")
    If shtName = "" Then
        Exit Sub
    End If
    On Error GoTo badName
    For i = 1 To ActiveWorkbook.Sheets.Count
        ActiveWorkbook.Sheets(i).Name = shtName & Mid(ActiveWorkbook.Sheets(i).Name, chrRemove + 1)
    Next i
    Exit Sub
badName:
    ActiveWorkbook.Sheets(i).Name = shtName & Mid(ActiveWorkbook.Sheets(i).Name, chrRemove + 1) & i
    Err.Clear
    On Error GoTo badName
    Resume Next
    
End Sub
best response confirmed by Raj_123 (Brass Contributor)
Solution
Thank you friend.

I have used the above code and it actaully worked but issue arised is as per our procedures we renumber and rename the sheets using a macro button then it would look like this 1-Rob1, 2-Rob2,...10-Rob10,....100-Rob100 now i have inputtted the character as 5 for the old and renamed the result came out missed from10-Rob10 (bRaj10) bcoz it has 6 characters the and the 100-Rob10 has 7chartacters (obRaj100).

I have used the below code but the lapse is it is changing the serial number but it ibrilliantly changes the sheet name just by asking new code....can we adjust the below code accordingly.



Sub ChangeWorkSheetName()

Dim Rng As Range

Dim WorkRng As Range

On Error Resume Next

newName = Application.InputBox(“Name”, xTitleId, “”, Type:=2)

For i = 1 To Application.Sheets.Count

Application.Sheets(i).Name = newName & i

Next

End Sub

@Raj_123  I'm sorry, no.  I am completely lost what you want and honestly feel like you are taking advantage of me and this system to do you contract work.  My understanding of this message board and why I VOLUNTER here is to help people struggling with Excel; not to do the work for them.

Your last post shows VBA code that looks exactly like what I expected from your original post to which I gave advice how to change, sight unseen.  I then wrote code for you to use based on what you said you wanted/needed.  Then you changed what your needs were, and I wrote modified code.  Now you have additional changes and needs.  I am sorry but I have to cut this off as I said, I volunteer to HELP not to DO your project, and I feel I have bent way over that line already.

Best of luck.

Hi @mtarler,

I did understand the pain u went on..but friend, i have learnt a lot from your recent solutions...i bow to you, and you are very very helpful and super talented....after running the macro code i am testing the combinations with the other macros that are already builtin that is why new queries arised for me...

As i am pretty new i could not incorporate the additional loop that you have given in your first reply that is why i asked you to give me the entire code.

I am not doing any project and i am learning and trying the new things.
I remember your support lifelong and i agree that you have helped me more than i expected...

Kudos to you :)

All the best.

@Raj_123  I am very happy to hear you have learned a lot.  I hope your project goes well.  If you have any other problems or errors you can't figure out we are always here. 

As a tip, it works best when you share the worksheet and VBA you have and what the problem is in the most complete terms as you can.  Then we can help fix the problem and sometimes can offer suggestions for better ways to do it.

all the best.

Thank you my bestie...
1 best response

Accepted Solutions
best response confirmed by Raj_123 (Brass Contributor)
Solution
Thank you friend.

I have used the above code and it actaully worked but issue arised is as per our procedures we renumber and rename the sheets using a macro button then it would look like this 1-Rob1, 2-Rob2,...10-Rob10,....100-Rob100 now i have inputtted the character as 5 for the old and renamed the result came out missed from10-Rob10 (bRaj10) bcoz it has 6 characters the and the 100-Rob10 has 7chartacters (obRaj100).

I have used the below code but the lapse is it is changing the serial number but it ibrilliantly changes the sheet name just by asking new code....can we adjust the below code accordingly.



Sub ChangeWorkSheetName()

Dim Rng As Range

Dim WorkRng As Range

On Error Resume Next

newName = Application.InputBox(“Name”, xTitleId, “”, Type:=2)

For i = 1 To Application.Sheets.Count

Application.Sheets(i).Name = newName & i

Next

End Sub

View solution in original post