VBA to Delete columns

Status
Not open for further replies.

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
I have a very large spreadsheet (Market Data) it has about 100 columns, but I only use about 6 of those columns. Is there a VBA that will allow me to keep only the column headers I want and delete the rest?

I would like to keep column headers mkt data 10th, ee info, position, market ratio. Everything else is trash and can be deleted.

I wrote a macro but it goes by column number and not header name, so that did not work for me.

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This should work for you

Sub RemoveCol()
Dim ColNo As Integer
Dim rng As Range

Set rng = ThisWorkbook.Sheets(1).UsedRange

For ColNo = rng.Columns.Count To 1 Step -1
Select Case rng.Cells(1, ColNo)
Case "mkt data 10th", "ee info", "position", "market ratio"
'Take No Action
Case Else 'Delete the Column
rng.Columns(ColNo).Delete
End Select
Next ColNo

End Sub
 
Last edited:
Upvote 0
Please do not post the same question multiple times. Questions of a duplicate nature will be locked or deleted, per the Posting Rules (especially points 21-23).

It seems that you have moved on from here so any bumps, clarifications, or follow-ups should be posted to the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,203,046
Messages
6,053,192
Members
444,644
Latest member
keepontruckinc4

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