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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,123
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Is this header a text value? If not try replacing "1005" with 1005.
 

Nickslat

New Member
Joined
Jun 18, 2008
Messages
3
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,879
Messages
5,834,217
Members
430,264
Latest member
ddeboor

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
Top