VBA to delete certain sheets based on cell contents with IF formula

bakarken

Board Regular
Joined
Sep 23, 2016
Messages
50
Office Version
  1. 2019
Platform
  1. Windows
Hi guys

Hoping you can help!

I've got an Excel 2019 spreadsheet with sheets:

Setup
Sheet1
Sheet2
Sheet3

In the Setup sheet, column A contains the names of other sheets in the workbook, and column B may or may not say 'DEL' next to each of these sheet names, based on IF formulas (see example image).

I'd like a VBA where, if column B says 'DEL' next to a certain sheet(s), that those sheet(s) are deleted using a developer button 'Delete sheets'. Preferably once clicked, no warning appears and sheets are just deleted.

I'm happy with adding in the Developer button and assigning a macro to it, just not sure what the Macro needs to say to achieve this.

In the future, I'd like to add more sheets and just list these down column A/B of Setup, so if this VBA could apply to the whole columns A:B please that would be great (rather than just cells A1:B3)

Thanks in advance!!
 

Attachments

  • Capture.PNG
    Capture.PNG
    3.2 KB · Views: 19

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Give this a try with a copy of your workbook.

VBA Code:
Sub Del_Sheets()
  Dim r As Long
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  With Sheets("Setup")
    For r = .Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
      If .Cells(r, "B").Value = "DEL" Then
        On Error Resume Next
        Sheets(.Cells(r, "A").Value).Delete
        On Error GoTo 0
        .Rows(r).Delete '<-This deletes the row from 'Setup'
      End If
    Next r
  End With
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub

True
End Sub
 
Upvote 0
Solution
Give this a try with a copy of your workbook.

VBA Code:
Sub Del_Sheets()
  Dim r As Long
 
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  With Sheets("Setup")
    For r = .Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
      If .Cells(r, "B").Value = "DEL" Then
        On Error Resume Next
        Sheets(.Cells(r, "A").Value).Delete
        On Error GoTo 0
        .Rows(r).Delete '<-This deletes the row from 'Setup'
      End If
    Next r
  End With
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub

True
End Sub
That's fantastic thanks so much Peter!!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

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