Sort Multiple Columns Using VBA

bisel

Board Regular
Joined
Jan 4, 2010
Messages
233
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a worksheet and have created this VBA to sort a range with two sort keys. First in descending order on Range N8 as the first key ...

Code:
Sub sort_ruldown()


Application.ScreenUpdating = False


    Sort.SortFields.Clear
    With Sort
        .SortFields.Add Key:=Range("N8"), Order:=xlDescending
        .SortFields.Add Key:=Range("R8"), Order:=xlDescending
        .SetRange Range("rescomp_table")
        .Header = xlYes
        .Apply
    End With
    




End Sub

... and similarly this routine to sort on Range N8 in ascending order ...

Code:
Sub sort_rulup()


Application.ScreenUpdating = False


    Sort.SortFields.Clear
    With Sort
        .SortFields.Add Key:=Range("N8"), Order:=xlAscending
        .SortFields.Add Key:=Range("R8"), Order:=xlDescending
        .SetRange Range("rescomp_table")
        .Header = xlYes
        .Apply
    End With
       
    
End Sub

The sorts perform, however, it is not picking up on the second key ... Range R8. Does anyone have a thought on why?

Thanks,

Steve
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I don't know if this is the solution to the problem (I couldn't see anything different to some sorting code I have) but you need to turn ScreenUpdating back on after your primary code finishes running, so add this line
Code:
Application.ScreenUpdating = True
after the End With line.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
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