Delete worksheets based on a list

aerobharath15

New Member
Joined
Mar 14, 2018
Messages
11
hi All,

With the help of a member here, i managed to create multiple WORKSHEETS based on a list using the code below. but now i want a macro to delete all the created worksheets without deletion prompt window. Please someone can help urgently.

Sub makeSheets()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Template")
Set sh2 = Sheets("Points")
For Each c In sh2.Range("B5", sh2.Cells(Rows.Count, 2).End(xlUp))
sh1.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value: ActiveSheet.Range("D5") = c.Value
Next
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this:
Code:
Sub deleteSheets()

    Dim sh2 As Worksheet, c As Range

    Set sh2 = Sheets("Points")

    Application.DisplayAlerts = False
    On Error Resume Next
    For Each c In sh2.Range("B5", sh2.Cells(Rows.Count, 2).End(xlUp))
        Sheets(c.Value).Delete
    Next c
    On Error GoTo 0
    
    Application.DisplayAlerts = True

End Sub
 
Last edited:
Upvote 0
I think this last display alerts should be set to true. is that not correct?

Application.DisplayAlerts = False

 
Upvote 0
I think this last display alerts should be set to true. is that not correct?

Application.DisplayAlerts = False
It is.;)
(I fixed it just before you posted that).
 
Upvote 0
Many thanks Joe.

Sorry. I made a slight change to worksheet. B5 cell must be replaced with B15. I changed it a bit and when i run it only starts to delete the worksheets one by one from B17 and not from B15 where the first worksheet on the list begins.

Also, is it not possible to delete all the worksheets from the list at once instead of one by one?

Many thanks again :)
 
Upvote 0
this is the updated code.

Sub GENERATE()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("PCM Template")
Set sh2 = Sheets("PCM SUMMARY")
For Each c In sh2.Range("B15", sh2.Cells(Rows.Count, 2).End(xlUp))
sh1.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value: ActiveSheet.Range("B28") = c.Value
Next
End Sub
 
Upvote 0
How about
Code:
Sub DelShts()
   Dim Ary As Variant
   Dim i As Long
   Dim Cl As Range
   
   ReDim Ary(1 To Worksheets.Count)
   With Sheets("PCM Summary")
      For Each Cl In .Range("B15", .Range("B" & Rows.Count).End(xlUp))
         If Evaluate("isref('" & Cl.Value & "'!a1)") Then
            i = i + 1
            Ary(i) = Cl.Value
         End If
      Next Cl
   End With
   ReDim Preserve Ary(1 To i)
   Application.DisplayAlerts = False
   Sheets(Ary).Delete
   Application.DisplayAlerts = True
End Sub
 
Upvote 0
How about
Code:
Sub DelShts()
   Dim Ary As Variant
   Dim i As Long
   Dim Cl As Range
   
   ReDim Ary(1 To Worksheets.Count)
   With Sheets("PCM Summary")
      For Each Cl In .Range("B15", .Range("B" & Rows.Count).End(xlUp))
         If Evaluate("isref('" & Cl.Value & "'!a1)") Then
            i = i + 1
            Ary(i) = Cl.Value
         End If
      Next Cl
   End With
   ReDim Preserve Ary(1 To i)
   Application.DisplayAlerts = False
   Sheets(Ary).Delete
   Application.DisplayAlerts = True
End Sub

hi, thanks but it shows an error SUBSCRIPT OUT OF RANGE :(
 
Upvote 0
Which line is highlighted?
 
Upvote 0
Also, is it not possible to delete all the worksheets from the list at once instead of one by one?
If it is all being done automatically by a macro, what difference does it really make?

Are there any other sheets other than "Points" and "Template" that you want to keep?
If not, you can just delete all sheets not named those, and there is no need to loop through a list somewhere on your sheet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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