Sort Data from Largest to smallest

bassemjohn1

Board Regular
Joined
Jun 26, 2020
Messages
51
Office Version
  1. 2016
Platform
  1. Windows
Hello All

so I need to sort my numbers in column K from Largest to smallest, the problem is I have another header in row 13 and it changes every month, what is the right VBA code to filter just the numbers and sort it from largest to smallest without messing with my headers in row 1 and 13.

1542001 - 2364001 Recon June- final 5.XLSX
KN
1Amount in local currencyEff.exchange rate
2-321.8740.37706
3-1,630.1110.37701
4-524.1250.37701
5-2,742.8190.37700
6-12,430.3790.37700
7-396.4470.37700
8672.4481.00000
9
10
11
12
13Amount in local currencyEff.exchange rate
141,630.1110.37701
15321.8741.00000
16524.1890.37706
172,742.8190.37700
1812,442.9800.37738
19396.5130.37706
BHR - BHC
 
In column L, I think you might need separate formulas for the top section and bottom section.

bassemjohn1.xlsm
KLM
1Amount in local currencyRecon.Match
2-51.0251 - 1x
3-2092.3722092 - 1x
4-149.34149 - 1x
5-83738373 - 1 
6-12.45512 - 1 
7-1677.061677 - 1x
8-121.843121 - 1x
9-345.16345 - 1 
10-2978.7552978 - 1x
11-149.46149 - 2x
12-703.852703 - 1x
13-627.188627 - 1x
14-3216.4483216 - 1x
15-3517.5463517 - 1x
16-1241.3131241 - 1x
17-117.32117 - 1x
18-77.24977 - 1x
19-965.334965 - 1x
20-834.041834 - 1x
21-77.24877 - 2x
22-37.69837 - 1x
23 
24 
25 
26 
27Amount in local currency 
2851.0251 - 1x
292092.3722092 - 1x
30149.34149 - 1x
31121.84121 - 1x
322978.7552978 - 1x
33149.46149 - 2x
34703.852703 - 1x
35627.188627 - 1x
368385.4558385 - 1 
373216.4483216 - 1x
383517.5463517 - 1x
391241.3131241 - 1x
401677.061677 - 1x
41117.32117 - 1x
4277.24977 - 1x
43965.334965 - 1x
44834.041834 - 1x
4577.24977 - 2x
4637.69837 - 1x
Sheet3
Cell Formulas
RangeFormula
L2:L22L2=INT(ABS(K2))&" - "&COUNTIF(L$1:L1,INT(ABS(K2))&" -*")+1
M2:M46M2=IF(COUNTIF($L$2:$L$292,L2)=2,"x","")
L28:L46L28=INT(K28)&" - "&COUNTIF(L$27:L27,INT(K28)&" -*")+1
Worked perfectly, the only problem is the report changes monthly and it won't be L28 every time. the range changes.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If you are still using a macro to sort the two sections then the macro could work out the ranges and insert the relevant formulas.
 
Upvote 0
If you are still using a macro to sort the two sections then the macro could work out the ranges and insert the relevant formulas.
Would you please explain how?
if am using a macro and stating in my L formula that INT=(K28) then the macro would catch K28 every time.
 
Upvote 0
Would you please explain how?
Try this with a copy of your data.

VBA Code:
Sub SortPlusFormulas()
  Dim rng1 As Range, rng2 As Range
  
  Set rng1 = Columns("K").SpecialCells(xlConstants, xlNumbers).Areas(1)
  Set rng2 = Columns("K").SpecialCells(xlConstants, xlNumbers).Areas(2)
  Intersect(rng1.EntireRow, Columns("A:Z")).Sort Key1:=rng1, Order1:=xlAscending, Header:=xlNo
  Intersect(rng2.EntireRow, Columns("A:Z")).Sort Key1:=rng2, Order1:=xlAscending, Header:=xlNo
  rng1.Offset(, 1).Resize(, 2).Formula = Array( _
    "=INT(ABS(K2))&"" - ""&COUNTIF(L$1:L1,INT(ABS(K2))&"" -*"")+1", _
    "=IF(COUNTIF($L$2:$L$" & rng2.Row + rng2.Rows.Count - 1 & ",L2)=2,""x"","""")")
  rng2.Offset(, 1).Resize(, 2).Formula = Array( _
    Replace(Replace("=INT(K#)&"" - ""&COUNTIF(L$%:L%,INT(K#)&"" -*"")+1", "#", rng2.Row), "%", rng2.Row - 1), _
    "=IF(COUNTIF($L$2:$L$" & rng2.Row + rng2.Rows.Count - 1 & ",L" & rng2.Row & ")=2,""x"","""")")
End Sub
 
Upvote 0
You're welcome. :)

You could also consider this which puts a slightly more efficient formula in column L

VBA Code:
Sub SortPlusFormulas_v2()
  Dim rng1 As Range, rng2 As Range
  
  Set rng1 = Columns("K").SpecialCells(xlConstants, xlNumbers).Areas(1)
  Set rng2 = Columns("K").SpecialCells(xlConstants, xlNumbers).Areas(2)
  Intersect(rng1.EntireRow, Columns("A:Z")).Sort Key1:=rng1, Order1:=xlAscending, Header:=xlNo
  Intersect(rng2.EntireRow, Columns("A:Z")).Sort Key1:=rng2, Order1:=xlAscending, Header:=xlNo
  rng1.Offset(, 1).Resize(, 2).Formula = Array( _
    "=INT(ABS(K2))&"" - ""&COUNTIF(L$1:L1,INT(ABS(K2))&"" -*"")+1", _
    "=IF(ISNUMBER(MATCH(L2," & rng2.Offset(, 1).Address & ",0)),""x"","""")")
  rng2.Offset(, 1).Resize(, 2).Formula = Array( _
    Replace(Replace("=INT(K#)&"" - ""&COUNTIF(L$%:L%,INT(K#)&"" -*"")+1", "#", rng2.Row), "%", rng2.Row - 1), _
    "=IF(ISNUMBER(MATCH(L" & rng2.Row & "," & rng1.Offset(, 1).Address & ",0)),""x"","""")")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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