Vba already selected range sort

CrashOD

Board Regular
Joined
Feb 5, 2019
Messages
118
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Sub Sort_D_Then_B()

ActiveWorkbook.Worksheets("Per Capita").Sort.SortFields.Clear

Sort_D_Then_B

s)

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields.Add Key:=Range(_ "D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=

x1SortNormal

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields. Add Key:=Range(

"B:B"), Sorton:=xlSortOnValues, Order:=xlAscending, DataOption:=

x1SortTextAsNumbers

With ActiveWorkbook.Worksheets ("Per Capita").Sort

'.SetRange Rows (ActiveCells)

.Header = xlGuess

.MatchCase = False

.Orientation = x1TopToBottom

.SortMethod = xlPinyin

.Apply

ActiveCell.Offset(1, 0).Select

End With

End Sub.

For some reason it's not sorting it. It doesn't run into an error anymore. And I got it to select one cell afterwards just to clear the full selection. But it does not sort it at all.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The line wrapping on your code is all messed up, but I assume that is just a copy/paste issue.
Please use code tags when posting your VBA code, and that should avoid that issue and allow us to easily copy and test your code.
See here for how to do that: How to Post Your VBA Code

It might be helpful if you also posted a small sample of data you are trying to run this on.
 
Upvote 0
Further to what Joe has said, you also need to change all the x1 to xl (lower case L not number one)
 
Upvote 0
Sorry the one is actually an l I took a picture off my computer at work and use Google lens to copy paste and didn't realize that it was showing a one instead of an l.


Sub Sort_D_Then_B()

ActiveWorkbook.Worksheets("Per Capita").Sort.SortFields.Clear

Sort_D_Then_B

s)

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields.Add Key:=Range(_ "D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=

x1SortNormal

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields. Add Key:=Range(

"B:B"), Sorton:=xlSortOnValues, Order:=xlAscending, DataOption:=

x1SortTextAsNumbers

With ActiveWorkbook.Worksheets ("Per Capita").Sort

'.SetRange Rows (ActiveCells)

.Header = xlGuess

.MatchCase = False

.Orientation = x1TopToBottom

.SortMethod = xlPinyin

.Apply

ActiveCell.Offset(1, 0).Select

End With

End Sub.

For some reason it's not sorting it. It doesn't run into an error anymore. And I got it to select one cell afterwards just to clear the full selection. But it does not sort it at all.
VBA Code:
Sub Sort_D_Then_B()

ActiveWorkbook.Worksheets("Per Capita").Sort.SortFields.Clear

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields.Add Key:=Range("D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields. Add Key:=Range("B:B"), Sorton:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers

With ActiveWorkbook.Worksheets ("Per Capita").Sort

' .SetRange Rows (ActiveCells)

.Header = xlGuess

.MatchCase = False

.Orientation = xlTopToBottom

.SortMethod = xlPinyin

.Apply

ActiveCell.Offset(1, 0).Select

End With

End Sub.
 
Last edited by a moderator:
Upvote 0
Sub Sort_D_Then_B()

ActiveWorkbook.Worksheets("Per Capita").Sort.SortFields.Clear

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields.Add Key:=Range("D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

ActiveWorkbook.Worksheets ("Per Capita").Sort.SortFields. Add Key:=Range("B:B"), Sorton:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers

With ActiveWorkbook.Worksheets ("Per Capita").Sort

' .SetRange Rows (ActiveCells)

.Header = xlGuess

.MatchCase = False

.Orientation = xlTopToBottom

.SortMethod = xlPinyin

.Apply

ActiveCell.Offset(1, 0).Select

End With

End Sub
2022 - WCT - Real Estate.xls
BCDEFGHIJNO
32001900039U 690.71704.81775.2920800019-00-076-105-A
32101900048U 91.3293.18102.512750019-00-075-097
32201900053U 195.93199.93219.925900019-00-075-089
32301900077U 214.18218.56240.426450019-00-084-039-C
3240190007810/04/2022P123 824.53841.36925.5024830019-00-084-043-B
3250190009010/04/2022P456 142.81145.72160.294300019-00-085-035
3260190010210/04/2022P789 638.25651.28716.4119220019-00-085-040-A
32701900103U 292.22298.19328.018800019-00-074-037
32801900118U 769.41785.12863.6423170019-00-075-075
32901900119U 38.5239.3143.241160019-00-075-075-A-001
33001900123U 796.98813.24894.5624000019-00-074-043-B
33101900155U 631.25644.15708.5819010019-00-085-024-A-001
33201900168U 548.59559.78615.7616520019-00-085-027-A
33301900178U 614.34626.88689.5718500019-00-083-048
Real Estate
 
Upvote 0
updated work sheet name to sample i shared. i need to next when code works make it work for any worksheet or book im in.

VBA Code:
Sub Sort_D_Then_B()

ActiveWorkbook.Worksheets("Real Estate").Sort.SortFields.Clear

ActiveWorkbook.Worksheets("Real Estate").Sort.SortFields.Add Key:=Range("D:D"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

ActiveWorkbook.Worksheets("Real Estate").Sort.SortFields.Add Key:=Range("B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers

With ActiveWorkbook.Worksheets("Real Estate").Sort

' .SetRange Rows (ActiveCells)

.Header = xlGuess

.MatchCase = False

.Orientation = xlTopToBottom

.SortMethod = xlPinYin

.Apply

ActiveCell.Offset(1, 0).Select

End With

End Sub
 
Last edited by a moderator:
Upvote 0
Is you are selecting the range first try
VBA Code:
Sub Sort_D_Then_B()

Selection.Sort Range("D:D"), xlAscending, Range("B:B"), , xlAscending

End Sub
 
Upvote 0
Solution
Is you are selecting the range first try
VBA Code:
Sub Sort_D_Then_B()

Selection.Sort Range("D:D"), xlAscending, Range("B:B"), , xlAscending

End Sub
thanks so much! lol such a simple line of code for my long code from altering from a recording i did.

if someone needs this code the searches for it

Sub Try()

'select cell C 10,000
ActiveSheet.Range("C10000").Select

' goes up to last drawer (blank cell) posted if cell is empty in C
Selection.End(xlUp).Select
' (not needed since C should be empty) Selection.End(xlUp).Select

' highlights cells below for sorting
Rows(ActiveCell.Row).Select
Range(Selection, Selection.End(xlDown)).Select

' Sorts D then B
Selection.Sort Range("D:D"), xlAscending, Range("B:B"), , xlAscending

'select cell C 10,000
ActiveSheet.Range("C10000").Select

' goes up to last drawer posted (last cell that has info) then goes down one
Selection.End(xlUp).Offset(1, 0).Select

' Inserts new line + make bold + color line
ActiveCell.EntireRow.Insert
ActiveCell.EntireRow.Font.Bold = True
Rows(ActiveCell.Row).Select
With Selection.Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,970
Members
448,933
Latest member
Bluedbw

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