H
Howard
I offered this code to an OP, given his explanation that whenever his users leave a worksheet column B should be sorted. Then it turned out to be multiple columns that needed to be sorted (see .SetRange Range("B:R") in code).
That worked in my tests where col B through R would be sorted and col B was the "sort on col". I assume that is the default unless somehow coded differently, which I did not pursue.
Then the other shoe fell and it turns out that not every sheet in the workbook requires a sort, in fact some sheets should be excluded of any sort, and at the same time the columns to sort are different across the various sheets that require a sort.
Is there a common sense approach to this where the sheet needing NO sort can be excluded while at the same time those that require a sort can sanely be include and the columns to sort be identified?
Perhaps a Case Select...
Where:
Case = Sheet1
Do a sort on XYZ
Case = Sheet 25
Do a sort on ABC
Case Else
Don't do anything
End Case
Thanks>
Howard
Option Explicit
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
With ActiveSheet.Sort
'.SetRange Range("B:R")
.SetRange Range("B:B")
.Header = xlNo
'.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
That worked in my tests where col B through R would be sorted and col B was the "sort on col". I assume that is the default unless somehow coded differently, which I did not pursue.
Then the other shoe fell and it turns out that not every sheet in the workbook requires a sort, in fact some sheets should be excluded of any sort, and at the same time the columns to sort are different across the various sheets that require a sort.
Is there a common sense approach to this where the sheet needing NO sort can be excluded while at the same time those that require a sort can sanely be include and the columns to sort be identified?
Perhaps a Case Select...
Where:
Case = Sheet1
Do a sort on XYZ
Case = Sheet 25
Do a sort on ABC
Case Else
Don't do anything
End Case
Thanks>
Howard
Option Explicit
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
With ActiveSheet.Sort
'.SetRange Range("B:R")
.SetRange Range("B:B")
.Header = xlNo
'.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub