PDA

View Full Version : [SOLVED:] Dynamic Sort Range. How to code



LutonBarry
11-21-2015, 01:58 AM
I 'm trying to code a dynamic Sort range, as you can see below I'm trying to create a Dim and insert the Dim into the code but how?


Macro1 Macro
'
'
Dim myCells As Range

Range("A1").CurrentRegion.Select
Set myCells = Range("A1").CurrentRegion

ActiveWorkbook.Worksheets("Finished Sheet").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Finished Sheet").Sort.SortFields.Add Key:=Range( _
"B2:B15"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal

With ActiveWorkbook.Worksheets("Finished Sheet").Sort
.SetRange Range("A1:D15")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

nilem
11-21-2015, 02:21 AM
maybe so

With Range("A1:D" & Cells(Rows.Count, 1).End(xlUp).Row)
'or
'With Range("A1").CurrentRegion
.Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Header:=xlYes
End With

LutonBarry
11-21-2015, 02:52 AM
Nilem,

With Range("A1:D" & Cells(Rows.Count, 1).End(xlUp).Row)
Does the job and is easy to manipulate thanks very much.
Not need for the declaration either.