Macro to delete existing sheet

cornepiek

New Member
Joined
Jun 22, 2010
Messages
48
hi all

i need a macro to check what weekday it is based on cell value,
then to look for a sheet named for that day.

if cell valy is monday, look for sheet named monday.

if sheet exists, delete it, if not continue with operation.


please assist
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,

Try:

Code:
Sub deleteSheets()
    Dim wkDay As String
    Application.DisplayAlerts = False
    wkDay = Format(Weekday(Range("A1").Value), "dddd")
    For Each sht In ThisWorkbook.Sheets
        If sht.Name = wkDay Then sht.Delete
    Next
    Application.DisplayAlerts = True
End Sub

Edit: The above will work if you have a date in cell A1.
 
Last edited:
Upvote 0
This assumes the cell value (in A1) being tested is in the first worksheet and the weekday sheets follow this:
Code:
Sub DeleteMyNoggin ()
 
Dim i as Long
Dim DeleteValue as string
 
Application.ScreenUpdating = False
 
DeleteValue = Sheets(1).Range("A1")
On Error Resume Next
For i = 2 to Worksheets.Count
  If sheets(i).Name = DeleteValue Then
     Sheets(i).Delete
     Exit For
  End If
Next i
 
On Error Goto 0
Application.ScreenUpdating = True 
End Sub
 
Upvote 0
HI jack

you macro works, though i am prompted that sheet might contain data.

is there a way to bypass this prompt?
 
Upvote 0
Change my code to:
Code:
Sub EatinMePenguin()
 
Dim i As Long
Dim DeleteValue As String
 
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .Calculation = xlCalculationManual
End With
 
DeleteValue = Sheets(1).Range("A1")

On Error Resume Next

For i = 2 To Worksheets.Count
  If Sheets(i).Name = DeleteValue Then
     Sheets(i).Delete
     Exit For
  End If
Next i
 
On Error GoTo 0
 
With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .Calculation = xlCalculationAutomatic
End With
 
End Sub
 
Upvote 0
You could make this small addition to Jacks's code to delete the sheet immediately. This removes the loop

Cheers

Dave

Code:
Sub EatinMePenguin2()
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    On Error Resume Next
    ThisWorkbook.Sheets(Sheets(1).Range("A1").Value).Delete
    On Error GoTo 0
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,921
Latest member
BBQKING

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