Sort Columns with dynamic range

cajsoft

New Member
Joined
Feb 22, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi Experts,
I have an excel sheet where the data "in Plan" columns are all copied in with a macro from different sheets. I need a way programmatically (ie as part of the same macro) to Sort the columns by Row number 2 - "Ship Date" (left to right).

I want the sort to happend At the end of the current VBA code in the macro. I have saved the starting Column and End Column numbers as variables.. ie in this case "E" and "N".

I've tried to use similar code to this -

Range("E1:N1").Sort Key1:=Range("E2:N2"), Order1:=xlAscending, Orientation:=xlLeftToRight

but it keeps failing (obviously I'd need to replace the static column values with a variable, but in this case I was just testing)

please help.


Capture.PNG
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this:

VBA Code:
Sub sortcolumns()
  Dim lr As Long
  Dim sh As Worksheet
  
  Set sh = ActiveSheet
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  lr = sh.Range("E:N").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  sh.Range("E1:N" & lr).Sort Key1:=Range("E2:N2"), Order1:=xlAscending, _
    Orientation:=xlLeftToRight, Header:=xlNo
End Sub
 
Upvote 1
Solution
Try this:

VBA Code:
Sub sortcolumns()
  Dim lr As Long
  Dim sh As Worksheet
 
  Set sh = ActiveSheet
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  lr = sh.Range("E:N").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  sh.Range("E1:N" & lr).Sort Key1:=Range("E2:N2"), Order1:=xlAscending, _
    Orientation:=xlLeftToRight, Header:=xlNo
End Sub

thanks, that works a treat.. I tried to adjust the code to allow variables in the Range but it's not working.. here's a test I did using a String.

not sure why its not working as I thought it was just a string value in the Range.

VBA Code:
    strRange = "E:N"
    strKeyRange = "E2:N2"
    Set sh = ActiveSheet
    If sh.AutoFilterMode Then sh.AutoFilterMode = False
    lr = sh.Range(strRange).Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
    sh.Range(strRange & lr).Sort Key1:=Range(strKeyRange), Order1:=xlAscending, Orientation:=xlLeftToRight, Header:=xlNo
 
Upvote 0
Try this:
Rich (BB code):
strRange = "E1:N"
thank you.. I figured it out that I had a typo after I posted this..

had to make a dirty looking string to hold all the values.. no ideal.. but it works :)



VBA Code:
strSortStartRange = Split(Cells(1, intStartColumn).Address(True, False), "$")
strSortEndRange = Split(Cells(1, intStartColumn).Address(True, False), "$")

strRange = strSortStartRange(0) & ":" & strSortEndRange(0)
    strRange2 = strSortStartRange(0) & strSortStartRange(1) & ":" & strSortEndRange(0)
    strKeyRange = strSortStartRange(0) & "2:" & strSortEndRange(0) & "2"
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,597
Members
449,038
Latest member
Arbind kumar

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