VBA Delete current work sheet if not a specific title

mrmrf

New Member
Joined
Jun 3, 2019
Messages
34
Hi.

Im needing help for VBA to delete current work sheet if not a specific name.

Quite a lot of examples out, but can't seem to adapt.

Thanks.
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,039
Office Version
2016
Platform
Windows
Code:
Application.DisplayAlerts = False
If ActiveSheet.Name = "Sheet1" Then ActiveSheet.Delete
Application.DisplayAlerts = True
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,048
Office Version
2010
Platform
Windows
Code:
Application.DisplayAlerts = False
If ActiveSheet.Name = "Sheet1" Then ActiveSheet.Delete
Application.DisplayAlerts = True
The OP said "delete current work sheet if not a specific name", so I think your equal sign (=) should be a not equal sign (<>).
 

mrmrf

New Member
Joined
Jun 3, 2019
Messages
34
Thanks footoo, but what if i dont want it to delete the active sheet if its a specific name.

For example, i could have sheet1, sheet2, sheet3. Each page has this code to delete, but i dont want it to delete if the name is sheet1
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,039
Office Version
2016
Platform
Windows
See post # 3
 

mrmrf

New Member
Joined
Jun 3, 2019
Messages
34
Actually, the code works and delets as require, but i get this error

run-time error '-2147221080 (800401a8) automation error

Not sure why?
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,039
Office Version
2016
Platform
Windows
Do you have the macro containing the code in the sheet module being deleted?
If so, put in a normal module instead.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,666
Messages
5,488,184
Members
407,631
Latest member
Mehezabin

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top