Delete column based on criteria in header

Nickslat

New Member
Joined
Jun 18, 2008
Messages
3
Please help
I am writing a macro code that will automatically remove colummns on a spreadsheet. The criteria is in row 1 (header) and is a number.
The code I have tried to write is as follows

Code:
Sheets("WX_CAPPLAN").Select
    Dim LastCol As Integer, c As Integer
            LastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
                For c = LastCol To 2 Step -1
                    If Cells(1, c).Value > "1005" Then Columns(c).Delete
                Next c

but it doesnt seem to be working. What this particular code needs to do is delete any colummn whose header is higher than the value 1005

Any help from you gurus would be appreciated

Thanks in advance
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If Cells(1, c).Value > "1005" Then Columns(c).Delete

change to

If 0+Cells(1, c).Value > 1005 Then Columns(c).Delete

assumes all headers are numeric by nature
 
Upvote 0
That worked perfectly. I added the 0+ in front of the 'cells' and removed the "" marks and everything is great - you guys are the greatest

Thanks for the help
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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