Sub IRDUB52_Subtotal()
Dim Ctrl As String
Ctrl = Application.Caller
Dim LastRow As Long
Dim NextMonth As String
Dim R As Long
Dim Rng As Range
Dim ThisMonth As String
Dim Wks As Worksheet
Set Wks = Worksheets("IRDUB52")
LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Wks.Range(Cells(2, "A"), Cells(LastRow, "D"))
Rng.Sort Key1:=Wks.Range("B1")
R = 2
With Wks
Do While .Cells(R, "C").Value <> ""
SubAmount = SubAmount + .Cells(R, "D").Count
ThisMonth = Format(.Cells(R, "B"), "dd/mm/yyyy")
NextMonth = Format(.Cells(R + 1, "B"), "dd/mm/yyyy")
If ThisMonth <> NextMonth Then
.Cells(R + 1, "B").EntireRow.Insert Shift:=xlShiftDown
With .Cells(R + 1, "A")
.Value = "Count " & ThisMonth
.Font.Bold = True
End With
With .Cells(R + 1, "D")
.Font.Bold = True
.Value = SubAmount
End With
.Cells(R + 2, "B").EntireRow.Insert Shift:=xlShiftDown
TotalAmount = TotalAmount + SubAmount
SubAmount = 0
R = R + 3
Else
R = R + 1
End If
Loop
.Cells(R, "B").Value = "Total"
.Cells(R, "B").Font.Bold = True
.Cells(R, "D").Value = TotalAmount
.Cells(R, "D").Font.Bold = True
With .Range(.Cells(R - 1, "A"), .Cells(R - 1, "D"))
End With
End With
Set Wks = Worksheets("IRDUB55")
LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Wks.Range(Cells(2, "A"), Cells(LastRow, "D"))
Rng.Sort Key1:=Wks.Range("B1")
R = 2
With Wks
Do While .Cells(R, "C").Value <> ""
SubAmount = SubAmount + .Cells(R, "D").Count
ThisMonth = Format(.Cells(R, "B"), "dd/mm/yyyy")
NextMonth = Format(.Cells(R + 1, "B"), "dd/mm/yyyy")
If ThisMonth <> NextMonth Then
.Cells(R + 1, "B").EntireRow.Insert Shift:=xlShiftDown
With .Cells(R + 1, "A")
.Value = "Count " & ThisMonth
.Font.Bold = True
End With
With .Cells(R + 1, "D")
.Font.Bold = True
.Value = SubAmount
End With
.Cells(R + 2, "B").EntireRow.Insert Shift:=xlShiftDown
TotalAmount = TotalAmount + SubAmount
SubAmount = 0
R = R + 3
Else
R = R + 1
End If
Loop
.Cells(R, "B").Value = "Total"
.Cells(R, "B").Font.Bold = True
.Cells(R, "D").Value = TotalAmount
.Cells(R, "D").Font.Bold = True
With .Range(.Cells(R - 1, "A"), .Cells(R - 1, "D"))
End With
End With
Set Wks = Worksheets("IRDUB60")
LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Wks.Range(Cells(2, "A"), Cells(LastRow, "D"))
Rng.Sort Key1:=Wks.Range("B1")
R = 2
With Wks
Do While .Cells(R, "C").Value <> ""
SubAmount = SubAmount + .Cells(R, "D").Count
ThisMonth = Format(.Cells(R, "B"), "dd/mm/yyyy")
NextMonth = Format(.Cells(R + 1, "B"), "dd/mm/yyyy")
If ThisMonth <> NextMonth Then
.Cells(R + 1, "B").EntireRow.Insert Shift:=xlShiftDown
With .Cells(R + 1, "A")
.Value = "Count " & ThisMonth
.Font.Bold = True
End With
With .Cells(R + 1, "D")
.Font.Bold = True
.Value = SubAmount
End With
.Cells(R + 2, "B").EntireRow.Insert Shift:=xlShiftDown
TotalAmount = TotalAmount + SubAmount
SubAmount = 0
R = R + 3
Else
R = R + 1
End If
Loop
.Cells(R, "B").Value = "Total"
.Cells(R, "B").Font.Bold = True
.Cells(R, "D").Value = TotalAmount
.Cells(R, "D").Font.Bold = True
With .Range(.Cells(R - 1, "A"), .Cells(R - 1, "D"))
End With
End With
Set Wks = Worksheets("IRDUB65")
LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Wks.Range(Cells(2, "A"), Cells(LastRow, "D"))
Rng.Sort Key1:=Wks.Range("B1")
R = 2
With Wks
Do While .Cells(R, "C").Value <> ""
SubAmount = SubAmount + .Cells(R, "D").Count
ThisMonth = Format(.Cells(R, "B"), "dd/mm/yyyy")
NextMonth = Format(.Cells(R + 1, "B"), "dd/mm/yyyy")
If ThisMonth <> NextMonth Then
.Cells(R + 1, "B").EntireRow.Insert Shift:=xlShiftDown
With .Cells(R + 1, "A")
.Value = "Count " & ThisMonth
.Font.Bold = True
End With
With .Cells(R + 1, "D")
.Font.Bold = True
.Value = SubAmount
End With
.Cells(R + 2, "B").EntireRow.Insert Shift:=xlShiftDown
TotalAmount = TotalAmount + SubAmount
SubAmount = 0
R = R + 3
Else
R = R + 1
End If
Loop
.Cells(R, "B").Value = "Total"
.Cells(R, "B").Font.Bold = True
.Cells(R, "D").Value = TotalAmount
.Cells(R, "D").Font.Bold = True
With .Range(.Cells(R - 1, "A"), .Cells(R - 1, "D"))
End With
End With
End Sub
Bookmarks