Delete sheet based on cell value

PCTech

Board Regular
Joined
Mar 24, 2005
Messages
215
I have a worksheet that I need the keep clean. In column A is a value that has a sheet with the same name. What I would like to do is, if there is an X in column F, delete the sheet that column A in the same row refers to, and then delete that row.
Will someone please help?
Thanks,
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hello PCTech,
See if something like this doesn't get you started.
Code:
Sub DeleteSheets()
Dim LstRw&, ShtName&
With Application
  .DisplayAlerts = False
  .ScreenUpdating = False

  '''/// Change 'Sheet1' to the real name of your sheet _
         containing the list of sheet names in column A.
  With Sheets("Sheet1")
    LstRw = .Cells(Rows.Count, "A").End(xlUp).Row
    For ShtName = LstRw To 1 Step -1
      If .Cells(ShtName, "F").Value = "X" Then
        On Error Resume Next
        Sheets(.Cells(ShtName, "A").Value).Delete
        If Not Err.Number = 0 Then
          MsgBox "There is no sheet named " & .Cells(ShtName, "A").Value & _
                 " to delete.", , "Sheet Deletion Error"
          Err.Clear
          GoTo SkipThisOne
        End If
        .Rows(ShtName).EntireRow.Delete
      End If
SkipThisOne:
    Next
  End With

  .ScreenUpdating = True
  .DisplayAlerts = True
End With
End Sub

Hope it helps. Let us know if it doesn't. (. . . and why. :wink: )
 
Upvote 0
The concept is correct. For every X in column F, I get the message box telling me there is no sheet named 86651. Which is the first one it comes to. It does that with all the X's. I did change the format in the column to text and I get the same error.
What else should I try?

UPDATE. It works on all new sheets I create.

Thanks for your help.
 
Upvote 0
Ahh, sheets named with numeric values instead of text.
(I should've though of that.) Try changing this line from:
Sheets(.Cells(ShtName, "A").Value).Delete
to:
Sheets(.Cells(ShtName, "A").Text).Delete

This way it shouldn't matter what the sheet names are or how the column is formatted.

Does that help?
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

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