• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macros to delete columns based on condition

Karthik

New Member
Hi,


I have a file which I get every week which has upto 25 columns of data, however all that matters to me is just 4 columns of data: 'Campaign,' 'Status,' 'Budget,' & 'Cost.' (sample table can be found here: https://docs.google.com/spreadsheet/...hl=en_US#gid=0 )


Can somebody help me with an excel macro that can identify only these 4 column headers (preferably in this order), and retain their values, and delete the rest of the columns.


Thanks a lot in advance!
 
Karthik


Your link doesn't work ?


However you could try this

Copy and paste it to a VBA code module

Change the sheet definition line

Run it

[pre]
Code:
Sub delete_cols()
Dim i As Integer
Worksheets("sheet1").Select 'Change sheet name to suit
For i = 25 To 1 Step -1
Select Case Cells(1, i).Value
Case "Campaign", "Cost", "Budget", "Status"
'do Nothing
Case Else
Columns(i).Delete
End Select
Next

End Sub
[/pre]
 
Hey Hui, Thanks for your reply. Actually, the doc link din't get pasted fully, i believe. It's https://docs.google.com/spreadsheet/ccc?key=0Ao7RzFobcTErdGVQV1BYOUpTN0UxWkVzc3ZXRGtGcGc&hl=en_US#gid=0


In the meantime, I shall try out your code!
 
Back
Top