Excel VBA sort entire Sheet with 3 keys

OlivierS

New Member
Joined
May 29, 2014
Messages
28
Hi,

i'm having trouble when running a Macro to:
- select my sheet (called SFDC)
- select all data
- sort all data ascendant using columns Account Name then Community then Status

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
What Columns are you using?

Is your "Account Name" column A, "Community" column B and "Status" Column C?
 
Upvote 0
Assuming you are using Columns A, B and C, this should work for you:

Code:
Sub Macrosort()

Dim Ws As Worksheet, Rngsort As Range, RngKey As Range, RngKey2 As Range, RngKey3 As Range

'Populate Ws
Set Ws = ActiveWorkbook.Worksheets("SFDC")

'Clear out any previous Sorts that may be leftover
Ws.Sort.SortFields.Clear

'range that includes all columns to sort
Set Rngsort = Ws.UsedRange

'Columns with keys to sort
Set RngKey = Ws.Range("A1")
Set RngKey2 = Ws.Range("B1")
Set RngKey3 = Ws.Range("C1")


'Perform the sort
With ActiveWorkbook.Worksheets("SFDC").Sort
    Rngsort.Sort Key1:=RngKey, Order1:=xlAscending, Header:=xlYes, OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, Orientation:=xlSortColumns, DataOption1:=xlSortNormal
    Rngsort.Sort Key1:=RngKey2, Order1:=xlAscending, Header:=xlYes, OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, Orientation:=xlSortColumns, DataOption1:=xlSortNormal
    Rngsort.Sort Key1:=RngKey3, Order1:=xlAscending, Header:=xlYes, OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, Orientation:=xlSortColumns, DataOption1:=xlSortNormal


End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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