Create a Excel Macro to delete Tabs and columns based on names of Tabs

chrisbonational

New Member
Joined
Nov 6, 2015
Messages
4
I need a macro that deletes all tabs not named "Address," "Phone Number," and "Birth Date" (there will are varying tab names depending on the report, so i want this macro to work regardless of how many and the names of the other tabs [if possible]. I would also like it if one of the above stated tabs is missing, to just skip it and keep going.)

Once that is done, I need to delete specific columns (varies depending on which of the 3 tabs we are looking at). Can someone help me out with this please?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi

The following should delete any sheets not titled "Address", "Phone Number" or "Birth Date".

Code:
Sub DeleteSheets()


    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    For Each sht In ThisWorkbook.Worksheets
    
    If sht.Name <> "Address" And sht.Name <> "Phone Number" And sht.Name <> "Birth Date" Then sht.Delete
    
    Next sht
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True


End Sub

Can you provide some more information on which columns on which worksheets need to be deleted?
 
Upvote 0
Code:
Sub Delete_columns()


    Sheets("Address").Select
    Columns("F:N").Delete
    Columns("A:D").Delete


    Sheets("Phone number").Select
    Columns("F:N").Delete
    Columns("B:D").Delete


    Sheets("Birth date").Select
    Columns("B:N").Delete


End Sub
 
Upvote 0
Hey benpauley: Thanks for your response.

When running the code you gave me, it gave me an error, when i hit Debug, it had

"Then sht.Delete" highlighted. Is it because there is data on the other sheets as opposed to the other sheets being blank?
 
Upvote 0
It is okay, thank you Benpauley

I was able to figure it out using your macro mixed with another one i found online. It needed an If Error thing in there.
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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