VBA Macro to change name of worksheet

dinkss

Board Regular
Joined
Aug 25, 2020
Messages
129
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

Is there any way of creating a macro that will create new worksheet inside of workbook with weeks numbering?

I have worksheet named WEEK-21, and want to have a macro that when used (I will assign macro button) will create a new worksheet with name WEEK-22 and so on. So after pressing assigned button I want to be able to create new worksheets with weeks numbering following the name of the first workbook, so WEEK-22, WEEK-23, WEEK-24 etc etc.

Any help will be greatly appreciated.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
try this code:
VBA Code:
Sub Addsheet()
nshts = Worksheets.Count
maxwk = 0
For i = 1 To nshts
 wn = Worksheets(i).Name
  If Left(wn, 4) = "WEEK" Then
    dsh = InStr(wn, "-")
    nm = Mid(wn, dsh + 1)
    If nm > maxwk Then
     maxwk = nm
    End If
  End If
Next i
If maxwk > 0 Then
 Worksheets.Add(After:=Sheets("WEEK-" & maxwk)).Name = "WEEK-" & maxwk + 1
End If

End Sub
 
Upvote 0
try this code:
VBA Code:
Sub Addsheet()
nshts = Worksheets.Count
maxwk = 0
For i = 1 To nshts
 wn = Worksheets(i).Name
  If Left(wn, 4) = "WEEK" Then
    dsh = InStr(wn, "-")
    nm = Mid(wn, dsh + 1)
    If nm > maxwk Then
     maxwk = nm
    End If
  End If
Next i
If maxwk > 0 Then
 Worksheets.Add(After:=Sheets("WEEK-" & maxwk)).Name = "WEEK-" & maxwk + 1
End If

End Sub
I will try it and will let you know! Thank you!
 
Upvote 0
I will try it and will let you know! Thank you!
try this code:
VBA Code:
Sub Addsheet()
nshts = Worksheets.Count
maxwk = 0
For i = 1 To nshts
 wn = Worksheets(i).Name
  If Left(wn, 4) = "WEEK" Then
    dsh = InStr(wn, "-")
    nm = Mid(wn, dsh + 1)
    If nm > maxwk Then
     maxwk = nm
    End If
  End If
Next i
If maxwk > 0 Then
 Worksheets.Add(After:=Sheets("WEEK-" & maxwk)).Name = "WEEK-" & maxwk + 1
End If

End Sub
Hi Buddy,

Your macro works brilliant but can you modify it to copy current sheet instead of creating blank one?
It changes the name perfectly but I would likle to copy current sheet and change name automatically.

Is it possible?

I was using this macro for long time - it is copying previous sheet and give you a message box with question how many copies do you want but it doesn't change name like your one.

Dim I As Long
Dim xNumber As Integer
Dim xName As String
Dim xActiveSheet As Worksheet
On Error Resume Next
Application.ScreenUpdating = False
Set xActiveSheet = ActiveSheet
xNumber = InputBox("Enter number of times to copy the current sheet")
For I = 1 To xNumber
xName = ActiveSheet.Name
xActiveSheet.Copy After:=ActiveWorkbook.Sheets(xName)
ActiveSheet.Name = "WEEK-" & I
Next
xActiveSheet.Activate
Application.ScreenUpdating = True
End Sub


Can you help me please?

Thank so much for your help.
 
Upvote 0
try this:
VBA Code:
Sub Addsheet()
nshts = Worksheets.Count
maxwk = 0
For i = 1 To nshts
 wn = Worksheets(i).Name
  If Left(wn, 4) = "WEEK" Then
    dsh = InStr(wn, "-")
    nm = Mid(wn, dsh + 1)
    If nm > maxwk Then
     maxwk = nm
    End If
  End If
Next i
If maxwk > 0 Then
 ActiveSheet.Copy After:=Sheets("WEEK-" & maxwk)
 ActiveSheet.Name = "WEEK-" & maxwk + 1
End If
 
Upvote 0
try this:
VBA Code:
Sub Addsheet()
nshts = Worksheets.Count
maxwk = 0
For i = 1 To nshts
 wn = Worksheets(i).Name
  If Left(wn, 4) = "WEEK" Then
    dsh = InStr(wn, "-")
    nm = Mid(wn, dsh + 1)
    If nm > maxwk Then
     maxwk = nm
    End If
  End If
Next i
If maxwk > 0 Then
 ActiveSheet.Copy After:=Sheets("WEEK-" & maxwk)
 ActiveSheet.Name = "WEEK-" & maxwk + 1
End If
This works perfect but it doesn't give me a popup box with question of how many copies do I want to create. Can you add something like that?
InputBox("Enter number of times to copy the current sheet")

thank you
 
Upvote 0
You didn't ask for that in the original question, this is what is known as "Requirements Creep" which in the real world of software developement can cost you a lot!!!
The easy way to do this is to add the subroutine :
VBA Code:
Sub loopaddsht()
xNumber = InputBox("Enter number of times to copy the current sheet")
For I = 1 To xNumber
Call Addsheet
Next I
End Sub
And run this one from your button
 
Upvote 0
You didn't ask for that in the original question, this is what is known as "Requirements Creep" which in the real world of software developement can cost you a lot!!!
The easy way to do this is to add the subroutine :
VBA Code:
Sub loopaddsht()
xNumber = InputBox("Enter number of times to copy the current sheet")
For I = 1 To xNumber
Call Addsheet
Next I
End Sub
And run this one from your button
Oh, sorry! I'm only a newbie. Have no clue about it. Very sorry. I will try this tomorrow and will let you know. Thanks
 
Upvote 0
Oh, sorry! I'm only a newbie. Have no clue about it. Very sorry. I will try this tomorrow and will let you know. Thanks
Hi buddy,

you are genius! All is working perfectly!!! WOW! I'm really greatfull for your help!
 
Upvote 0
Could you be so kind and help me with another task?

I'm looking to copy data - export data from multiple workbooks in different catalogues, but only from the range of A286:F311. Is it possible to do it?
I have 6 folders with 1 workbook in each folder. I want to have a folder SUMMARY with workbook SUMMARY that would copy all data from the above range from each workbook.

Is it possible to do that? Let say if I open Summary workbook I want to have data shown side by side to see full output from production. Hopefuly I have explained this right.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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