Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to record Value based on condition - Excel

0

Hi Team,

Happy New Year - 2024!

I am looking for the solution for recording the sum of quantity to respective cells.

Please advise for solution to record the sum value: 

Whenever in the Column D, there is any changes from Empty value to "Buy" or from "Sell" to "Buy" than the respected quantity available in Column C to add to Column E. 

Whenever in the Column D, there is any changes from Empty value to "Sell" or from "Buy" to "Sell " than the respected quantity available in Column C to add to Column F 

For example, If whenever there is any change in Trade Column from Blank value to Buy or from Sell to Buy (positive move), than the given quantity i.e. 100 in quantity column to add to Total Buy column and If whenever there is any change in Trade Column from Blank value to Sell or from Buy to Sell (negative move), than the given quantity i.e. 100 in quantity column to add to Total Sell column and so on. 

You have already given same kind of solution to my previous query i.e. "Record Number of sum to formula based cell - Excel". 

I have attached the excel sheet for your ready reference.

Can I request you to please help for the solution.

Thanks,

Answer
Discuss

Answers

0
Selected Answer

Sunil

Happy New Year to you and all Forum users!

In the FIRST attached revised file:

  1. the sherts are renamed to Transactions (linked to the remote file) and Totals- for the VBA-summed values) 
  2. the VBA ThisWorkbook section has this code (with commnets to show how it works):
Dim LstRw As Long, LastVals As Variant

Private Sub Workbook_Open()

    With Sheet1
        ' capture depth of sheet (col B) and current Trade values (col D)
        LstRw = .Range("B" & Rows.Count).End(xlUp).Row
        LastVals = .Range("D1:D" & LstRw).Value
    End With
End Sub

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Dim Rw As Long

    ' check for changes occured on the  "Totals" sheet only
    If Sh.Name <> Sheet1.Name Then Exit Sub

    ' prevent retrigger and reverse the change
    Application.EnableEvents = False

    With Sheet1
        LstRw = .Range("B" & Rows.Count).End(xlUp).Row
        ' compare each cell in D with LastVals to find changes
        For Rw = 3 To LstRw
            ' see if a value changed (allowing for offset
            If LastVals(Rw, 1) <> "Buy" And .Cells(Rw, 4).Value = "Buy" Then

                    ' if it changes to Buy, add C to E...
                    .Cells(Rw, 5).Value = .Cells(Rw, 5).Value + .Cells(Rw, 3).Value

                ElseIf LastVals(Rw, 1) <> "Sell" And .Cells(Rw, 4).Value = "Sell" Then
                    ' if it changes to Sell, add C to F...
                    .Cells(Rw, 6).Value = .Cells(Rw, 6).Value + .Cells(Rw, 3).Value

                Else
                    ' assume no change and do nothi45ng
            End If
        Next Rw

        'save changed values
        LastVals = .Range("D1:D" & LstRw).Value
        Application.EnableEvents = True

    End With

End Sub

(This is slighly changed from the code in my Answer to your recent Question under Record value when change in respective cell. It now looks for changes to "Buy" or to "Sell")

The second attached file is renamed (to include an underscore since your link needed that). If you open that first then the .xlsm file, you can change Trade labels and quantities in the remote file and see them reflected as instantaneous values (in C of the .xlsm file)  and sums (in columns E and F).

Hope this fixes your problem. If so, pllease remember to mark the Answer as Seelected (and next time, make an attempt to solve your own problem first please).

Discuss

Discussion

Thank you John for your quick solution.
SunilA (rep: 58) Jan 1, '24 at 10:10 am
Thanks for selecting my Answer Sunil. 
John_Ru (rep: 6142) Jan 1, '24 at 12:02 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login