Sort Multiple Columns Using VBA

bisel

Active Member
Joined
Jan 4, 2010
Messages
257
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,225,749
Messages
6,186,802
Members
453,373
Latest member
Ereha

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