Rename Worksheets with VBA

Lasa1

New Member
Joined
Mar 31, 2020
Messages
18
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have 2 questions on renaming worksheets:

1) f I tried to name the worksheet with the name of each month from January to December with code below, I got the error "Compiled error.Variable not defined". It seems that code mmmm is not read.

VBA Code:
Option Explicit

Sub RenameSheets()
' copy Sheet 1 12 times, one per month
Dim x As Integer
For x = 1 To 12

Worksheets(1).Copy After:=Worksheets(x)
Worksheets(1 + x).Name = Format(DateSerial(1, x, 1), [B]mmmm[/B])

Next x
Worksheets(1).Delete

End Sub


2) If I tried to name the worksheet with numbers from 1 to 12 with code below, all sheets are correctly renamed except the 10th one which is named "137165" instead of "10".
Any idea?
VBA Code:
Option Explicit

Sub RenameSheets()
' copy Sheet 1 12 times, one per month
Dim x As Integer
For x = 1 To 12

Worksheets(1).Copy After:=Worksheets(x)
Worksheets(1 + x).Name = Format(DateSerial(1, x, 1), x)

Next x
Worksheets(1).Delete

End Sub

Thanks!
 

Attachments

  • 1585666225005.png
    1585666225005.png
    67.1 KB · Views: 7

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You are missing the quotes in the format
VBA Code:
Worksheets(1 + x).Name = Format(DateSerial(1, x, 1), "mmmm")
 
Upvote 0
not sure about the second code, but the first one you just need to quote the mmmm

VBA Code:
Option Explicit

Sub RenameSheets()
' copy Sheet 1 12 times, one per month
Dim x As Integer
For x = 1 To 12

Worksheets(1).Copy After:=Worksheets(x)
Worksheets(1 + x).Name = Format(DateSerial(1, x, 1), "mmmm")

Next x
Worksheets(1).Delete

End Sub
 
Upvote 0
If you just want the number rather than the month use
VBA Code:
Worksheets(1 + x).Name = x
 
Upvote 0
Wow, that was quick , thanks both!Question # 2 s driving me crazy.. cannot solve it..
 
Upvote 0
If you just want the number rather than the month use
VBA Code:
Worksheets(1 + x).Name = x
right on! and I would just turn off the DisplayAlerts in case he get the excel message about permanent deleting a sheet...

VBA Code:
Option Explicit

Sub RenameSheets()
' copy Sheet 1 12 times, one per month
    Dim x As Integer
        For x = 1 To 12

    Worksheets(1).Copy After:=Worksheets(x)
    Worksheets(1 + x).Name = x

        Next x
        
    Application.DisplayAlerts = False
    Worksheets(1).Delete
    Application.DisplayAlerts = True

End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
For referrence, as you want the month name you can use
VBA Code:
   Worksheets(1 + x).Name = MonthName(x)
or for the abbreviated name
VBA Code:
   Worksheets(1 + x).Name = MonthName(x, True)
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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