SOLVED

VBA to rename sheets

%3CLINGO-SUB%20id%3D%22lingo-sub-1633051%22%20slang%3D%22en-US%22%3EVBA%20to%20rename%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1633051%22%20slang%3D%22en-US%22%3ERenaming%20the%20only%20the%20sheet%3CBR%20%2F%3EThere%20are%20many%20sheet%20tabs%20in%20excel%20for%20say%20sheet1%20sheet2%20sheet3%20sheet%204%2C%20sheet5%20and%20so%20on...however%20i%20have%20deleted%20sheet2%20..the%20remaining%20will%20be%20sheet1%2C%20sheet3%2C%20sheet4%2Csheet5%20and%20so%20on..%3CBR%20%2F%3E%3CBR%20%2F%3EAfter%20renaming%20it%20should%20look%20like%20Rob1%2CRob3%2CRob4%2CRob5%20and%20so%20on...%20But%20the%20VBA%20code%20that%20i%20have%20is%20giving%20the%20answer%20as%20Rob1%2C%20Rob2%2CRob3%2CRob4%20.%3CBR%20%2F%3EI%20don't%20want%20the%20serial%20number%20to%20get%20disturbed%3CBR%20%2F%3E%3CBR%20%2F%3EI%20want%20a%20VBA%20code%20for%20this%20please%20and%20Rob%20in%20the%20above%20name%20is%20taken%20just%20as%20an%20example%20i%20want%20a%20new%20name%20should%20be%20asked%20by%20macro%20when%20ever%20i%20run%20it.%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20help%20me%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1633051%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1633623%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20to%20rename%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1633623%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F780831%22%20target%3D%22_blank%22%3E%40Raj_123%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESee%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel%2Frenaming-the-only-the-sheet%2Ftd-p%2F1632361%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3ERenaming%20the%20only%20the%20sheet%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1633760%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20to%20rename%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1633760%22%20slang%3D%22en-US%22%3EYes%20i%20have%20tried%20the%20second%20one%20having%20the%20option%20of%20old%20and%20new...it%20is%20creating%20an%20issue...for%20say%2C%20the%20old%20sheet%20name%20now%20is%20ROB1%20i%20have%20to%20type%20just%20Rob%20when%20ever%20it%20promted%20for%20old...if%20i%20type%20ROB1%20everything%20will%20go%20for%20a%20toss...i%20verymuch%20underatood%20what%20to%20keyin...but%20it%20will%20not%20be%20a%20user-friendly%20to%20the%20people%20who%20does%20not%20know%20VBA%20they%20just...you%20have%20helped%20me%20a%20lot.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20used%20the%20below%20VBA%20code%3CBR%20%2F%3Ei%20can%20change%20the%20names%20n%20number%20if%20times%20%22%20however%20it%20is%20changing%20the%20sheet%20numbers%22%20that%20is%20all%20my%20concern.%20After%20deleting%20the%20sheet4%20and%20i%20rename%20the%20answer%20to%20be%20is%20Rob1%2C%20Rob2%2CRob4%2CRob5....but%20it%20is%20showing%20the%20result%20as%20Rob1%2CRob2%2CRob3%2CRob4...%3CBR%20%2F%3E%3CBR%20%2F%3EI%20trust%20you%20can%20modify%20the%20and%20you%20are%20super%20talented%3CBR%20%2F%3E%3CBR%20%2F%3ESub%20ChangeWorkSheetName()%3CBR%20%2F%3E%3CBR%20%2F%3EDim%20Rng%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3EDim%20WorkRng%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3EOn%20Error%20Resume%20Next%3CBR%20%2F%3E%3CBR%20%2F%3EnewName%20%3D%20Application.InputBox(%E2%80%9CName%E2%80%9D%2C%20xTitleId%2C%20%E2%80%9C%E2%80%9D%2C%20Type%3A%3D2)%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20i%20%3D%201%20To%20Application.Sheets.Count%3CBR%20%2F%3E%3CBR%20%2F%3EApplication.Sheets(i).Name%20%3D%20newName%20%26amp%3B%20i%3CBR%20%2F%3E%3CBR%20%2F%3ENext%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1635261%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20to%20rename%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1635261%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F780831%22%20target%3D%22_blank%22%3E%40Raj_123%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELike%20this%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20RenameSheets()%0A%20%20%20%20Dim%20OldName%20As%20String%0A%20%20%20%20Dim%20NewName%20As%20String%0A%20%20%20%20Dim%20wsh%20As%20Worksheet%0A%20%20%20%20Dim%20p%20As%20Long%0A%20%20%20%20NewName%20%3D%20InputBox(%22Enter%20the%20new%20name%22)%0A%20%20%20%20If%20NewName%20%3D%20%22%22%20Then%0A%20%20%20%20%20%20%20%20Beep%0A%20%20%20%20%20%20%20%20Exit%20Sub%0A%20%20%20%20End%20If%0A%20%20%20%20For%20Each%20wsh%20In%20Worksheets%0A%20%20%20%20%20%20%20%20OldName%20%3D%20wsh.Name%0A%20%20%20%20%20%20%20%20For%20p%20%3D%20Len(OldName)%20To%201%20Step%20-1%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20Not%20IsNumeric(Mid(OldName%2C%20p%2C%201))%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Exit%20For%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20p%0A%20%20%20%20%20%20%20%20wsh.Name%20%3D%20NewName%20%26amp%3B%20Mid(OldName%2C%20p%20%2B%201)%0A%20%20%20%20Next%20wsh%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1635420%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20to%20rename%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1635420%22%20slang%3D%22en-US%22%3EHello%20friend%2C%3CBR%20%2F%3E%3CBR%20%2F%3EIt%20is%20working%20now...will%20let%20you%20know%20if%20i%20find%20any%20difficulties....i%20am%20actaully%20trying%20now%20with%20around%20250%20pages%20adding%20the%20pages%20in%20between%20and%20then%20renaming%20got%20an%20vba%20404%20error%20but%20i%20have%20closed%20the%20worksheet%20and%20reopened%20it%20and%20worked%20for%20now....not%20able%20to%20trace%20why%20i%20got%20an%20error%20intially.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1636062%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20to%20rename%20sheets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1636062%22%20slang%3D%22en-US%22%3EHere%20comes%20the%20issue%20again!!!%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20deleted%20the%20sheet2%2Csheet4%20and%20sheet6%20among%20the%2020%20shets%20i%20have%20ahand%20renamed%20the%20sheets%20to%20Rob%20with%20the%20above%20code%20i%20got%20the%20answer%20as%20Rob1%2CRob3%2CRob5%2CRob7%20and%20so%20on...%3CBR%20%2F%3E%3CBR%20%2F%3ENow%2C%20i%20have%20added%20the%2010%20new%20sheets%20the%20sheets%20are%20automatically%20started%20from%20sheet1%2CSheet2%2C%20Sheet%203%2C%20sheet4%2C%20and%20so%20on...to%20sheet10%3CBR%20%2F%3E%3CBR%20%2F%3EActually%20the%20newly%20added%20sheets%20should%20start%20from%20sheet21%20and%20end%20at%20sheet30.%20bcoz%20of%20this%20if%20i%20rename%20the%20sheets%20again%20from%20Rob%20to%20Martin%20there%20is%20a%20error%20occuring%20to%20rename%20sheets%20because%20of%20clash%20between%20Rob1%2C%20Rob3%20and%20soon%20as%20it%20is%20getting%20conflicted%20with%20sheet1to%20sheet10%20which%20are%20newly%20created...feeling%20sad%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20help%20me%3C%2FLINGO-BODY%3E
Highlighted
Contributor
Renaming the only the sheet
There are many sheet tabs in excel for say sheet1 sheet2 sheet3 sheet 4, sheet5 and so on...however i have deleted sheet2 ..the remaining will be sheet1, sheet3, sheet4,sheet5 and so on..

After renaming it should look like Rob1,Rob3,Rob4,Rob5 and so on... But the VBA code that i have is giving the answer as Rob1, Rob2,Rob3,Rob4 .
I don't want the serial number to get disturbed

I want a VBA code for this please and Rob in the above name is taken just as an example i want a new name should be asked by macro when ever i run it.

Please help me
7 Replies
Highlighted
Highlighted
Yes i have tried the second one having the option of old and new...it is creating an issue...for say, the old sheet name now is ROB1 i have to type just Rob when ever it promted for old...if i type ROB1 everything will go for a toss...i verymuch underatood what to keyin...but it will not be a user-friendly to the people who does not know VBA they just...you have helped me a lot.

I have used the below VBA code
i can change the names n number if times " however it is changing the sheet numbers" that is all my concern. After deleting the sheet4 and i rename the answer to be is Rob1, Rob2,Rob4,Rob5....but it is showing the result as Rob1,Rob2,Rob3,Rob4...

I trust you can modify the and you are super talented

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
Highlighted
Best Response confirmed by Raj_123 (Contributor)
Solution

@Raj_123 

Like this:

Sub RenameSheets()
    Dim OldName As String
    Dim NewName As String
    Dim wsh As Worksheet
    Dim p As Long
    NewName = InputBox("Enter the new name")
    If NewName = "" Then
        Beep
        Exit Sub
    End If
    For Each wsh In Worksheets
        OldName = wsh.Name
        For p = Len(OldName) To 1 Step -1
            If Not IsNumeric(Mid(OldName, p, 1)) Then
                Exit For
            End If
        Next p
        wsh.Name = NewName & Mid(OldName, p + 1)
    Next wsh
End Sub
Highlighted
Hello friend,

It is working now...will let you know if i find any difficulties....i am actaully trying now with around 250 pages adding the pages in between and then renaming got an vba 404 error but i have closed the worksheet and reopened it and worked for now....not able to trace why i got an error intially.
Highlighted
Here comes the issue again!!!

I have deleted the sheet2,sheet4 and sheet6 among the 20 shets i have ahand renamed the sheets to Rob with the above code i got the answer as Rob1,Rob3,Rob5,Rob7 and so on...

Now, i have added the 10 new sheets the sheets are automatically started from sheet1,Sheet2, Sheet 3, sheet4, and so on...to sheet10

Actually the newly added sheets should start from sheet21 and end at sheet30. bcoz of this if i rename the sheets again from Rob to Martin there is a error occuring to rename sheets because of clash between Rob1, Rob3 and soon as it is getting conflicted with sheet1to sheet10 which are newly created...feeling sad

Please help me
Highlighted

@Raj_123 

That's not the way Excel works.

Highlighted
@Hans Vogelaar,

The VBA code which you have given is working fine...the errors coming are because of the combinations of other VBA codes...

Kudos to you..thanks for your brilliant support. Happy to have in this community.