Option Explicit
Sub demo()
Dim WS_Count As Integer, i As Integer, j As Integer, pCol As Integer, tCol As Integer, lr As Long
Dim ShtArr, PaidArr, x
Dim pr(1 To 4) As Double
Dim ws As Worksheet
Dim wsn As String
ShtArr = Array("SL", "RS", "VC", "PR", "RP", "EP")
PaidArr = Array("RECEIVED BANK", "RECEIVED CASH", "PAID BANK", "PAID CASH")
Application.ScreenUpdating = False
For i = 1 To 6
Set ws = Sheets(ShtArr(i - 1))
With ws
pCol = Application.Match("*" & "PAY", .Rows("1:1"), 0) ' Find column with "PAY" header
tCol = Application.Match("TOTAL", .Rows("1:1"), 0) ' Find "TOTAL" column
If i <= 3 Then
For j = 1 To 2 ' Loop through RECEIVED x
pr(j) = pr(j) + WorksheetFunction.SumIfs(.Columns(tCol), .Columns(pCol), PaidArr(j - 1) & "*")
Next j
End If
If i >= 3 Then
For j = 3 To 4 ' Loop through PAID
pr(j) = pr(j) + WorksheetFunction.SumIfs(.Columns(tCol), .Columns(pCol), PaidArr(j - 1) & "*")
Next j
End If
End With
Next i
With Sheets("BALANCES")
.[G2] = .[b2] + (pr(1) - pr(3)) ' BANK
.[G3] = .[B3] + (pr(2) - pr(4)) ' CASH
.[G6].Resize(4, 1) = Application.Transpose(pr) ' <<<< for TESTING only
End With
Application.ScreenUpdating = True
End Sub
Bookmarks