How to Create New sheet with suffix and How to Delete them with Prefix

Bijan2048

New Member
Joined
Apr 16, 2023
Messages
9
Office Version
  1. 2007
Platform
  1. Windows
Hi Masters
I need a VBA code to create new sheet with Suffix number,
for example if a sheet named REPORT1 is exist , each time VBA code run, new sheet create another one with name REPORT2 and 3,4,......
in addition I need a VBA code to delete any sheets that name begins with REPORT
Thanks in advanced
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi Masters
I need a VBA code to create new sheet with Suffix number,
for example if a sheet named REPORT1 is exist , each time VBA code run, new sheet create another one with name REPORT2 and 3,4,......
in addition I need a VBA code to delete any sheets that name begins with REPORT
Thanks in advanced
One procedure to create sheets and one to delete them.

Test on a NEW workbook.

Call with :

Call subCreateNewSheet("Report")

Call subDeleteSheets("Report")

VBA Code:
Public Sub subCreateNewSheet(strPrefix As String)
Dim Ws As Worksheet
Dim intMax As Integer

  For Each Ws In ActiveWorkbook.Worksheets
  
    If LCase(Ws.Name) Like LCase(strPrefix) & "*" Then
                      
      If Val(Replace(Ws.Name, strPrefix, "", 1)) > intMax Then
      
        intMax = Val(Replace(Ws.Name, strPrefix, "", 1))
      
      End If
             
    End If
  
  Next Ws
  
  Worksheets.Add after:=Sheets(Sheets.Count)
  
  ActiveSheet.Name = strPrefix & intMax + 1

End Sub

Public Sub subDeleteSheets(strPrefix As String)
Dim Ws As Worksheet
Dim intMax As Integer

  Application.DisplayAlerts = False
  
  For Each Ws In ActiveWorkbook.Worksheets
  
    If LCase(Ws.Name) Like LCase(strPrefix) & "*" Then
                      
      Ws.Delete
                  
    End If
  
  Next Ws
  
  Application.DisplayAlerts = True
  
End Sub
 
Upvote 0
Solution
Here a couple of macros to consider

To create:
VBA Code:
Sub Create_Sheets()
  Dim sh As Worksheet
  Dim sName As String
  Dim nMax As Long, n As Long
  
  Application.ScreenUpdating = False
  sName = "REPORT"
  
  For Each sh In Sheets
    If UCase(Left(sh.Name, Len(sName))) = sName Then
      n = Replace(sh.Name, sName, "") + 1
      If n > nMax Then nMax = n
    End If
  Next
  
  If n = 0 Then nMax = 1
  Sheets.Add(, Sheets(Sheets.Count)).Name = sName & nMax
  
  Application.ScreenUpdating = True
End Sub

To delete:
VBA Code:
Sub Delete_Sheets()
  Dim sh As Worksheet
  Dim sName As String
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  sName = "REPORT"
  
  For Each sh In Sheets
    If UCase(Left(sh.Name, Len(sName))) = sName Then
      sh.Delete
    End If
  Next
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
Here a couple of macros to consider

To create:
VBA Code:
Sub Create_Sheets()
  Dim sh As Worksheet
  Dim sName As String
  Dim nMax As Long, n As Long
 
  Application.ScreenUpdating = False
  sName = "REPORT"
 
  For Each sh In Sheets
    If UCase(Left(sh.Name, Len(sName))) = sName Then
      n = Replace(sh.Name, sName, "") + 1
      If n > nMax Then nMax = n
    End If
  Next
 
  If n = 0 Then nMax = 1
  Sheets.Add(, Sheets(Sheets.Count)).Name = sName & nMax
 
  Application.ScreenUpdating = True
End Sub

To delete:
VBA Code:
Sub Delete_Sheets()
  Dim sh As Worksheet
  Dim sName As String
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  sName = "REPORT"
 
  For Each sh In Sheets
    If UCase(Left(sh.Name, Len(sName))) = sName Then
      sh.Delete
    End If
  Next
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub
Thank you
The codes work Great.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

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