Consulting

Results 1 to 3 of 3

Thread: VBA to calculate TOTAL based on condition

  1. #1
    VBAX Newbie
    Joined
    Dec 2019
    Posts
    4
    Location

    VBA to calculate TOTAL based on condition

    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.
    Attached Files Attached Files

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    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
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Newbie
    Joined
    Dec 2019
    Posts
    4
    Location
    Hello paulked,

    Thanks a lot for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •