Sort Macro

F

FrankTimJr

I'm curious to find out if I can eliminate the Range on the below macro I
created?

I want to it to not care what Range I select, as long as the Range includes
cells from column AN (Not the entire column). Since Excel 2007 does not save
the sort parameters I need to figure out a way to automate the sort. I have
several worksheets and several seperate ranges I need to sort on. Is this
possible? I want to run the macro AFTER I select the Range I want to sort
and not have the macro select the Range.

Sub SortByColumAN()
'
' Sorts data by column AN
' Keyboard Shortcut: Ctrl+Shift+Z
'
ActiveWorkbook.Worksheets("Renewal Rate").Sort.SortFields.Add
Key:=Range( _
"AN54:AN58"), SortOn:=xlSortOnValues, Order:=xlDescending,
DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Renewal Rate").Sort
.SetRange Range("E54:DF58")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
S

Sheeloo

Test with
If Not Intersect(ActiveWindow.RangeSelection, ActiveSheet.Range("N:N")) Is
Nothing

then pass ActiveWindow.RangeSelection as the range to sort
 
F

FrankTimJr

I don't completely understand how to add that code into the macro. I don't
know how to write code. Do I replace the code completely or add that line
somewhere within the existing code?
 
S

Sheeloo

Try (whole macro is given)
'(I am assuming headers are in Row 1)

Sub SortByColumAN()
'
' Sorts data by column AN
' Keyboard Shortcut: Ctrl+Shift+Z
'
ActiveWorkbook.Worksheets("Renewal Rate").Sort.SortFields.Clear

If Not Intersect(ActiveWindow.RangeSelection, ActiveSheet.Range("AN:AN")) Is
Nothing Then
ActiveWorkbook.Worksheets("Renewal Rate").Sort.SortFields.Add _
Key:=Range("AN1:AN1"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Renewal Rate").Sort
..SetRange ActiveWindow.RangeSelection
..Header = xlGuess
..MatchCase = False
..Orientation = xlTopToBottom
..SortMethod = xlPinYin
..Apply
End With
Else
MsgBox "Col N is not included in Selection."
End If
End Sub
 
D

Dave Peterson

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Applying Variables to SORT 4
Undo Macro Action 3
writing a sort macro 2
Modify Sort Routine to inlcude All Data 2
VBA 2 Codes 2
Clear Check Box 2
Sort by one column then another. 2
Sort Macro Compatibility 3

Top