Hello all, I have a vba code which basically pulls my "Debtors" and "Debtors Received" from my monthly sheets .( I have 12 worksheets in a workbook, one for each month) and displays them in a separate worksheet called Debtors. The code is working fine but there is a small issue with it . I have different categories debtors and debtors received are not my only categories, for instance i have sales, Expenses , Advance received Etc. When i have advance received that particular customers debtors are not reflecting.
For instance Advance Received from John 12000
And Debtors is 15000
It doesn't even show john. This happens only with advance Received . I am attaching the sample workbook and the code. any help would be grateful. I have highlighted in yellow in my excel sheet in the sheet of April 2020 and Debtors.
Thank You
The link for the excel file is
For instance Advance Received from John 12000
And Debtors is 15000
It doesn't even show john. This happens only with advance Received . I am attaching the sample workbook and the code. any help would be grateful. I have highlighted in yellow in my excel sheet in the sheet of April 2020 and Debtors.
Thank You
VBA Code:
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
If LastRow < 3 Then LastRow = 3
.Range("$A$3:H" & LastRow).ClearContents
.Range("$K$3:L" & LastRow).ClearContents
End With
For Each ws In Sheets
If ws.Name <> "Debtors" Or ws.Name <> "Total" Or ws.Name <> "Names" Then
LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For Each CustName In ws.Range("$J$2: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("$K$3:K" & LastRow)
For Each ws In Sheets
If ws.Name <> "Debtors" Or ws.Name <> "Names" Or ws.Name <> "Total" Then
With ws
If WorksheetFunction.CountIf(.Range("D:D"), CustName) > 0 Then
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("$A$1:J" & LastRow).AutoFilter Field:=4, Criteria1:=CustName
Set fnd = .Range("C:C").SpecialCells(xlCellTypeVisible).Find("Debtors Received")
If Not fnd Is Nothing Then
.Range("$A$1:J" & LastRow).AutoFilter Field:=3, Criteria1:="Debtors Received"
With desWS
ws.Range("$A$2:A" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "E").End(xlUp).Offset(1, 0)
ws.Range("$D$2:D" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "F").End(xlUp).Offset(1, 0)
ws.Range("$E$2:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "G").End(xlUp).Offset(1, 0)
ws.Range("$G$2: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("$A$1:J" & LastRow).AutoFilter Field:=10, Criteria1:=CustName
Set fnd = .Range("I:I").SpecialCells(xlCellTypeVisible).Find("Debtors")
If Not fnd Is Nothing Then
.Range("$A$1:J" & LastRow).AutoFilter Field:=9, Criteria1:="Debtors"
With desWS
ws.Range("$H$2:H" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
ws.Range("$J$2:J" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0)
ws.Range("$K$2:K" & LastRow).SpecialCells(xlCellTypeVisible).Copy .Cells(.Rows.Count, "C").End(xlUp).Offset(1, 0)
ws.Range("$L$2: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($D$3:D" & LastRow & ")"
.Range("H" & LastRow + 1).Formula = "=sum($H$3:H" & LastRow & ")"
.Range("$L$3:L" & LastRow).Formula = "=SUMIF($B$2:D" & LastRow & ",K3,$D$2:D" & LastRow & ")-SUMIF($F$2:H" & LastRow & ",K3,$H$2:H" & LastRow & ")"
.Range("L" & LastRow + 1).Formula = "=sum($L$3:L" & LastRow & ")"
End With
Application.ScreenUpdating = True
End Sub
The link for the excel file is
Dropbox
www.dropbox.com