Option Explicit
Public rng As Range
Public ws As Worksheet, sht As Worksheet
Public Item, Val, Header
Public salesperson As String, customer As String
Public SP As Object
Public Tot As Double
Public Hdr As Boolean
Public r As Long, col As Long, rw As Long, lrow As Long, sr As Long, i As Long, diff As Long, ii As Long, _
lr As Long, numrows As Long, nrow As Long
Sub Create_Report()
Application.ScreenUpdating = False
Call Extract_Information
For Each Item In SP
Hdr = False
For col = 7 To 14 Step 2
With Sheet1
.Activate
lrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
With .Range("A6:N" & lrow)
.AutoFilter col, Item
End With
numrows = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
If numrows > 0 Then
If numrows = 1 Then
sr = .Range("A" & Rows.Count).End(xlUp).Row
Else
sr = .Range("A7", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(12).Row
End If
lr = .Range("A" & Rows.Count).End(xlUp).Row
Set rng = Union(.Range("B" & sr & ":F" & lr).SpecialCells(12), .Range(.Cells(sr, col + 1), .Cells(lr, col + 1)))
If Not WorksheetExists(Item) Then
Set ws = Sheets.Add
ws.Name = Item
End If
With ws
If Hdr = False Then
.Range("A1") = Item: .Range("A1:E1").MergeCells = True: .Range("A1:E1").HorizontalAlignment = xlCenter
.Range("A2") = "Commission Report": .Range("A2:E2").MergeCells = True: .Range("A2:E2").HorizontalAlignment = xlCenter
.Range("A3") = Worksheets("Sheet1").Range("H2"): .Range("A3:E3").MergeCells = True: .Range("A3:E3").HorizontalAlignment = xlCenter
.Range("A1").Resize(3).Font.Bold = True
With .Range("A5").Resize(, 5)
.Value = Header
.Font.Bold = True
.Font.ColorIndex = 55
.Borders(xlEdgeBottom).Weight = xlThick
.Borders(xlEdgeBottom).ColorIndex = 55
End With
Hdr = True
End If
nrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
rng.Copy
.Range("A" & nrow).PasteSpecial xlPasteAll
.Range("A" & nrow).Font.Bold = True
End With
End If
.AutoFilterMode = False
End With
Next col
Call sheetfix
Next Item
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Sub sheetfix()
Set sht = ws
With sht
.Activate
Tot = 0: lrow = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A5:A" & lrow).Font.Bold = True
With .UsedRange.Rows
Range(.Cells(5, 1), .Cells(.Count, 1)).AdvancedFilter xlFilterCopy, , .Range("P1"), True
With .Range("P1").CurrentRegion: Val = .Value: .Clear: End With
End With
For i = 2 To UBound(Val)
.Range("A5:F" & lrow).AutoFilter 1, Val(i, 1)
numrows = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
If numrows = 1 Then
sr = .Range("A" & Rows.Count).End(xlUp).Row
Else
sr = .Range("A6", .Range("A" & Rows.Count).End(xlUp)).SpecialCells(12).Row
End If
lr = .Range("A" & Rows.Count).End(xlUp).Row: diff = lr - sr
If numrows > 1 Then
.Rows(lr + diff).Insert
.Range("E" & sr & ":E" & lr).Cut Destination:=.Range("A" & sr + 1)
For ii = 1 To diff + 1
.Range("A" & sr + ii) = Space(5) & .Range("A" & sr + ii)
Next ii
.Range("F" & sr & ":F" & lr).Cut Destination:=.Range("E" & sr)
.Range("B" & sr & ":E" & lr).Cut Destination:=.Range("B" & sr + 1)
.Rows(lr + diff + 1).Insert
.Range("A" & lr + diff + 1) = Val(i, 1) & " Total"
.Range("E" & lr + diff + 1) = Application.Sum(.Range("E" & sr + 1 & ":E" & lr + diff + 1))
Tot = Tot + .Range("E" & lr + diff + 1).Value
.Range("A" & lr + diff + 1 & ":E" & lr + diff + 1).Interior.ColorIndex = 24
.Range("A" & lr + diff + 1 & ":E" & lr + diff + 1).Font.Bold = True
Else
.Range("E" & lr).Cut
.Rows(lr + 1).Insert: .Range("A" & lr + 1) = Space(5) & .Range("A" & lr + 1)
.Range("F" & lr).Cut Destination:=.Range("E" & lr)
.Range("B" & lr & ":E" & lr).Cut Destination:=.Range("B" & lr + 1)
.Rows(lr + 2).Insert
.Range("A" & lr + 2) = Val(i, 1) & " Total"
.Range("E" & lr + 2) = Application.Sum(.Range("E" & lr + 1 & ":E" & lr))
Tot = Tot + .Range("E" & lr + 2).Value
.Range("A" & lr + 2 & ":E" & lr + 2).Interior.ColorIndex = 24
.Range("A" & lr + 2 & ":E" & lr + 2).Font.Bold = True
End If
Next i
.AutoFilterMode = False
lr = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & lr + 1) = "Totals": .Range("E" & lr + 1) = Tot
With .Range("A" & lr + 1 & ":E" & lr + 1)
.Font.Bold = True
.Borders(xlEdgeBottom).Weight = xlThick
.Borders(xlEdgeBottom).ColorIndex = 55
.Borders(xlEdgeBottom).LineStyle = xlDouble
End With
.Columns("E:E").NumberFormat = "0.00"
.Columns("A:E").Columns.AutoFit
End With
Call disclaimer
Sheets("Sheet1").Activate
End Sub
Sub Extract_Information()
Header = Array("Customer", "Customer Sign Date", "Start Date", "Renewal", "Commission")
Set SP = CreateObject("System.Collections.ArrayList")
With Sheet1
lrow = .Range("A" & Rows.Count).End(xlUp).Row
For col = 7 To 14 Step 2
For rw = 7 To lrow
If Not .Cells(rw, col) = "" Then
salesperson = .Cells(rw, col)
If Not SP.Contains(salesperson) Then SP.Add salesperson
End If
Next rw
Next col
SP.Sort
End With
End Sub
Function WorksheetExists(Item) As Boolean
WorksheetExists = Evaluate("ISREF('" & Item & "'!A1)")
End Function
Sub disclaimer()
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim N As Long
For Each ws In Sheets
If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" Then
N = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
ws.Cells(N, "A") = "This report is confidential and for use between Company Name and the affinity/referral partner only. It is not to be disclosed to any other third party."
ws.Activate
With Cells(N, "A")
.Font.Size = 10
.VerticalAlignment = xlTop
.Resize(2, 5).Merge
.WrapText = True
.HorizontalAlignment = xlCenter
End With
End If
Next ws
Application.ScreenUpdating = True
End Sub
Bookmarks