Private Function UniqueValues(arr)
Dim ret() As String
ReDim ret(1 To 1)
ret(1) = arr(1)
For i = 2 To UBound(arr)
test = False
For j = 1 To UBound(ret)
If arr(i) = ret(j) Then
test = True
Exit For
End If
Next j
If test = False Then
ReDim Preserve ret(1 To UBound(ret) + 1)
ret(UBound(ret)) = arr(i)
End If
Next i
UniqueValues = ret
End Function
Sub rearrangeDataInReportFormat()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim lastRow As Integer
Set dataSheet = ThisWorkbook.Worksheets("Data")
lastRow = dataSheet.Cells(Rows.count, 7).End(xlUp).Row
Set Rng = dataSheet.Range("G2:M" & lastRow)
Dim orderArr() As Variant
Dim itemArr() As Variant
ReDim orderArr(1 To Rng.Rows.count)
ReDim itemArr(1 To Rng.Rows.count)
For i = 1 To Rng.Rows.count
orderArr(i) = Rng.Cells(i, 1).Value
For j = 2 To 5
itemArr(i) = itemArr(i) & "," & Rng.Cells(i, j).Value
Next j
itemArr(i) = Right(itemArr(i), Len(itemArr(i)) - 1)
Next i
unqOrderArr = UniqueValues(orderArr)
unqItemArr = UniqueValues(itemArr)
Dim qnCount() As Integer
ReDim qnCount(1 To UBound(unqItemArr))
For i = LBound(itemArr) To UBound(itemArr)
For j = LBound(unqItemArr) To UBound(unqItemArr)
If itemArr(i) = unqItemArr(j) Then
qnCount(j) = qnCount(j) + Rng.Cells(i, Rng.Columns.count).Value
Exit For
End If
Next j
Next i
Dim itemsPerOrder() As Integer
Dim countPerItem() As Integer
Dim sumPerItem() As Integer
ReDim itemsPerOrder(1 To UBound(unqOrderArr))
ReDim countPerItem(1 To UBound(unqItemArr))
ReDim sumPerItem(1 To UBound(unqItemArr))
For i = LBound(unqOrderArr) To UBound(unqOrderArr)
For j = LBound(orderArr) To UBound(orderArr) - 1
If unqOrderArr(i) = orderArr(j) Then
If itemArr(j) <> itemArr(j + 1) Then
itemsPerOrder(i) = itemsPerOrder(i) + 1
End If
End If
Next j
Next i
itemsPerOrder(UBound(itemsPerOrder)) = itemsPerOrder(UBound(itemsPerOrder)) + 1
For i = LBound(unqItemArr) To UBound(unqItemArr)
For j = LBound(itemArr) To UBound(itemArr)
If unqItemArr(i) = itemArr(j) Then
countPerItem(i) = countPerItem(i) + Rng.Cells(j, Rng.Columns.count).Value
sumPerItem(i) = sumPerItem(i) + Rng.Cells(j, Rng.Columns.count - 1).Value * Rng.Cells(j, Rng.Columns.count).Value
End If
Next j
Next i
Set reportSheet = ThisWorkbook.Worksheets("Report")
Dim cRow As Integer
Dim dsRow As Integer
Dim cpiIndex As Integer
Dim arr() As String
cRow = 2
dsRow = 1
cpiIndex = 1
For i = LBound(unqOrderArr) To UBound(unqOrderArr)
reportSheet.Cells(cRow, 1) = i
reportSheet.Cells(cRow, 2) = unqOrderArr(i)
cRow = cRow + 1
For j = 1 To itemsPerOrder(i)
For k = 1 To 4
arr = Split(unqItemArr(cpiIndex), ",")
reportSheet.Cells(cRow, k + 1) = arr(k - 1)
Next k
reportSheet.Cells(cRow, 6) = "cm"
reportSheet.Cells(cRow, 12) = countPerItem(cpiIndex)
reportSheet.Cells(cRow, 13) = sumPerItem(cpiIndex)
Dim count As Integer
Dim itmCount As Integer
count = 0
itmCount = 0
While itmCount < countPerItem(cpiIndex)
For k = 1 To 5
If Rng.Cells(dsRow, Rng.Columns.count).Value <= 10 Then
reportSheet.Cells(cRow, k + 6).Value = Rng.Cells(dsRow, Rng.Columns.count - 1).Value
count = count + 1
itmCount = itmCount + 1
If count = Rng.Cells(dsRow, Rng.Columns.count).Value Then
count = 0
dsRow = dsRow + 1
End If
If itmCount Mod 5 = 0 Then
cRow = cRow + 1
End If
If itmCount = countPerItem(cpiIndex) Then
Exit For
End If
Else
reportSheet.Range(Cells(cRow, 7), Cells(cRow, 8)).Merge
reportSheet.Cells(cRow, 7) = Rng.Cells(dsRow, Rng.Columns.count - 1).Value & " x " & Rng.Cells(dsRow, Rng.Columns.count).Value
k = k + 1
itmCount = itmCount + Rng.Cells(dsRow, Rng.Columns.count).Value
dsRow = dsRow + 1
End If
Next k
Wend
cpiIndex = cpiIndex + 1
cRow = cRow + 1
Next j
Next i
reportSheet.Range(Cells(cRow + 1, 1), Cells(cRow + 1, 11)).Merge
reportSheet.Cells(cRow + 1, 1).Value = "Total"
For i = 3 To cRow - 1
reportSheet.Cells(cRow + 1, 12).Value = reportSheet.Cells(cRow + 1, 12).Value + reportSheet.Cells(i, 12).Value
reportSheet.Cells(cRow + 1, 13).Value = reportSheet.Cells(cRow + 1, 13).Value + reportSheet.Cells(i, 13).Value
Next i
End Sub