Commence macro from current (not absolute) cursor position

BlackieHamel

Board Regular
Joined
May 9, 2014
Messages
93
I should know how to do this, but I don't.

When I use Record Macro to create some new VBA code, it records the absolute position - row, column, and sheet name - but I often want a macro to work anywhere from the starting position. See the code below. My goal is to select a set of cells in a column, run the macro, and have those cells sorted Largest to Smallest:
Code:
Sub Sort_returns_boxes()
'
' Sort_returns_boxes Macro
' Sort the selected column from highest to lowest without affecting other columns in the spreadsheet.
'
' Keyboard Shortcut: Ctrl+s
'
    ActiveWorkbook.Worksheets("""Periodic Table""").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("""Periodic Table""").Sort.SortFields.Add2 Key:= _
        Range("N26:N34"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("""Periodic Table""").Sort
        .SetRange Range("N26:N34")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

How do I make this relative rather than absolute positioning? Thanks -- Blackie
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Change
Code:
 Range("N26:N34")
to
Code:
Selection
 
Upvote 0
Fluff -- thanks, but that didn't work. Note that "Range ("N6:N34")" appears twice in the code. Do I replace it with "Selection" (and not Range:Selection) in both places? And can I safely delete the lines that make reference to
ActiveWorkbook.Worksheets("""Periodic Table""")
 
Upvote 0
Change both instances of the range with selection, the periodic table sheet will need to be the active sheet.
 
Upvote 0
I think I'm close -- it worked once! -- but now I get a 1004 error: "the Sort reference is not valid." Can you see what's wrong with this code?
Code:
Sub Sort_returns_boxes()
'
' Sort_returns_boxes Macro
' Sort the selected column from highest to lowest without affecting other columns in the spreadsheet.
'
' Keyboard Shortcut: Ctrl+n
'
    
    ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:= _
        Selection, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.ActiveSheet.Sort
        .SetRange Selection
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Firstly you have removed the line that clears the sort field, you need to put that back in, otherwise you are likely to get into trouble.
 
Upvote 0
Another option is
Code:
Sub BlackieHamel()
   Selection.Sort key1:=Selection, order1:=xlDescending, Header:=xlNo, dataoption1:=xlSortNormal
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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