For L = 1 To 5
Sh = XArray(1, L) ' sheet to be sorted
r1 = XArray(2, L) ' first row in whole matrix (col in PREORDER
rL = XArray(4, L) ' last row in active matrix (col in PREORDER
c1 = XArray(5, L) ' first col in whole matrix (row in PREORDER)
cL = XArray(6, L) ' last col in whole matrix (row in PREORDER)
c2 = XArray(7, L) ' col of Supply Code (row in PREORDER)
c3 = XArray(8, L) ' col for retailer names (row in PREORDER)
c4 = XArray(9, L) ' col of Face Values (row in PREORDER)
cT = XArray(15, L) ' col of Type Retailer (N/A in INV ACC, ORDER FORM or PREORDER)
B = XArray(16, L) ' Boolean value to use in orientation
If L < 5 Then
With ActiveWorkbook.Worksheets(Sh)
Set Range1 = .Range(.Cells(r1, c1), .Cells(rL, cL)) ' Entire range to be sorted
Set Range2 = .Range(.Cells(r1, c2), .Cells(rL, c2)) ' supply code
Set Range3 = .Range(.Cells(r1, c3), .Cells(rL, c3)) ' retailer name
Set Range4 = .Range(.Cells(r1, c4), .Cells(rL, c4)) ' product value
Set Range5 = .Range(.Cells(r1, cT), .Cells(rL, cT)) ' Type of retailer
End With
orient = 2 ' top to bottom
End If
If L = 5 Then
With ActiveWorkbook.Worksheets(Sh)
Set Range1 = .Range(.Cells(c1, r1), .Cells(cL, rL)) ' Entire range to be sorted
Set Range2 = .Range(.Cells(c2, r1), .Cells(c2, rL)) ' supply code
Set Range3 = .Range(.Cells(c3, r1), .Cells(c3, rL)) ' retailer name
Set Range4 = .Range(.Cells(c4, r1), .Cells(c4, rL)) ' product value
End With
orient = 1 'left to right
End If
Select Case S
Case 1 ' Sort by Name, product Value only
ActiveWorkbook.Worksheets(Sh).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(Sh).Sort.SortFields.Add Key:=Range3, SortOn:=xlSortOnValues, order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(Sh).Sort.SortFields.Add Key:=Range4, SortOn:=xlSortOnValues, order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(Sh).Sort
.SetRange Range1
.Header = xlNo
.MatchCase = False
.Orientation = orient
.SortMethod = xlPinYin
.Apply
End With
Two other cases follow with the same structure.