Is it possible to rename the worksheet with vba code?

dener123

New Member
Joined
Jul 10, 2024
Messages
18
Office Version
  1. 2021
How can I write the scenario for the following case?

If sheet name "Jan" exists, rename this sheet as "Feb".
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Something like this.
VBA Code:
Sub Rename()
    Dim ws As Worksheet
    Dim sheetExists As Boolean
    sheetExists = False
   
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = "Jan" Then
            sheetExists = True
            ws.Name = "Feb"
            Exit For
        End If
    Next ws
   
    If Not sheetExists Then
        MsgBox "Sheet 'Jan' does not exist."
    Else
        MsgBox "Sheet 'Jan' has been renamed to 'Feb'."
    End If
End Sub
 
Upvote 0
There a few different ways. Here is one.

VBA Code:
    Dim X As Variant
    
    On Error Resume Next
    X = ActiveWorkbook.Worksheets("Jan").Name
    On Error GoTo 0
    
    If IsEmpty(X) Then
        ActiveSheet.Name = "Jan"
    Else
        ActiveSheet.Name = "Feb"
    End If
 
Upvote 0
Another idea maybe if you want to change tab name each month

VBA Code:
Sub Rename()
    Dim LastMonth As String, ThisMonth As String
    
    LastMonth = MonthName(Month(Date) - 1, True)
    ThisMonth = MonthName(Month(Date), True)
    
    If Evaluate("ISREF('" & LastMonth & "'!A1)") Then ThisWorkbook.Worksheets(LastMonth).Name = ThisMonth
   
End Sub

Dave
 
Upvote 0
The proverbial "Roads to Rome". Many of them.
Code:
Sub Change_Sheet_Name()
Dim sh As Worksheet
    For Each sh In ThisWorkbook.Worksheets
            If [ISREF(Jan!A1)] Then Sheets("Jan").Name = "Feb": Exit For
    Next sh
End Sub
 
Upvote 0
Try this:
Excel Formula:
Private Sub CommandButton1_Click()
Dim i As Long
For i = 1 To Sheets.Count
Select Case Sheets(i).Name
Case "Jan": Sheets(i).Name = "Feb"
End Select
Next
End Sub
 
Upvote 0
Hey guys,
I really appreciate all the replies. I think I wasn't clear enough with the scenario.

Let's say I have multiple worksheets named Jan, Feb, Mar, Apr, and May.
I need a VBA code that creates a new worksheet and rename itself as the month after if the prior month sheets exist.

For example;
If worksheet named Jan already exist, rename this newly created sheet as Feb.
If worksheet named Feb already exist, rename this newly created sheet as Mar.
and so on until Dec.
 
Upvote 0
There a few different ways. Here is one.

VBA Code:
    Dim X As Variant
   
    On Error Resume Next
    X = ActiveWorkbook.Worksheets("Jan").Name
    On Error GoTo 0
   
    If IsEmpty(X) Then
        ActiveSheet.Name = "Jan"
    Else
        ActiveSheet.Name = "Feb"
    End If
Hi,
This code works in the scenario I've wanted!
But could you make this longer?

What I meant by that is;
I need this all the way to Dec.

For example;
If worksheet named Jan already exist, rename this newly created sheet as Feb.
If worksheet named Feb already exist, rename this newly created sheet as Mar.
and so on all the way to Dec.
 
Upvote 0
From Post #1: "If sheet name "Jan" exists, rename this sheet as "Feb"."
Now you say that it could be any month of the year.
 
Upvote 0
So how many sheets are in this workbook exactly?
Do you want to rename ALL the prior worksheets?
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
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