Sorting with the Current Range in VBA

Muleskin57

New Member
Joined
Dec 22, 2016
Messages
23
I have the following macro that I want to run using the current active range (which I've already defined with another macro). What can I substitute for the range definitions to make this dynamic?

Sub test()
'
' test Macro
'


'
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=Range("G3:G83"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=Range("H3:H83"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=Range("A3:A83"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("LOG").Sort
.SetRange Range("A2:O83")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Is this related to your last question, where you know your starting point, but do not know your ending point (row)?
If so, then you can capture the row number of the cell you are finding like this:
Code:
lastRow = ActiveCell.Row
Then, you can incorporate that into all your range references, i.e.
Code:
[COLOR=#333333]Range("[/COLOR]G3:G83[COLOR=#333333]")[/COLOR]
would become:
Code:
[COLOR=#333333]Range("[/COLOR]G3:G[COLOR=#333333]" & lastRow)[/COLOR]
Repeat for all the other range references.
 
Upvote 0
Assuming your "other" macro selects all the data in the range A2:O83 try this:
Code:
Dim R as range
Set R = Selection

now replace your keys (in the order they appear) with these pieces of R:

R.columns(7).offset(1,0).resize(R.rows.count -1) '<--- G3:G83
R.Columns(8).Offset(1,0).Resize(R.rows.count -1) '<--- H3:H83
R.columns(1).Offset(1,0).Resize(R.rows.count -1) '<--- A3:A83

For Range("A2:O83") use R
 
Upvote 0
Assuming your "other" macro selects all the data in the range A2:O83 try this:
Code:
Dim R as range
Set R = Selection

now replace your keys (in the order they appear) with these pieces of R:

R.columns(7).offset(1,0).resize(R.rows.count -1) '<--- G3:G83
R.Columns(8).Offset(1,0).Resize(R.rows.count -1) '<--- H3:H83
R.columns(1).Offset(1,0).Resize(R.rows.count -1) '<--- A3:A83

For Range("A2:O83") use R

Not liking this syntax:

ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=R.Columns(7).Offset(1, 0).Resize(R.Rows.Count - 1), _
 
Upvote 0
Not liking this syntax:

ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=R.Columns(7).Offset(1, 0).Resize(R.Rows.Count - 1), _
Is that a standalone line or is there something after the continuation? Can you post all of your code?
 
Upvote 0
Here's both Macros. The 2nd is running off a range instead of the selection setup in the 1st macro, which is what I'm trying to accomplish.

Sub Last_Log_Data()
'
' Last_Log_Data Macro
'


'
Sheets("LOG").Select
Columns("O:O").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.Offset(rowOffset:=-1, columnOffset:=0).Activate
ActiveCell.Select
Range("A2:" & ActiveCell.Address).Select
Dim R As Range
Set R = Selection
End Sub
Sub Log_Sort_Name()
'
' Log_Sort_Name Macro
'


'
Application.Goto Reference:="R2C1:R3000C15"
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=Range("A3:A3000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=Range("H3:H3000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("LOG").Sort.SortFields.Add Key:=Range("G3:G3000"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("LOG").Sort
.SetRange Range("A2:O3000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A3").Select
End Sub
 
Upvote 0
That looks like your original post. Where does the code I gave you, that you say is causing a syntax error, fit in?
 
Last edited:
Upvote 0
I patched it in between the Key= sign and , (comma). Tried several things, this one works, but is not what I was shooting for.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,297
Messages
6,129,956
Members
449,545
Latest member
SURY

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