Sub HideRowsAndCalculateTotal()
Dim ws As Worksheet
Dim rng As Range
Dim i As Long
Dim j As Long
Dim total As Double
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set rng = ws.Range("B5:D" & ws.Range("B" & ws.Rows.Count).End(xlUp).Row)
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
If rng.Cells(i, j).Value > 5 Then
rng.Rows(i).EntireRow.Hidden = True
Exit For
Else
rng.Rows(i).EntireRow.Hidden = False
End If
Next j
Next i
For j = 1 To rng.Columns.Count
total = Application.WorksheetFunction.Aggregate(9, 3, rng.Columns(j).SpecialCells(xlCellTypeVisible))
rng.Cells(rng.Rows.Count + 1, j).Value = total
Next j
End Sub