Private Sub CommandButtonTestCode_Click()
Dim c As String * 1
c = "a"
Dim iCell As Range
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim RowTop As Integer
Dim RowBot As Integer
'Dim FirstPrintRow As Long
'Dim LastPrintRow As Long
Dim LastUsedRow As Long
Set ws1 = Sheet1
Set ws = Sheet6
Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
'FirstPrintRow = ws.Range(ws.PageSetup.PrintArea).Row
'LastPrintRow = ws.Range(ws.PageSetup.PrintArea).Row + ws.Range(ws.PageSetup.PrintArea).Rows.Count - 1
For Each iCell In Range("A2:CG31")
With iCell
If iCell.Interior.ColorIndex = 35 And iCell.Value > 0 Then
Lastrow = Lastrow + 1
RowTop = Lastrow
ws.Cells(Lastrow + 1, 1).Value = iCell.Value & ws1.Cells(10, 85).Text
ws1.Cells(10, 85).Value = ws1.Cells(10, 85).Value + 1
ws.Cells(Lastrow + 1, 1).Offset(0, 1).Value = iCell.Offset(0, 1).Value
ws.Cells(Lastrow + 1, 1).Offset(0, 1).Font.Size = 12
ws.Cells(Lastrow + 1, 1).Offset(0, 1).Font.Bold = True
Lastrow = Lastrow + 1
ElseIf iCell.Value > 0 And iCell.Interior.ColorIndex = 3 Then
Sheets(1).Cells(iCell.Row, 81) = Sheets(1).Cells(iCell.Row, 81) + 1
ws.Cells(Lastrow + 1, 2).Value = c
c = Chr(Asc(c) + 1)
ws.Cells(Lastrow + 1, 3).Value = Sheets(1).Cells(1, iCell.Column).Value
ws.Cells(Lastrow + 1, 10).Value = iCell.Value
ws.Cells(Lastrow + 1, 9).Value = "="
Lastrow = Lastrow + 1
ElseIf iCell.Value <> "" And iCell.Interior.ColorIndex = 24 Then
Sheets(1).Cells(iCell.Row, 82) = Sheets(1).Cells(iCell.Row, 82) + 1
ws.Cells(Lastrow + 1, 2).Value = c
c = Chr(Asc(c) + 1)
ws.Cells(Lastrow + 1, 3).Value = Sheets(1).Cells(1, iCell.Column).Value
ws.Cells(Lastrow + 1, 6).Value = iCell.Value
ws.Cells(Lastrow + 1, 6).Cells.HorizontalAlignment = xlHAlignCenter
ws.Cells(Lastrow + 1, 7).Value = "x"
ws.Cells(Lastrow + 1, 8).Value = iCell.Offset(0, 1).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignCenter
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10) = ws.Cells(Lastrow + 1, 6) * ws.Cells(Lastrow + 1, 8)
Lastrow = Lastrow + 1
If ws1.Cells(iCell.Row - 1, 80).Value > 0 And ws1.Cells(iCell.Row, 79).Value > ws1.Cells(iCell.Row - 1, 80).Value Then
ws.Rows(LastUsedRow + 2).PageBreak = xlPageBreakManual
End If
ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 40 Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(1, iCell.Column).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10).Value = iCell.Value 'move subtotal
ws.Cells(Lastrow + 1, 10).Borders(xlEdgeTop).LineStyle = xlContinuous
Lastrow = Lastrow + 1
ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 46 Then
If Sheets(1).Cells(iCell.Row, 80).Value = "" Then
Sheets(1).Cells(iCell.Row, 80).Value = Sheets(1).Cells(1, iCell.Column).Value
Else
Sheets(1).Cells(iCell.Row, 80).Value = Sheets(1).Cells(iCell.Row, 80).Value & ", " & Sheets(1).Cells(1, iCell.Column).Value
End If
ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 48 Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(1, iCell.Column).Value & " " & Sheets(1).Cells(iCell.Row, 80).Value & " (" & iCell.Text & ")"
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 9).Value = "-"
ws.Cells(Lastrow + 1, 10).Value = iCell.Offset(0, 1).Value
Lastrow = Lastrow + 1
ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 45 Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(1, iCell.Column).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10).Value = iCell.Value 'move room total
Lastrow = Lastrow + 1
ElseIf iCell.Interior.ColorIndex = 19 Then
LastUsedRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If (Sheets(1).Cells(iCell.Row + 1, 1).Value > "A" Or Sheets(1).Cells(iCell.Row + 1, 1) = "") And Sheets(1).Cells(iCell.Row, 1).Value = "A" Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(1, 85).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 8).Font.Bold = True
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10).Value = Sheets(1).Cells(2, 85).Value
ws.Cells(Lastrow + 1, 10).Font.Bold = True
ws1.Cells(10, 85).Value = 1
c = "a"
Lastrow = Lastrow + 1
ws.Rows(LastUsedRow + 2).PageBreak = xlPageBreakManual
ElseIf (Sheets(1).Cells(iCell.Row + 1, 1).Value > "B" Or Sheets(1).Cells(iCell.Row + 1, 1) = "") And Sheets(1).Cells(iCell.Row, 1).Value = "B" Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(3, 85).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 8).Font.Bold = True
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10).Value = Sheets(1).Cells(4, 85).Value
ws.Cells(Lastrow + 1, 10).Font.Bold = True
ws1.Cells(10, 85).Value = 1
c = "a"
Lastrow = Lastrow + 1
ws.Rows(LastUsedRow + 2).PageBreak = xlPageBreakManual
ElseIf (Sheets(1).Cells(iCell.Row + 1, 1).Value > "C" Or Sheets(1).Cells(iCell.Row + 1, 1) = "") And Sheets(1).Cells(iCell.Row, 1).Value = "C" Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(5, 85).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 8).Font.Bold = True
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10).Value = Sheets(1).Cells(6, 85).Value
ws.Cells(Lastrow + 1, 10).Font.Bold = True
ws1.Cells(10, 85).Value = 1
c = "a"
ws1.Cells(10, 81).Value = ws1.Cells(10, 81).Value + 1
Lastrow = Lastrow + 1
ws.Rows(LastUsedRow + 2).PageBreak = xlPageBreakManual
ElseIf Sheets(1).Cells(iCell.Row, 1).Value = "D" And Sheets(1).Cells(iCell.Row + 1, 1) = "" Then
ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(7, 85).Value
ws.Cells(Lastrow + 1, 8).Cells.HorizontalAlignment = xlHAlignRight
ws.Cells(Lastrow + 1, 8).Font.Bold = True
ws.Cells(Lastrow + 1, 9).Value = "="
ws.Cells(Lastrow + 1, 10).Value = Sheets(1).Cells(8, 85).Value
ws.Cells(Lastrow + 1, 10).Font.Bold = True
ws1.Cells(10, 85).Value = 1
c = "a"
Lastrow = Lastrow + 1
ws.Rows(LastUsedRow + 2).PageBreak = xlPageBreakManual
End If
RowBot = LastUsedRow
c = "a"
ws1.Cells(iCell.Row, 86).Value = RowTop
ws1.Cells(iCell.Row, 87).Value = RowBot
ws1.Cells(iCell.Row, 90).Value = PageB
End If
End With
Next iCell
With ws.Cells
ws.Columns("G").Cells.HorizontalAlignment = xlHAlignCenter
ws.Columns("I").Cells.HorizontalAlignment = xlHAlignCenter
ws.Columns("A").Cells.HorizontalAlignment = xlHAlignLeft
ws.Columns("J").NumberFormat = "$#,##0.00"
End With
End Sub