Sub J3v16v2()
Dim ShtArr, Data, Chk, X0, X1, X2, Temp, StDt As Date, EdDt As Date
Dim Dict As Object, Str As String, i As Long, ii As Long, x As Long
ShtArr = [{"SVR","SR"}]: Set Dict = CreateObject("Scripting.Dictionary")
ReDim Temp(1 To 1000, 1 To 7)
With Sheets("Report")
StDt = .Range("E2"): EdDt = .Range("G2")
If StDt = 0 Then .Rows(5 & ":" & .Rows.Count).Delete
Exit Sub
End With
For i = 1 To 2
With Sheets(ShtArr(i)).Cells(1).CurrentRegion
Data = .Value
For ii = 2 To UBound(Data)
If Data(ii, 4) <> "" Then
Str = Data(ii, 4) & Data(ii, 5) & Data(ii, 6)
X0 = Application.CountIfs(.Columns(4), Data(ii, 4), .Columns(5), Data(ii, 5), .Columns(6), Data(ii, 6), .Columns(2), ">=" & "" & CDbl(StDt) & "", .Columns(2), "<=" & "" & CDbl(EdDt) & "")
If X0 > 0 Then
If i = 2 Then
With Sheets(ShtArr(i - 1)).Cells(1).CurrentRegion
X0 = Application.CountIfs(.Columns(4), Data(ii, 4), .Columns(5), Data(ii, 5), .Columns(6), Data(ii, 6), .Columns(2), ">=" & "" & CDbl(StDt) & "", .Columns(2), "<=" & "" & CDbl(EdDt) & "")
If X0 = 0 Then GoTo Nxt
End With
End If
X1 = Application.SumIfs(.Columns(7), .Columns(4), Data(ii, 4), .Columns(5), Data(ii, 5), .Columns(6), Data(ii, 6), .Columns(2), ">=" & "" & CDbl(StDt) & "", .Columns(2), "<=" & "" & CDbl(EdDt) & "")
X2 = Application.SumIfs(.Columns(8), .Columns(4), Data(ii, 4), .Columns(5), Data(ii, 5), .Columns(6), Data(ii, 6), .Columns(2), ">=" & "" & CDbl(StDt) & "", .Columns(2), "<=" & "" & CDbl(EdDt) & "") / X0
If Not Dict.exists(Str) Then
Dict.Add Str, "": x = x + 1
Temp(x, 1) = Str: Temp(x, 2) = Data(ii, 4)
Temp(x, 3) = Data(ii, 5): Temp(x, 4) = Data(ii, 6)
Temp(x, 5) = X1: Temp(x, IIf(i = 1, 6, 7)) = X2
Else
If i = 2 Then Chk = Application.Match(Str, Application.Index(Temp, , 1), 0): Temp(Chk, 7) = X2
End If
End If
End If
Nxt:
Next ii
End With
Next i
With Sheets("Report")
.Rows(5 & ":" & .Rows.Count).Delete
.Cells(5, 5).Resize(x, 7) = Temp
.Cells(5, 5).Resize(x).Value = Evaluate("Row(1:" & x & ")")
.Cells(5, 12).Resize(x + 1).Formula = "=I5*J5-K5"
.Cells(5, 10).Resize(x + 1, 3).NumberFormat = "#,0.00"
.Cells(5 + x, 9).Resize(, 3).Formula = "=SUM(I5:I" & 5 + x - 1 & ")"
.Cells(5 + x, 5) = "TOTAL"
With .Cells(5, 5).CurrentRegion
.Columns(7).SpecialCells(xlCellTypeBlanks) = 0
.Borders.Weight = 2
.Columns(1).Font.Bold = True
.HorizontalAlignment = xlCenter
End With
End With
End Sub
Bookmarks