Sub Filter_it()
Dim i As Integer
Dim lr As Long
Dim keyrng As Range
Application.ScreenUpdating = False
Worksheets("Filtered").Range("A1:ZZ1000").ClearContents
Worksheets("Filter_Formula").Activate
lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("A4:O" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"Crit"), CopyToRange:=Range("Filter_Start"), Unique:=False
Worksheets("Filtered").Activate
For i = 11 To 15
lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Set keyrng = Range(Cells(2, i), Cells(2, i))
'
' Remove Duplicates
'
Range(Cells(1, i), Cells(lr, i)).Select
ActiveSheet.Range(Cells(1, i), Cells(lr, i)).RemoveDuplicates Columns:=1, Header:= _
xlYes
'
' Sort
'
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=keyrng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveSheet.Sort
.SetRange Range(Cells(2, i), Cells(lr, i))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Next i
Insert_Columns
Columns("K:AN").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
Application.ScreenUpdating = True
Range("A1").Select
End Sub
Sub Insert_Columns()
headings = Array(" ", "Total: #/%", "P: #/%", "L: #/%", "B: #/%", "WP: #/%")
pl = Array(" ", "*", "P", "L", "B", "WP")
Columns("L:P").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("R:V").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("X:AB").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("AD:AH").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
nfrow = Cells(Rows.Count, "A").End(xlUp).Row - 1
ncc = 5
For i = 11 To 35 Step 6
ncc = ncc + 1
For j = 1 To 5
nn = i + j
Cells(1, nn) = headings(j)
Call Calc_NP(i, nn, ncc, nfrow, pl(j))
Next j
nlast = Cells(Rows.Count, i).End(xlUp).Row
For k = 2 To nlast
pct = Cells(k, i + 1) / (nfrow - 1)
Cells(k, i + 1) = Cells(k, i + 1) & "/" & Format(pct, "#0.0%")
Next k
Next i
End Sub
Sub Calc_NP(col, cc, c, nfr, cat)
nlast = Cells(Rows.Count, col).End(xlUp).Row
For i = 2 To nlast
If cat = "*" Then
Var1 = Cells(i, col)
Var2 = Application.WorksheetFunction.CountIf(Range(Cells(2, c), Cells(nfr, c)), Var1)
pct = Var2 / nfr
Cells(i, cc) = Var2
' Cells(i, cc) = Var2 & "/" & Format(pct, "#0.0%")
Else
Var1 = Cells(i, col)
Var2 = Application.WorksheetFunction.CountIfs(Range(Cells(2, c), Cells(nfr, c)), Var1, Range(Cells(2, 5), Cells(nfr, 5)), cat)
pct = 0
If Cells(i, col + 1) <> 0 Then pct = Var2 / Cells(i, col + 1)
Cells(i, cc) = Var2 & "/" & Format(pct, "#0.0%")
End If
Next i
End Sub
Data is output to Sheet "Filtered"
Bookmarks