Option Explicit
Sub sixshe_v4()
Const n As Long = 8
Const tbl_beg As String = "Row Labels"
With ThisWorkbook.Sheets("07_Sheet")
'Conditions
Dim r As Range: Set r = .Cells.Find(tbl_beg, .Cells(.Rows.Count, .Columns.Count), xlValues, xlByRows)
If r Is Nothing Then MsgBox "No data": Exit Sub
Dim univ: univ = r.CurrentRegion.Columns.Count
If univ < 3 Then MsgBox "Insufficient data": Exit Sub
'Clearing up
If univ > 3 Then .Range(r.Range("D1"), r.End(xlToRight)).EntireColumn.Delete Shift:=xlToLeft
r.Offset(-1, 0).Clear 'r.Offset(-1, 0).Resize(1, univ).Clear
'Headings I
r.Range("D1").Resize(1, 10).Value = Array("Row Labels", "No. of Claims", "Amount", "SOW", "No of claims (%)", _
"Row Labels", "No. of Claims", "Amount", "SOW1", "No of claims (%)1")
'Variables
Dim c As Integer, cr, frmla
cr = Array(0, 0, 0, -1, -3, 0, 0, 0, -9, -11)
frmla = Array(0, 0, 0, 0, r.Row + (n + 1), 0, 0, 0, 0, r.Row + r.CurrentRegion.Rows.Count - 1)
'Formulas
For c = 4 To 9 Step 5
r.Cells(2, c).Resize(n, 3).Value = r.Range("A2").Resize(n, 3).Value
r.Cells(2, c).Offset(n, 0).Value = "Grand Total"
r.Cells(2, c).Offset(n, 1).Resize(1, 4).FormulaR1C1 = "=SUM(R[-" & n & "]C:R[-1]C)"
r.Cells(2, c + 3).Resize(n, 1).FormulaR1C1 = "=RC[" & cr(c - 1) & "]/R" & frmla(c) & "C[" & cr(c - 1) & "]"
r.Cells(2, c + 4).Resize(n, 1).FormulaR1C1 = "=RC[" & cr(c) & "]/R" & frmla(c) & "C[" & cr(c) & "]"
Next
'Formats
r.Copy
r.Range("D1").Resize(1, 10).PasteSpecial xlPasteFormats: Application.CutCopyMode = False
r.Range("A2").Resize(n + 1, 3).Copy
r.Range("D2").Resize(n + 1, 3).PasteSpecial xlPasteFormats
r.Range("I2").Resize(n + 1, 3).PasteSpecial xlPasteFormats: Application.CutCopyMode = False
r.Select
For c = 4 To 9 Step 5
r.Range(Cells(2, c + 3), Cells(2, c + 4)).Resize(n + 1, 2).NumberFormat = "#0.00%"
Next
r.Range("D2").Offset(n, 0).Resize(1, 10).Font.Bold = True
r.CurrentRegion.EntireColumn.AutoFit
'Headings II & Formats
r.Offset(-1, 0).Value = "Actual Data"
r.Offset(-1, 0).Resize(1, 3).HorizontalAlignment = xlCenterAcrossSelection
r.Offset(-1, 0).Resize(1, 3).Interior.ColorIndex = 34
r.Offset(-1, 3).Value = "Only top %"
r.Offset(-1, 3).Resize(1, 5).HorizontalAlignment = xlCenterAcrossSelection
r.Offset(-1, 3).Resize(1, 5).Interior.ColorIndex = 35
r.Offset(-1, 8).Value = "On ALL"
r.Offset(-1, 8).Resize(1, 5).HorizontalAlignment = xlCenterAcrossSelection
r.Offset(-1, 8).Resize(1, 5).Interior.ColorIndex = 36
'Summary descriptions
With r.Range("D1")
.Offset(n + 4, 0).Value = "1st Highest is " & """" & .Range("A2").Value & """" & ", Amount is " & .Range("C2").Text & ", Percentage is " & .Range("D2").Text
.Offset(n + 5, 0).Value = "2nd Highest is " & """" & .Range("A3").Value & """" & ", Amount is " & .Range("C3").Text & ", Percentage is " & .Range("D3").Text
.Offset(n + 6, 0).Value = "3rd Highest is " & """" & .Range("A4").Value & """" & ", Amount is " & .Range("C4").Text & ", Percentage is " & .Range("D4").Text
.Offset(n + 7, 0).Value = "The Total is " & .Range("C" & n + 2).Text & ", Precentage is " & .Range("D" & n + 2).Text & ", Percentage is " & .Range("E" & n + 2).Text
End With
.Range("A1").Select
End With
End Sub
Bookmarks