How to delete entire.column between a Range.

Trev0j

New Member
Joined
May 27, 2018
Messages
28
Hello,

what I want to do is:

i have a ranged name" Adjusted" and ranged name " Start" which is located in A1. i want to delete entire columns between range "Start" and "Adjusted". wherever the range " Adjusted" is located.
btw, before this code, need to find the range "Adjusted" and delete all the columns in between A1

I am thankfull for every help!
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Based on your responses, I am guessing Rick hit the nail on the head here:
Is "Start" and "Adjusted" actual Excel Defined Names or are they just range variables set within your code?
Maybe explain to us exactly how/where you created these "named" ranges.
If in VBA code, please post your code.
 
Upvote 0
Based on your responses, I am guessing Rick hit the nail on the head here:

Maybe explain to us exactly how/where you created these "named" ranges.
If in VBA code, please post your code.

Sorry if i named it wrong,also sorry for my english :) but these are the headings of the columns.

i have just recorded a macro selecting the entire rows
Code:
Rows("1:1").Select
Selection.Find(What:="Adjusted", After:=ActiveCell, LookIn:=xlFormulas, _        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate...
and to delete the columns without specific count in between "Start" and "Adjusted"
 
Upvote 0
Yes, that makes a huge difference. A cell populated with that value and a named range are two entirely different things.
I think this will do what you want:
Code:
Sub MyDelete()

    Dim startCol As Long
    Dim adjCol As Long
    
'   Find Start column
    startCol = Rows("1:1").Find(What:="Start", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
        
'   Find Adjusted column
    adjCol = Rows("1:1").Find(What:="Adjusted", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column

'   Delete columns in between
    If adjCol - startCol > 1 Then
        Range(Cells(1, startCol + 1), Cells(1, adjCol - 1)).EntireColumn.Delete
    End If
    
End Sub
 
Upvote 0
Yes, that makes a huge difference. A cell populated with that value and a named range are two entirely different things.
I think this will do what you want:
Code:
Sub MyDelete()

    Dim startCol As Long
    Dim adjCol As Long
    
'   Find Start column
    startCol = Rows("1:1").Find(What:="Start", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
        
'   Find Adjusted column
    adjCol = Rows("1:1").Find(What:="Adjusted", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column

'   Delete columns in between
    If adjCol - startCol > 1 Then
        Range(Cells(1, startCol + 1), Cells(1, adjCol - 1)).EntireColumn.Delete
    End If
    
End Sub
Thank you..it is working..
Thanks everyone!!!
 
Upvote 0
You are welcome.
Give Rick enough time, and he will probably come up with code that is much more concise than mine!;)
 
Last edited:
Upvote 0
Give Rick enough time, and he will probably come up with code that is much more concise than mine!;)
Not sure this would be considered more concise (is has more lines of code), but here is another macro that you can try...
Code:
Sub DeleteBetweenStartAndAdjusted()
  Dim A As Long, S As Long, C As Long, Row1 As Variant
  Row1 = Range("A1", Cells(1, Columns.Count).End(xlToLeft))
  For C = 1 To UBound(Row1, 2)
    If Row1(1, C) = "Start" Then S = C + 1
    If Row1(1, C) = "Adjusted" Then A = C - 1
  Next
  Range(Columns(S), Columns(A)).Delete
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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