Delete sheet if the row 2 cells don't contain a specific status

badrhari

New Member
Joined
Jan 6, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi all & Happy New Year,

Quite new to using VBA and facing a challenge.
I have an Excel file with multiple sheets and the same structure, as below. I'm interested in working only in row 2 range
NameABC
StatusCreatedHoldReady
Datetodaytodaytoday

What I want is to go in each sheet and if the row 2 range doesn't contain Ready status (or it contains only Hold and Created) on any cell, delete the whole sheet. If it contains, just keeps the sheet.
There some piece of code I've found and adapted but doesn't work as expected.

Sub test()
Dim i As Integer
Application.DisplayAlerts = False
On Error Resume Next
For i = Worksheets.Count To 1 Step -1
Sheets(i).Select
If Not Range("A2").Value = "Ready" Then
Sheets(i).Delete
End If
Next i
Application.DisplayAlerts = True
End Sub

Any help or suggestions will be much appreciated.

Many thanks.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Forum, and Happy New Year to you too.

The code below will delete all worksheets that don't contain "Ready" in row 2. Is this what you wanted?

VBA Code:
Sub Test()
    
    Dim ws As Worksheet
    
    Application.DisplayAlerts = False
    
    For Each ws In ThisWorkbook.Worksheets
        If IsError(Application.Match("Ready", ws.Range("2:2"), 0)) Then ws.Delete
    Next ws
      
    Application.DisplayAlerts = True

End Sub
 
Upvote 0
Welcome to the Forum, and Happy New Year to you too.

The code below will delete all worksheets that don't contain "Ready" in row 2. Is this what you wanted?

VBA Code:
Sub Test()
   
    Dim ws As Worksheet
   
    Application.DisplayAlerts = False
   
    For Each ws In ThisWorkbook.Worksheets
        If IsError(Application.Match("Ready", ws.Range("2:2"), 0)) Then ws.Delete
    Next ws
     
    Application.DisplayAlerts = True

End Sub
Many thanks Stephen. This works just perfect for what I need.

Cheers.
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

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