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
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,694
Messages
5,597,571
Members
414,156
Latest member
WDMix

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