Deleting tab without using sheet name but object name

Dkeller12

Board Regular
Joined
Oct 7, 2011
Messages
60
I am looking for VBA code that will allow me to delete a sheet using its Object Name and not the sheet name. The reason I need this is I download a document on a monthly basis where the name of sheet1 is different each month. I know the first sheet is always sheet1 then the new file name in the project explorer. Can I this be done?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to the Board!

Something like this:

Code:
Application.DisplayAlerts = False
    SH1CodeName.Delete
Application.DisplayAlerts = True

HTH,
 
Upvote 0
Smitty - Thanks for the quick response. I however am not getting that to work. I am getting an error on line 2 of the code (SH1CodeName.Delete). Should I be adjusting this code at all?

Thanks again.
 
Upvote 0
Since I am trying to delete sheet1 my coding looks like this:

Application.DisplayAlerts = False
Sheet1.Delete
Application.DisplayAlerts = True

This however is giving me an error. I am getting it to work using the following code:

Application.DisplayAlerts = False
Sheets(1).Delete
Application.DisplayAlerts = True

This way relies on the sheet I want to delete always being in the first spot in the spreadsheet. I would like to use the code you gave me but don't know why I am getting an error. Could the fact that I am running this code from a personal macro and not a worksheet macro be causing the error?
 
Upvote 0
Yes, it does matter; you can't use the codenames of sheets in one workbook from another workbook in that fashion. You could do it more convoluted:

Code:
    Application.DisplayAlerts = False
    With ActiveWorkbook
        .Worksheets(CStr(.VBProject.VBComponents("Sheet1").Properties("name"))).Delete
    End With
    Application.DisplayAlerts = True
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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