molsonlabatt
New Member
- Joined
- Jul 25, 2012
- Messages
- 14
Hi,
I have read a lot on this topic, but I am still stumped.
What I need is a dynamic range for both columns and rows (I.e. the number of columns and number of rows will change). The entire selection will need to be sorted by the last column with data.
Below is the VBA code I get from the recorder. I know I need to create a dynamic range for but, I am unable to get it to work. I have highlighted below the areas that need to change (I think).
Cheers
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H1:H20") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:H20")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I have read a lot on this topic, but I am still stumped.
What I need is a dynamic range for both columns and rows (I.e. the number of columns and number of rows will change). The entire selection will need to be sorted by the last column with data.
Below is the VBA code I get from the recorder. I know I need to create a dynamic range for but, I am unable to get it to work. I have highlighted below the areas that need to change (I think).
Cheers
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H1:H20") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:H20")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With