Sorting Trouble

wtnelso

Board Regular
Joined
Feb 15, 2012
Messages
241
Hello, I'm having trouble sorting the data on multiple worksheets in a work book. I want to cycle through each WS in my array and sort the data in column A:A.

Code:
Dim WS As Variant
For Each WS In Array("FY07 Report", "FY08 Report", "FY09 Report")
Worksheets("" & WS & "").Range("A:A").Sort.SortFields.Add order1:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom, SortMethod:=xlPinYin
Next

The error say: "Unable to get the sort property of the range class".

Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
wtnelso,


Sample worksheets:


Excel Workbook
A
1Header
210
39
48
57
66
75
8
FY07 Report





Excel Workbook
A
1Header
210
39
48
57
66
75
84
93
102
11
FY08 Report





Excel Workbook
A
1Header
210
39
48
57
66
75
84
93
102
111
12
FY09 Report





After the macro:


Excel Workbook
A
1Header
25
36
47
58
69
710
8
FY07 Report





Excel Workbook
A
1Header
22
33
44
55
66
77
88
99
1010
11
FY08 Report





Excel Workbook
A
1Header
21
32
43
54
65
76
87
98
109
1110
12
FY09 Report





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub SortSheets()
' hiker95, 06/19/2012
' http://www.mrexcel.com/forum/showthread.php?641525-Sorting-Trouble
Dim WSary As Variant, ws As Worksheet, i As Long, lr As Long
Application.ScreenUpdating = False
WSary = Array("FY07 Report", "FY08 Report", "FY09 Report")
For i = LBound(WSary) To UBound(WSary)
  Set ws = Worksheets(WSary(i))
  lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
  ws.Range("A2:A" & lr).Sort key1:=ws.Range("A2"), order1:=1
Next i
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the SortSheets macro.
 
Upvote 0

Forum statistics

Threads
1,203,749
Messages
6,057,145
Members
444,908
Latest member
Jayrey

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