PDA

View Full Version : [SOLVED:] VBA to calculate TOTAL based on condition



aruncr
12-31-2019, 11:14 PM
Hello Experts,


I have a workbook with 2 sheets.


I request your help with a VBA macro in Sheet1 to calculate totals in case the Sales and Purchases for an item are equal.
The sample output should be as in Sheet2.


Thanks in advance.

paulked
01-01-2020, 10:51 AM
Sub KedTest()
Dim lr As Long, rw As Long, p As Long, s As Long, t As Double
lr = Cells(Rows.Count, 1).End(xlUp).Row
For rw = 2 To lr
t = 0
p = 0
s = 0
Do Until Cells(rw, 1) = ""
Cells(rw, 1).Select
If Cells(rw, 2) = "Sales" Then s = s + Cells(rw, 4)
If Cells(rw, 2) = "Purchase" Then p = p + Cells(rw, 4)
t = t + Cells(rw, 6)
rw = rw + 1
Loop
If s = p Then Cells(rw, 6) = t
Next
End Sub

aruncr
01-06-2020, 09:56 PM
Hello paulked,

Thanks a lot for your help.