Delete sheet if found.

pavin

Active Member
Joined
Jan 20, 2009
Messages
308
Hi all,

I'm using the following code to delete a sheet if found. Its throwing an error, when it is found and deleted. Need to understand how to recalculate the sheets count in a loop as such. Please help

Sht_Cnt = ActiveWorkbook.Sheets.Count
For i = 1 To Sht_Cnt
If Sheets(i).Name = "Dummy" Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
End If
Next i
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try

Code:
Sht_Cnt = ActiveWorkbook.Sheets.Count
For i = Sht_Cnt To 1 Step -1
    If Sheets(i).Name = "Dummy" Then
        Application.DisplayAlerts = False
        Sheets(i).Delete
        Application.DisplayAlerts = True
        Exit For
    End If
Next i
 
Upvote 0
Hi all,

Not well tested, but I think you could also 'roughshod' it.

Rich (BB code):
Option Explicit
    
Function DelSheets(ParamArray ShNames() As Variant)
Dim i As Long
    
    On Error Resume Next
    Application.DisplayAlerts = False
    For i = LBound(ShNames) To UBound(ShNames)
        ThisWorkbook.Worksheets(ShNames(i)).Delete
    Next
    Application.DisplayAlerts = True
End Function
    
Sub exa1()
    Call DelSheets("Sheet1", "Sheet2")
End Sub
 
Upvote 0
Hi all,

I'm using the following code to delete a sheet if found. Its throwing an error, when it is found and deleted. Need to understand how to recalculate the sheets count in a loop as such. Please help

Sht_Cnt = ActiveWorkbook.Sheets.Count
For i = 1 To Sht_Cnt
If Sheets(i).Name = "Dummy" Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
End If
Next i

FWIW without the loop

Code:
Sub pavin()
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Dummy").Delete
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Hi all,

Not well tested, but I think you could also 'roughshod' it.

Rich (BB code):
Option Explicit
    
Function DelSheets(ParamArray ShNames() As Variant)
Dim i As Long
    
    On Error Resume Next
    Application.DisplayAlerts = False
    For i = LBound(ShNames) To UBound(ShNames)
        ThisWorkbook.Worksheets(ShNames(i)).Delete
    Next
    Application.DisplayAlerts = True
End Function
    
Sub exa1()
    Call DelSheets("Sheet1", "Sheet2")
End Sub
Hello,

GTO please tell me how I can change your code to take sheet name from cell A5 to A15.
Thank you.
 
Upvote 0
Hello,

GTO please tell me how I can change your code to take sheet name from cell A5 to A15.
Thank you.

For like:
Excel Workbook
A
4Header
5Sheet19
6Sheet20
7Sheet21
8Sheet22
9Sheet 23
10Sheet24
11Sheet25
12Sheet26
13Sheet27
14Sheet28
15Sheet29
Sheet2


In a Standard Module:
Rich (BB code):
Option Explicit
    
Function DelSheets(ShNames() As Variant)
Dim i As Long
    
    On Error Resume Next
    Application.DisplayAlerts = False
    For i = LBound(ShNames) To UBound(ShNames)
        ThisWorkbook.Worksheets(ShNames(i)).Delete
    Next
    On Error GoTo 0
    Application.DisplayAlerts = True
End Function
    
Sub exa1()
Dim ary()
    
    '// Please note that I used the CodeName of Sheet2 to qualify the Range //
    ary = Application.Transpose(Sheet2.Range("A5:A15").Value)
    Call DelSheets(ary)
End Sub

The above of course does not check success, so presuming that our inadvertant space in 'Sheet 23' would leave 'Sheet23' un-deleted.

Hope that helps,

Mark
 
Upvote 0
Hello, GTO It give an error and highlight Sheet2 and sub exa1()

Sub exa1()
Dim ary()

'// Please note that I used the CodeName of Sheet2 to qualify the Range //
ary = Application.Transpose(Sheet2.Range("A5:A15").Value)
Call DelSheets(ary)
End Sub

Can you help me?
 
Upvote 0
Rich (BB code):
'// Please note that I used the CodeName of Sheet2 to qualify the Range //


Hi there,

You can use the correct codename by selecting the sheet, right-click and choose view code. See how the properties window lists two name properties for the sheet? The top one is the codename for the sheet. Use that.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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