Sort page on open?

Kentetsu

Well-known Member
Joined
Jan 22, 2004
Messages
520
Due to the protection function, users cannot sort by descending (using filter), and this is causing some problems. So I was wondering if it were possible for the sheet to be sorted automatically whenever the spreadsheet is opened. Is this a viable solution?

The sheet is titled Summary (Sheet 1), and the range is A4:CI301 and I would like to sort based on the data in column C, in Descending order (C4 - C301).

Like I said, I'm not sure if this is the best method or not, so I am open to any suggestions that may be applicable.

You input is appreciated. Thanks...
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
This might be because * is a wildcard - try

Code:
Private Sub Workbook_Open()
Dim ShAr, RgAr, KyAr
Dim i As Integer
ShAr = Array("WK", "PH", "CM", "Tardy", "Reason", "Adherence", "Lunch adherence")
RgAr = Array("A4:D200", "A4:D200", "A4:D200", "Z", "Z", "Z", "Z")
KyAr = Array("D4", "D4", "D4", "C2", "D2", "E2", "E2")
For i = LBound(ShAr) To UBound(ShAr)
    If RgAr(i) = "Z" Then
        Sheets(ShAr(i)).UsedRange.Sort Key1:=Range(KyAr(i)), Order1:=xlDescending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Else
        Sheets(ShAr(i)).Range(RgAr).Sort Key1:=Range(KyAr(i)), Order1:=xlDescending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End If
Next i
End Sub
 
Upvote 0
Sorry to get back so late...it still throws the same error in the same location:

Sheets(ShAr(i)).Range(RgAr).Sort Key1:=Range(KyAr(i)), Order1:=xlDescending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 
Upvote 0
Try

Rich (BB code):
Private Sub Workbook_Open()
Dim ShAr, RgAr, KyAr
Dim i As Integer
ShAr = Array("WK", "PH", "CM", "Tardy", "Reason", "Adherence", "Lunch adherence")
RgAr = Array("A4:D200", "A4:D200", "A4:D200", "Z", "Z", "Z", "Z")
KyAr = Array("D4", "D4", "D4", "C2", "D2", "E2", "E2")
For i = LBound(ShAr) To UBound(ShAr)
    If RgAr(i) = "Z" Then
        Sheets(ShAr(i)).UsedRange.Sort Key1:=Range(KyAr(i)), Order1:=xlDescending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Else
        Sheets(ShAr(i)).Range(RgAr(i)).Sort Key1:=Range(KyAr(i)), Order1:=xlDescending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End If
Next i
End Sub
 
Upvote 0
Same error - same location.

It occurs to me...and it isn't my code or a skill I have, but how is your code determining the starting point for the sort on the non-ranged delimited sheets?

Example - My "tardy" sheet, if I manually sort it, requires that I choose A3:(bottom/right of the full range) currently AI224....but changes rows and columns, depending.

Thanks for the time and consideration.
 
Upvote 0
I can't reproduce that error.

The ranges are the ones that you gave me in an earlier post.
 
Upvote 0
Maybe we got messages crossed:

I really could get along with the following, (using your code)

ShAr = Array("WK", "PH", "CM", "Tardy", "Reason", "Adherence", "Lunch adherence")
RgAr = Array("A4:D200", "A4:D200", "A4:D200", "C3:AV500", "A3:IV500", "A3:IV500", "A3:IV500")
KyAr = Array("D4", "D4", "D4", "G2", "D2", "E2", "E2")

I'm not sure how to manipulate the If-Then-Else to accomodate the change, or if it would even make a difference.

Thanks
 
Upvote 0
I think I've found my error :oops:

Try

Code:
Sub test2()
Dim ShAr, RgAr, KyAr
Dim i As Integer
ShAr = Array("WK", "PH", "CM", "Tardy", "Reason", "Adherence", "Lunch adherence")
RgAr = Array("A4:D200", "A4:D200", "A4:D200", "Z", "Z", "Z", "Z")
KyAr = Array("D4", "D4", "D4", "C2", "D2", "E2", "E2")
For i = LBound(ShAr) To UBound(ShAr)
    If RgAr(i) = "Z" Then
        Sheets(ShAr(i)).UsedRange.Sort Key1:=Sheets(ShAr(i)).Range(KyAr(i)), Order1:=xlDescending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Else
        Sheets(ShAr(i)).Range(RgAr(i)).Sort Key1:=Sheets(ShAr(i)).Range(KyAr(i)), Order1:=xlDescending, _
        Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End If
Next i
End Sub
 
Upvote 0
Success!!

I have tested and re-tested and I believe that I have exactly what I needed.

Thank you for your patience and tenacity.

Sincerely,

Jon
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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