Sub GetBalance()
Application.ScreenUpdating = False
Dim LastRow As Long, ws As Worksheet, CustName As Range, desWS As Worksheet, d As Long, h As Long, fnd As Range
Set desWS = Sheets("Debtors")
With desWS
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("A3:H" & LastRow).ClearContents
.Range("K4:L" & LastRow).ClearContents
End With
For Each ws In Sheets
If ws.Name <> "Debtors" Then
LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For Each CustName In ws.Range("J2:J" & LastRow)
If CustName <> "" Then
If WorksheetFunction.CountIf(desWS.Range("K:K"), CustName) = 0 Then
With desWS
.Cells(.Rows.Count, "K").End(xlUp).Offset(1, 0) = CustName
End With
End If
End If
Next
End If
Next ws
LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For Each CustName In desWS.Range("K4:K" & LastRow)
For Each ws In Sheets
If ws.Name <> "Debtors" Then
With ws
If WorksheetFunction.CountIf(.Range("D:D"), CustName) > 0 Then
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("A1:J" & LastRow).AutoFilter Field:=4, Criteria1:=CustName
Set fnd = .Range("C:C").SpecialCells(xlCellTypeVisible).Find("Debtors Received")
If Not fnd Is Nothing Then
.Range("A1:J" & LastRow).AutoFilter Field:=3, Criteria1:="Debtors Received"
With desWS
ws.Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1, 0)
.Cells(.Rows.Count, "F").End(xlUp).Offset(1, 0) = CustName
ws.Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "G").End(xlUp).Offset(1, 0)
ws.Range("G2:G" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "H").End(xlUp).Offset(1, 0)
End With
.Range("A1").AutoFilter
End If
End If
If WorksheetFunction.CountIf(.Range("J:J"), CustName) > 0 Then
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("A1:J" & LastRow).AutoFilter Field:=10, Criteria1:=CustName
Set fnd = .Range("I:I").SpecialCells(xlCellTypeVisible).Find("Debtors")
If Not fnd Is Nothing Then
.Range("A1:J" & LastRow).AutoFilter Field:=9, Criteria1:="Debtors"
With desWS
ws.Range("H2:H" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
.Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = CustName
ws.Range("K2:K" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0)
ws.Range("L2:L" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "D").End(xlUp).Offset(1, 0)
End With
.Range("A1").AutoFilter
End If
.Range("A1").AutoFilter
End If
If ws.AutoFilterMode Then ws.AutoFilterMode = False
End With
End If
Next ws
Next CustName
With desWS
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("D" & LastRow + 1).Formula = "=sum(D3:D" & LastRow & ")"
.Range("H" & LastRow + 1).Formula = "=sum(H3:H" & LastRow & ")"
.Range("L" & LastRow + 1).Formula = "=sum(L4:L" & LastRow & ")"
.Range("L4:L" & LastRow).Formula = "=SUMIF(B2:D" & LastRow & ",K4,D2:D" & LastRow & ")-SUMIF(F2:H" & LastRow & ",K4,H2:H" & LastRow & ")"
End With
Application.ScreenUpdating = True
End Sub