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

Record value when change in respective cell

0

Hi,

I am trying to get the changed value accordingly in excel as per below flow:

For example: if cell value of A2 = 100, B2 = Buy than C2 = 100 and when value of B2 change from Buy to Sell than D2 =100 and again value of A2 change from 100 to 200 and value of B2 change from Sell to Buy than C2 = 300 (100+200) It should give result only when the B2 value changes from Buy to Sell or Sell to Buy. A3 = 100, B3 = Sell than D2 = 100 and when value of B3 change from Sell to Buy than C2 =100 and again value of A3 change from 100 to 200 and value of B3 change from Buy to Sell than D3 = 300 (100+200) It should give result only when the B3 value changes from Sell to Buy or Buy to Sell. There should be change to respective row only. it should not impact other rows.

The values in column A and B will changed remotely (through formula based cell). The cell value will change based on formula. At a time multiple row cell will change.

Please find attached excel sheet for your reference.

Can you able to help to get the required result.

Thanks

Answer
Discuss

Discussion

Hi again Sunil.

That's a bit confusing to read  but I'll look at your file later hopefully.

Before I do so, please say (in your question text) if the values in column B are changed manually (or remotely, by SQL for example. as in some of your previous questions). Also, can only one cell change at a time or more?
John_Ru (rep: 6142) Dec 27, '23 at 5:57 am
Thank you for your kind response.
Added in Question text: The values in column A and B will changed remotely (through formula based cell). The cell value will change based on formula. At a time multiple row cell will change depend on the result from formula.
SunilA (rep: 58) Dec 27, '23 at 8:18 am
Thanks Sunil but can multiple rows change remotely? E.g. rows 2 and 4 in your simple example 
John_Ru (rep: 6142) Dec 27, '23 at 12:46 pm
Yes, row 2 and 4 can change remotely.
For example in excel:
The value of A2 = 100, B2 = Buy than C2 = 100 and when value of B2 change from Buy to Sell than D2 =100 and again value of A2 change from 100 to 200 and value of B2 change from Sell to Buy than C2 = 300 (100+200) It should give result only when the B2 value changes from Buy to Sell or Sell to Buy. A3 = 100, B3 = Sell than D2 = 100 and when value of B3 change from Sell to Buy than C2 =100 and again value of A3 change from 100 to 200 and value of B3 change from Buy to Sell than D3 = 300 (100+200) It should give result only when the B3 value changes from Sell to Buy or Buy to Sell. There should be change to respective row only. it should not impact other rows
Thanks
SunilA (rep: 58) Dec 28, '23 at 2:51 am
Thanks Sunil but I'd read that and meant "can rows 2and 4 change at the same time?" 
John_Ru (rep: 6142) Dec 28, '23 at 3:00 am
it depends upon the result of excel based formula. it may be the two different row cell change at same time or may be different time
SunilA (rep: 58) Dec 28, '23 at 4:40 am
Okay, the possibility of simultaneous cell complicates the solution a bit. Will try the answer later today my time. 
John_Ru (rep: 6142) Dec 28, '23 at 5:47 am
Thanks John for your kind response.
Appreciated very much.
SunilA (rep: 58) Dec 28, '23 at 7:15 am
Add to Discussion

Answers

0
Selected Answer

Sunil

The attached revised file allows for the possibility of multiple simultaneous changes in Sheet1 column B (as you clarified in the Discussion under your question).

When you open the file (macro-enabled), values in Sheet1 column B are stored in a variable LastVals via this code (within the ThisWorkbook section of VBA):

Dim LstRw As Long, LastVals As Variant

Private Sub Workbook_Open()

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

In the same section, this event code is stored, which uses LastVals to look for changes. It's commented so you can follow what's happening:

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

    ' check for changes occured on the target sheet only
    If Sh.Name <> Sheet2.Name Then Exit Sub

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

    With Sheet2
        LstRw = .Range("B" & Rows.Count).End(xlUp).Row
        ' compare each cell in B with LastVals to find change
        For Rw = 2 To LstRw
            ' see if a value changed
            If LastVals(Rw, 1) = "Buy" And .Cells(Rw, 2).Value = "Sell" Then

                    ' if it changes to Sell, add A to D...
                    .Cells(Rw, 4).Value = .Cells(Rw, 4).Value + .Cells(Rw, 1).Value

                ElseIf LastVals(Rw, 1) = "Sell" And .Cells(Rw, 2).Value = "Buy" Then
                    ' if it changes to Buy, add A to C...
                    .Cells(Rw, 3).Value = .Cells(Rw, 3).Value + .Cells(Rw, 1).Value

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

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

    End With

End Sub

If you change Sheet1, that triggers the event and Sheet2 gets updated as per your "Remarks" (=rules!).

You can add extra rows (in both sheets, with Events disabled so they both match) but  take care that there aren't too many rows so the code doesn't finish before the next chnage occurs.

You can use Copy/Paste (with the opposites of column B, in column F say) to test that it works for multiple simultaneous changes in Sheet1.

Hope this fixes your problem. If so, please remmeber to mark this Answer as Selected.

Discuss

Discussion

The excel is not working. Can i request you to please recheck and share updated excel. Thanks
SunilA (rep: 58) Dec 28, '23 at 11:57 am
Sunil. the file works fine for me (but I re-uploaded it just).

Have you checked that Sheet2 has not changed?
John_Ru (rep: 6142) Dec 28, '23 at 12:06 pm
It MUST be opened with macros enabled (so you might need to save it to a safe liocation first).. 
John_Ru (rep: 6142) Dec 28, '23 at 12:07 pm
If you can't get it to work, copy all my code above to the ThisWorkbook section of your file, save, close then re-open.
John_Ru (rep: 6142) Dec 28, '23 at 12:09 pm
I hope you realise that the code ONLY works when column B of Sheet1 changes (e.g. from "Buy" to "Sell")
John_Ru (rep: 6142) Dec 28, '23 at 12:34 pm
Thanks for your response.
Naturally, When the sheet will open at that 1st time the value in Column A & B will be Blank(empty) (based on formula). And in the middle of workflow also the Column A will change the quantity and column B might be 1st Blank than it will change to Buy or Sell or again Blank. Its whenever the condition match, the formula based cell i.e Column A & B will change to Blank or Buy or Sell. 
Can I request you to help for the same.
Thank you
SunilA (rep: 58) Dec 28, '23 at 12:57 pm
Sunil, why didn't you say that in your original question? You say *naturally" but your approach is as unique as that of others (and not intuitive) so don't expect us to guess! 

Once again I have answered your original question fully only to find you want something more. You may recall that this kind of behavour was problematic (under your previous user name NitilA). 

I won't be doing any more on this, at least not today.
John_Ru (rep: 6142) Dec 28, '23 at 1:15 pm
Noted your message. I am really sorry for inconvience caused.
Should i create new question with more clarity on question.
SunilA (rep: 58) Dec 29, '23 at 11:30 am
Sunil. Please look at the Rules of the Forum (hyperlink above), especially rules 2 and 6 under Asking Questions- these are the ones you are breaking in my opinion.

Kindly realise that Willie and I give our own time (without any payment whatsoever) to Answer user questions. If we spend our own free time answering a question, we don't want to spend even more time because the user forgot to add a crucial detail or then  wants something different. This is especially true when the user serms to have made no effort to solve their (not our) problem

You can ask a new question if you like but I may or may not reply. Willie may do likewise, especially since this a holiday period for us. 
John_Ru (rep: 6142) Dec 29, '23 at 1:33 pm
Thanks for selecting my ANswer, Sunil (and see my Answer to your new question).
John_Ru (rep: 6142) Jan 1, '24 at 8:38 am
Add to Discussion


Answer the Question

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