Hello Xelpme,
This macro sorts the data first to group all the same numbers together. It creates a table for each set of numbers and their totals (Price and Cost). The totals are in bold, and each table is seperated by 2 blank lines. See the attachment for how the table is formatted. After you insert a Standard VBA Module into your workbook, copy and paste the macro code into it. You can then assign it to a command button or run it using ALT+F8. The worksheet names in the macro Sheet1 and Sheet2. Sheet1 has the data, and Sheet2 is were the tables are created. Things can be changed to the names you are using. It looks long because of all the formatting code, but it works quite fast.
'Author: Leith Ross
Option Explicit
Sub CreateTotalTables()
Dim HeaderRng As Range
Dim LastRowSrc As Long
Dim NewRowDst As Long
Dim R As Long
Dim TopRowDst As Long
Dim TotalCost As Variant
Dim TotalPrice As Variant
Dim WksSrc As Worksheet
Dim WksDst As Worksheet
Set WksSrc = Worksheets("Sheet1")
Set WksDst = Worksheets("Sheet2")
WksDst.UsedRange.Clear
With WksSrc
.Activate
Set HeaderRng = .Range("A1:E1")
LastRowSrc = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Range(.Cells(2, "A"), .Cells(LastRowSrc, "E"))
.Sort Key1:=.Cells(1, "A")
End With
End With
R = 1
NewRowDst = 1
GoTo AddHeader
Do While R < LastRowSrc + 1
NewRowDst = NewRowDst + 1
WksSrc.Range("A" & R).Resize(1, 5).Copy Destination:=WksDst.Range("A" & NewRowDst)
'Sum the totals for Price and Cost
TotalPrice = TotalPrice + WksSrc.Cells(R, "D")
TotalCost = TotalCost + WksSrc.Cells(R, "E")
If WksSrc.Cells(R, "A") <> WksSrc.Cells(R + 1, "A") Then
WksSrc.Range("A" & R).Resize(1, 5).Copy Destination:=WksDst.Range("A" & NewRowDst)
NewRowDst = NewRowDst + 1
'Add inside column lines
With WksDst.Range("A" & TopRowDst & ":E" & NewRowDst - 1).Borders(xlInsideVertical)
.Weight = xlThin
.LineStyle = xlContinuous
End With
'Add border around table
With WksDst.Range("A" & TopRowDst & ":E" & NewRowDst)
.BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
End With
'Add double line above totals
With WksDst.Range("A" & NewRowDst).Resize(1, 5)
With .Borders(xlEdgeTop)
.Weight = xlThin
.LineStyle = xlDouble
End With
'Display totals in Bold
.Font.Bold = True
.Cells(1, 2).Value = "Totals"
.Cells(1, 4).Value = Format(TotalPrice, "currency")
.Cells(1, 5).Value = Format(TotalCost, "currency")
End With
'Add separator lines between total columns
With WksDst.Range("D" & NewRowDst)
With .Borders(xlEdgeLeft)
.Weight = xlThin
.LineStyle = xlContinuous
End With
With .Borders(xlEdgeRight)
.Weight = xlThin
.LineStyle = xlContinuous
End With
End With
TotalCost = 0
TotalPrice = 0
NewRowDst = NewRowDst + 3
AddHeader:
'Add bottom border to header labels
HeaderRng.Copy Destination:=WksDst.Range("A" & NewRowDst)
With WksDst.Range("A" & NewRowDst).Resize(1, 5).Borders(xlEdgeBottom)
.Weight = xlMedium
.LineStyle = xlContinuous
End With
TopRowDst = NewRowDst
End If
R = R + 1
Loop
'Delete last header
WksDst.Range("A" & NewRowDst).Resize(1, 5).Clear
End Sub
Sincerely,
Leith Ross
Bookmarks