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:
How do I make this relative rather than absolute positioning? Thanks -- Blackie
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