Deleting columns based on cell value

swalk88

New Member
Joined
Feb 13, 2014
Messages
16
Hi,

I have a spreadsheet with a row of dates. I want to be able to put a date in a cell say "A1" and run a macro to delete all columns with dates newer then the date in A1.

Thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Record a macro.
Highlight the header set filters.
Apply filters to the column with dates, using less than specific date.
Delete the date
stop recording.

Go into the visual basic editor
change the line of code that has the "<date you types" to "<" & range("A1")

then when you re-run the code, it will choose the value that is in A1 as the date variable.
 
Upvote 0
Maybe this
Code:
Sub MM1()
Dim lc As Integer, c As Integer
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For c = lc To 2 Step -1
    If Cells(1, c).Value > Cells(1, 1) Then
        Columns(c).Delete
    End If
Next c
End Sub
 
Upvote 0
This works well thanks.


Maybe this
Code:
Sub MM1()
Dim lc As Integer, c As Integer
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For c = lc To 2 Step -1
    If Cells(1, c).Value > Cells(1, 1) Then
        Columns(c).Delete
    End If
Next c
End Sub
 
Upvote 0
Hi,

Further to this, how can I set it to start at Column C rather than column A?

Thanks,
 
Upvote 0
This will delete columns from Column "C"
Code:
Sub MM1()
Dim lc As Integer, c As Integer
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For c = lc To 3 Step -1
    If Cells(1, c).Value > Cells(1, 1) Then
        Columns(c).Delete
    End If
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

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