Delete sheets with reference to named range

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
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:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Following on from this, I want to have two named ranges, like this:

Code:
Option Explicit
Sub Start()
    Dim MyCls As Class1
    Set MyCls = New Class1
    
    Set MyCls.Cols = Union(Sheet3.Range("a"), Sheet3.Range("b"))
    
    Call MyCls.DeleteWorksheets
End Sub

but it fails here:

Code:
Set Rng = .Find(What:=ws.Name, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)

with this message:

Code:
Run-time error '13': Type mismatch

What's wrong?
 
Upvote 0
Where is the 'With' statement? I'm guessing the troubled code is this:

Code:
.Cells(.Cells.Count)
 
Upvote 0
Where is the 'With' statement? I'm guessing the troubled code is this:

Code:
.Cells(.Cells.Count)


Here:

Code:
        With Cols
        
            Set Rng = .Find(What:=ws.Name, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
 
Last edited:
Upvote 0
No, it works in my first example with just

Code:
 With Cols

Adding

Code:
With MyCls.Cols

won't compile.
 
Last edited:
Upvote 0
Sorry. My fault. Should have seen it was within the class:

*Edited, didn't see named ranges
 
Last edited:
Upvote 0
Why would not something this simple work?

Code:
[B]​[/B]Sub Delete_Sheets()
Application.DisplayAlerts = False
Dim c As Range
    
    For Each c In Range("b")
    Sheets(c.Value).Delete
    Next
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Why would not something this simple work?

Code:
[B]​[/B]Sub Delete_Sheets()
Application.DisplayAlerts = False
Dim c As Range
    
    For Each c In Range("b")
    Sheets(c.Value).Delete
    Next
Application.DisplayAlerts = True
End Sub

My first example, with only a single named range, works.

I'm trying to extend it to two named ranges.
 
Last edited:
Upvote 0
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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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