change code to work correctly(increment numbers of new added sheet)

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
Hi

I need fixing this code
VBA Code:
Sub new_report()
    Dim a
  
   Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
    With ActiveSheet.Cells(1).CurrentRegion.Offset(1)
    ActiveSheet.Name = "TEST1" & Sheets.Count - 1
        a = .Value
        .ClearContents
        Cells(2, 1).Resize(UBound(a), 3) = Application.Index(a, Evaluate("row(1:" & UBound(a) & ")"), Array(1, 2, 6))
    End With
 
End Sub

the code add new sheet with copy specific data and the same formatting and incrementing numbers based on sheet number

location of sheet in in third sheet , for instance name the sheet third is TEST1 and based on the code will be TEST4 but what I want TEST2,TEST3,TEST4 consecutively

I need change this line
VBA Code:
ActiveSheet.Name = "TEST1" & Sheets.Count - 1
but I no know how
is there any way experts?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
See if this does what you need. It only works up to number 98 though.

VBA Code:
Sub new_report()
    Dim a
    Dim sht As Worksheet
    Dim shtName As String
    Dim NameSuffix As String

    Set sht = Worksheets(Sheets.Count)
    shtName = sht.Name
    
    NameSuffix = Mid(shtName, Len(shtName) - 1, 2)
    If IsNumeric(NameSuffix) Then
        NameSuffix = CInt(NameSuffix) + 1
        shtName = Left(shtName, Len(shtName) - Len(NameSuffix)) & NameSuffix
    ElseIf IsNumeric(Right(NameSuffix, 1)) Then
        NameSuffix = Right(NameSuffix, 1)
        NameSuffix = CInt(NameSuffix) + 1
        shtName = Left(shtName, Len(shtName) - Len(NameSuffix)) & NameSuffix
    Else
        NameSuffix = 1
        shtName = shtName & NameSuffix
    End If
    
    sht.Copy After:=Sheets(Sheets.Count)
    With ActiveSheet.Cells(1).CurrentRegion.Offset(1)
        ActiveSheet.Name = shtName
        a = .Value
        .ClearContents
        Cells(2, 1).Resize(UBound(a), 3) = Application.Index(a, Evaluate("row(1:" & UBound(a) & ")"), Array(1, 2, 6))
    End With
 
End Sub
 
Upvote 0
Solution
your updtaing works perfectly :)
It only works up to number 98 though.
is it rule in EXCEL?
just curiosity, can I change based on current month and word?
like this "REPORT MONTH OF " & " " & Format(Date, "mmm")
for instance REPORT MONTH OF DECEMBER
 
Upvote 0
is it rule in EXCEL?
If you are refering to my reference to 98 (should be 99), no that's not a rule. It is just that I have only catered for 2 digits so it maxes out at 99.
just curiosity, can I change based on current month and word?
like this "REPORT MONTH OF " & " " & Format(Date, "mmm")
for instance REPORT MONTH OF DECEMBER
If you use words you would need to very clear on what you want.
The line of code you have would always name it using the current month, since you are using Date and is not incrementing it based on the last sheet in the workbook.
Also "mmm" would give you Dec not DECEMBER. ("mmmm" would give you December)
If you want to use the previous sheet which is the subject of this thread, we would need to code for increasing the Month Name (for month no the existing code would work).
Also what do you want to do after December ? Just January ? You might already have the previous January in your workbook.
 
Upvote 0
based on last comment if you think you will create new code to achieve it just forget it . I said curiosity

I thought it could mod some lines ,sorry!!
Also what do you want to do after December ? Just January ? You might already have the previous January in your workbook.
in reality , I want showing message box "you should create new file" and don't add any new sheet . this should happen based on last sheet name REPORT OF MONTH DECEMBER

in other meaning , file should from JANUARY to DECEMBER but the month sheets start from third sheet because I have others sheets before it.
thanks again
 
Upvote 0
Give this a try if you want to increment the Month Name:
VBA Code:
Sub new_report_MthName()
    Dim a
    Dim sht As Worksheet
    Dim shtName As String
    Dim NameSuffix As String, NameMain As String
    Dim currMth As Date

    Set sht = Worksheets(Sheets.Count)
    shtName = sht.Name
    
    NameMain = Left(shtName, InStrRev(shtName, " "))
    NameSuffix = Right(shtName, Len(shtName) - InStrRev(shtName, " "))
    
    If UCase(NameSuffix) = "DECEMBER" Then
        MsgBox "you should create new file"
        Exit Sub
    End If
    
    On Error Resume Next
    currMth = DateValue("1-" & NameSuffix)
    If Err = 0 Then
        NameSuffix = UCase(MonthName((Month(currMth) + 1) Mod 12))
    Else
        NameSuffix = "JANUARY"
    End If
    On Error GoTo 0

    sht.Copy After:=Sheets(Sheets.Count)
    With ActiveSheet.Cells(1).CurrentRegion.Offset(1)
        ActiveSheet.Name = NameMain & NameSuffix
        a = .Value
        .ClearContents
        Cells(2, 1).Resize(UBound(a), 3) = Application.Index(a, Evaluate("row(1:" & UBound(a) & ")"), Array(1, 2, 6))
    End With
 
End Sub
 
Upvote 0
works except one thing when reach NOVEMBER month doesn't add DECEMBER month and shows error the is name is already existed , try another name
despite there is no sheet name DECEMBER before I run the code !
 
Upvote 0
I have logged out for the night.
When it errors out, in the immediate box paste the below and hit enter.
VBA Code:
? NameMain & NameSuffix
If the name makes sense and you believe it doesnt exist look for a hidden sheet with that name.
 
Upvote 0

Forum statistics

Threads
1,223,573
Messages
6,173,138
Members
452,501
Latest member
musallam

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