Sorts using a Macro

sharpeye

Board Regular
Joined
Oct 5, 2018
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hi guys, Im looking for a little help with my spreadsheet.
What im trying to do is a sort using row K largest to smallest then row I largest to smallest, however, the selection of rows is different each time I do the sort. I tried to record a macro after highlighting my selection but it just sorts the same selection every time.

I would like to select rows 52 to 97, CNTL + SHIFT + S, sorted by K then I
Then it may be select rows 105 to 142, CNTL + SHIFT + S, sorted by K then I

and so on.

Any ideas??
Many thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,

Why don't you post your initial macro ... to allow for the necessary modification ...
 
Upvote 0
Yeah sorry, this is what the macro looks like. I can see the selection probably just wants taking out of the macro now ive seen the code

Sub Sort()
'
' Sort Macro
' Sort K then I
'
' Keyboard Shortcut: Ctrl+Shift+S
'
ActiveWorkbook.Worksheets("SDLRES").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SDLRES").Sort.SortFields.Add Key:=Range( _
"K2858:K2952"), SortOn:=xlSortOnValues, order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("SDLRES").Sort.SortFields.Add Key:=Range( _
"I2858:I2952"), SortOn:=xlSortOnValues, order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("SDLRES").Sort
.SetRange Range("A2858:IJ2952")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Upvote 0
After a little digging around on the internet I found I should replace "Range" with "Selection" but when I try to run the Macro I get

Compile Error, Expected End With
 
Upvote 0
Actually thats not correct, I must have made another change so I changed it all back and started again. Now when I replace "Range" with "Selection" it just does nothing. Here's my Macro now

Sub Sort()
'
' SortD Macro
' Sort
'
' Keyboard Shortcut: Ctrl+Shift+S
'
ActiveWorkbook.Worksheets("SDLRESTG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SDLRESTG").Sort.SortFields.Add Key:=Selection, SortOn:=xlSortOnValues, order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("SDLRESTG").Sort.SortFields.Add Key:=Selection, SortOn:=xlSortOnValues, order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("SDLRESTG").Sort
.SetRange Selection
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Upvote 0
Post your code as you now have it.
Edit: OP just has.... reading it.

sharpeye, your key should be the column (or first cell of) the column you are sorting on.
 
Last edited:
Upvote 0
This is where I am now

Sub Sort()
'
' SortD Macro
' Sort
'
' Keyboard Shortcut: Ctrl+Shift+S
'
ActiveWorkbook.Worksheets("SDLRESTG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SDLRESTG").Sort.SortFields.Add Key:=Selection.Columns(11), SortOn:=xlSortOnValues, order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("SDLRESTG").Sort.SortFields.Add Key:=Selection.Columns(9), SortOn:=xlSortOnValues, order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("SDLRESTG").Sort
.SetRange Selection
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

This does nothing at all (as you may be able to tell, VBA isnt my strong suit)
 
Upvote 0
Hello,

As pointed out by Mark ... you need to adjust your Sort key ...

Code:
[COLOR=#333333]Key:=Selection.Columns(11)[/COLOR]

1. Get rid of the word Selection

2. Make sure Columns(11) which is Column K is the Column you want to use to sort your data ...

Hope this will help
 
Upvote 0
Dont know how but I managed to get it all working (its not so much that I dont know how I got it working but more the how it wasnt working thats puzzling??)
Thanks for your help =)
 
Upvote 0
You are welcome ...

Glad you could fix your problem ...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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