if statement to delete sheets

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
Hi Guys,
I need to write some code so that if a worksheet is there ie DATA, then the macro removes that worksheet.

any help would be greatly appreciated
thanks
Sam
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

zzjasonzz

Well-known Member
Joined
Apr 23, 2006
Messages
649
try:
Code:
For Each ws In ThisWorkbook.Worksheets
     If ws.Name = "DATA" Then
        Application.DisplayAlerts = False
        ws.Delete
        Application.DisplayAlerts = True
    End If
Next ws
 
Upvote 0

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
when I run this, it looks like it does something, however it doesnt delete the worksheet
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Code:
On Error Resume Next
Application.DesplayAlerts = False
Sheets("Data").Delete
Application.DisplayAlerts = True
End Sub

It will not delete the sheet if it is the only sheet in the workbook.
 
Upvote 0

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
sorry it does work - i just relised that it is case sensitive.
Is there anyway that we can make it so that it is not case sensitive?

thanks
 
Upvote 0

Forum statistics

Threads
1,190,579
Messages
5,981,761
Members
439,733
Latest member
hmopheim

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
Top