Delete sheets with reference to named range

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I want to delete worksheets according to what's specified on a worksheet.

In Sheet1, I have a named range called b, which contains two values, Sheet1 and Sheet2.

Code:
Option Explicit

Sub Start()

    Dim MyCls As Class1
    Set MyCls = New Class1
    
    Set MyCls.Cols = Sheet1.Range("b")
    
    Call MyCls.DeleteWorksheets

End Sub

This is in Class1:

Code:
Option Explicit
    Private pCols As Range
    
Public Property Get Cols() As Range
    
    Set Cols = pCols
    
End Property
Public Property Set Cols(ByVal C As Range)
    
    Set pCols = C
    
End Property
    
Sub DeleteWorksheets()
    Application.EnableEvents = False
    
    Dim Rng As Range
    
    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
               
        With Sheet1(Cols)
        
            Set Rng = .Find(What:=ws.Name, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            
            If Rng Is Nothing Then
                
                Application.DisplayAlerts = False
                ws.Delete
                Application.DisplayAlerts = True
            End If
            
        End With
       
    Next ws
    
    Application.EnableEvents = True
End Sub

The code fails here:

Code:
With Sheet1(Cols)

with a message:

Code:
Run-time error '438': Object doesn't support this property or method

What is wrong?

Thanks

EDIT SORTED:

Code:
With Cols
 
Last edited:
This line is the line causing the issues:

Code:
[COLOR=#333333]After:=.Cells(.Cells.Count)[/COLOR]

I just tested your code. What is the line trying to achieve? Trying to find the worksheet's name but only after a certain cell?
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
For 2 named range you would do it this way:

Code:
Sub Delete_Sheets()
Application.DisplayAlerts = False
Dim c As Range
Dim Del As Variant
Del = Array("b", "Mom")
   
    For i = 0 To 1
        For Each c In Range(Del(i))
            Sheets(c.Value).Delete
        Next
    Next
Application.DisplayAlerts = True
End Sub

Thanks, that's what I wanted.
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,090
Members
449,065
Latest member
Danger_SF

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