[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Sub ShiftPattern()[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] Dim ws As Worksheet
Dim iLast As Integer
Dim i As Integer
Dim j As Integer
Dim iRow As Integer
Dim iWidth As Integer
Dim sCol As String
Set ws = ThisWorkbook.Sheets(1)
ws.UsedRange.Offset(0, 1).ClearContents
iLast = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
iWidth = (iLast - 1) / 5
If iLast Mod 5 <> 1 Then
MsgBox vbCrLf & "The input data is not in multiples of five!" & vbCrLf & vbCrLf _
& "Please adjust the data and re-run this macro." & Space(10), vbOKOnly + vbExclamation
Exit Sub
End If
For i = 2 To iLast - 1
For j = i + 1 To iLast
If ws.Cells(i, "A") > ws.Cells(j, "A") Then
ws.Cells(i, "A").Copy ws.Range("B1")
ws.Cells(j, "A").Copy ws.Cells(i, "A")
ws.Range("B1").Copy ws.Cells(j, "A")
End If
Next j
Next i
ws.Range("B1").ClearContents
For iRow = 2 To iLast
i = Int((iRow - 2) / iWidth) + 2
j = ((iRow - 2) Mod iWidth) + 3
If i Mod 2 = 1 Then j = iWidth + 5 - j
ws.Cells(iRow, "A").Copy ws.Cells(i, j)
If i = 2 Then
ws.Cells(1, j) = "#" & CStr(j - 2)
ws.Cells(1, j).Font.Bold = True
sCol = Replace(Cells(1, j).Address(1, 0), "$1", "")
ws.Cells(8, j) = "=SUM(" & sCol & "2:" & sCol & "6)"
ws.Cells(8, j).Font.Bold = True
End If
Next iRow
End Sub
[/FONT]