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
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,946
Office Version
  1. 365
Platform
  1. Windows
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.
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,388
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Muleskin57

New Member
Joined
Dec 22, 2016
Messages
23

ADVERTISEMENT

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), _
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,388
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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?
 

Muleskin57

New Member
Joined
Dec 22, 2016
Messages
23

ADVERTISEMENT

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
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,388
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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:

Muleskin57

New Member
Joined
Dec 22, 2016
Messages
23
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:

Watch MrExcel Video

Forum statistics

Threads
1,122,818
Messages
5,598,280
Members
414,223
Latest member
Accountant2B

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
Top