Column deletion for a certain range (using letters i.e. C:CH)

d12345

New Member
Joined
Jun 22, 2018
Messages
2
I'm trying to delete a range of columns that have dates (starting at 9/1/2017) as headers. I need it to delete from 9-2017 -> 8-2018, but when I run the code it keeps: Sep-17, Oct-17, and deletes Sep-18 and Oct-18. I'm not quite sure what is happening. Here is the code:

Code:
Dim dateStart, dateEnd, dateStartAdd, dateEndAdd, startCol, endCol As Variant
    
        With ws.Range("C1:CY1")
            Set dateStart = .Find(What:="Sep-10", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            Set dateEnd = .Find(What:="Aug-17", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            
            dateStartAdd = dateStart.Address
            dateEndAdd = dateEnd.Address
            
            startCol = Split(dateStartAdd, "$")(1)
            endCol = Split(dateEndAdd, "$")(1)
            
            .Columns(startCol & ":" & endCol).EntireColumn.Delete
        
        End With
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You shouldn't need so many variables to delete columns, seems a bit excessive - try:
Code:
Dim dateStart As Range
Dim dateEnd As Range

With ws
    Set dateStart = .Range("C1:CY1").Find(what:="Sep-10", LookIn:=xlValues, lookat:=xlPart)
    Set dateEnd = .Range("C1:CY1").Find(what:="Aug-17", LookIn:=xlValues, lookat:=xlPart)
    .Range(dateStart, dateEnd).EntireColumn.Delete
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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