VBA to Sort Multiple Sheets in Excel Workbook with different criteria

lynnydd76

New Member
Joined
Dec 9, 2009
Messages
15
Hi All,

I'm trying to get a macro to perform the sort function across multiple sheets to reduce click count.

The columns and header rows don't change but when I record the macro, it never works on the next workbook.

I want to run it from copies of the same workbook so it needs to point to the active workbook.

This is what I need it to do;

Sheet 1 (Contacts):
Sort range A3:A1000 by columns A, then B (all ascending)

Sheet 2 (Centres):
Sort range A8:A1000 by columns B, then C, then A (all ascending)

Sheet 3 (Cases):
Sort range A4:A1000 by columns A, then B (all ascending)

Sheet 4 (Requests):
Sort range A4:A1000 by columns A, then B, then G (all ascending)

Any help would be appreciated, I'm OK at amending recorded macros with the basics but not writing from scratch.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
See if this will work

Code:
Sub Sorter()
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
Set sh1 = Sheets("Contacts")
Set sh2 = Sheets("Centres")
Set sh3 = Sheets("Cases")
Set sh4 = Sheets("Requests")
sh1.Range("A3:B1000").Sort sh1.Range("A3"), xlAscending, sh1.Range("B3"), , xlAscending
sh2.Range("A8:C1000").Sort sh2.Range("B8"), xlAscending, sh2.Range("C8"), , xlAscending, sh2.Range("A8"), xlAscending
sh3.Range("A4:B1000").Sort sh3.Range("A4").xlAscending, sh3.Range("B4"), , xlAscending
sh4.Range("A4:C1000").Sort sh4.Range("A4").xlAscending, sh4.Range("B4"), , xlAscending, sh4.Range("C4"), xlAscending
End Sub

This will run in the same workbook that the sheets are in. It will not work to run it from a single workbook for sheets in different workbooks.
 
Last edited:
Upvote 0
Thanks JLGWhiz, I can't get that to work.

The commas (, , vs , ) and dots (.xlascending vs xlascending) are inconsistent across the last section. Which way should they be?

Cheers, Lynette
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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