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 capture Addition and Subtraction value

0

Hi Variatus,

Hope you are doing well.

Like you have given the best solution for https://www.teachexcel.com/talk/5303/how-to-capture-highest-and-lowest-value-till-respective-time

Once again Thank you for the same.

Further, I am looking for the Addition and subtraction for respective cells in same manner.

I have tried to code the same like you have provided code for Highest and lowest value but I am facing run time error of 438 in Private Sub RecordAddSub(ByVal NewVal As Variant, _ (attached screenshot and revised Excel sheet.

Hence, Can I request you to please help me to solve the problem.

I would highly appreciate your valuable expert knowledgable solution.

Regards,

Revised Question:

I am looking for capture of Addition and Subtraction to the cell.

For example: Let's say, Start from number 10 in a cell (In Column C), than if the cell value change to 20 than In cell (In Addition: Column D) to show 10 and no change in cell (In Subtract: Column E).

For example 2: If the cell value change to 5 (considering start number 10 in Column C), than there is no change In cell (In Addition: Column D) and the cell value to change to 5 in cell (In Subtract: Column E).

Basically, I am looking all the changes in column C whether addition or subtraction to capture in column D (addition Column) and in Column E (subtraction Column).

Whenever there is change (difference) in increase of value in Column C, it should be capture in Column D and whenever there is change (difference) in decrease of value in Column C, it should be capture in Column E.

The Column C value is updated automatically from RTD (Real time data).

Trust you understand the question.

Please let me know for more understanding. 

I seek your help for solution.

Regards,

Answer
Discuss

Discussion

Sunil

I don't like your latest question to three reasons:

1) Your question doesn't say what went wrong with your attempt or which module/sub it relates to.

2) You've included a file (with the same name as before- that's bad version control!) which will now run macros every 10 seconds when opened. That's not good for the unsuspecting user/ PC (but I guessed and disabled macros first).

3) We've asked you previously not to address questions to individual contributors.

Suggest you isolate the problem by including the troublesome code (ONLY) in your question, explaining what goes wrong and sending a representative file with the troublesome code  only (and WITHOUT the Timers).

John
John_Ru (rep: 6142) Sep 28, '21 at 3:58 am
Hi John,
I am really sorry for any kind of wrong communication or any kind of communication gap.
1. I am facing run time error of 438 in Private Sub RecordAddSub(ByVal NewVal As Variant, _ (attached screenshot).
2. I kept 10 second just for see the result immediately. Changed to 5 min.
3. I have address to Variatus, as she have given the solution for same kind of problem in past. Anyways, Noted your email with thanks.
Attached revised excel sheet and screenshot of error for your ready reference.
I am looking for addition and subtraction value for respective cell.
Can I request you to please help for solution.
Regards,

SunilA (rep: 58) Sep 28, '21 at 6:12 am
Sunil

To your points
1) Add that to the question, like I asked. Don't know if screenshot is in file, won't open until...
2) I asked you to send only the troublesome macro in your fil;e (and remove timers, not just set time to 5 mins).
3) You say "she" but you left the name in the question and I think Variatus is a man!

Correct your question and I'll see if I can find time to look at it.

Also you also ignored my comment about version control- you should have soime means of identifying versions of a file (as your project progresses)- using the same name is just confusing.
John_Ru (rep: 6142) Sep 28, '21 at 6:18 am
Hi John,
Thank you for your response.
1. Added to the question.
2. Attached revised excel sheet to the question.
3. Sorry for my mistake, if Variatus is a man.
4. Also changed the file name to avoid confusion.

Request you to please help for solution.

Regards,
SunilA (rep: 58) Sep 28, '21 at 10:45 am
Thanks Sunil. Think I've worked it out; will answer shortly.

I'm worried about your project since there seems to be no clear progression- you seem to jump for file to file, going back and forth with answers/files over weeks. E.g. You have a line:
With Workbooks("Capture Addition and subtraction.xlsm").Worksheets("Sheet1")
when a safer/easier version had been pointed out in a previous question:
With ThisWorkbook.Worksheets("Sheet1")

Hope you have a plan!
John_Ru (rep: 6142) Sep 28, '21 at 11:56 am
Add to Discussion

Answers

0
Selected Answer

Sunil

I think there are two problems with your reduced file.

1) In your sub MyMacro, the second call on sub RecordAddSub sends an incorrect parameter- corrected and shown in bold below

For R = NwsFirstRow To Rl

      Ra = R - NwsFirstRow + 1

      RecordAddSub Arr(Ra, NwsAvg1), Arr(Ra, NwsAdd1), .Cells(R, NwsAdd1), True

      RecordAddSub Arr(Ra, NwsAvg1), Arr(Ra, NwsAdd1), .Cells(R, NwsSub1), False

2) In your new sub RecordAddSub, you have these lines:

        If Not IsEmpty(OldVal) Then

            If IsAdd Then

                NewVal = WorksheetFunction.Add(NewVal, OldVal)

            Else

                NewVal = WorksheetFunction.Sub(NewVal, OldVal)

but there no worksheet functions called Add or Sub in standard Excel (so VBA throws up the error you mention). I changed the lines to this, which seems to work:

If Not IsEmpty(OldVal) Then
            If IsAdd Then
                NewVal = NewVal + OldVal
            Else
                NewVal = NewVal - OldVal
            End If

EXTRA ANSWER (to revised question)

In the attached version of your file, I've removed the failed sub RecordAddSub and performed the hisorical stock movement sums in MyMacro.

I've added some new constants and tidied it up so it's now (changes in bold):

Enum Nws                                        ' worksheet navigation (Sheet1)
    ' 267 (ex 206)then modified 29 Sept 2021
    NwsFirstRow = 2  ' change to suit 3 (row 3)
    NwsStock1 = 3    ' change to suit (3 =column C)
    NwsAdd1 = 4      ' change to suit (4 =column DE)
    NwsSub1 = 5      ' change to suit (5 =column E)
    NwsLast1 = 6     ' change to suit (5 =column F)
End Enum
Then I've added a hidden column F in Sheet1 since I can't be sure when your RTD runs (or use a Worksheet_Change event macro) so MyMacro saves the last stock value there whenever it is run. The new commented code is:
Sub MyMacro()
    ' 267 (ex 206 - 17 Jun 2021) then modified 30 Sept 2021

Dim Rl          As Long                 ' last used row in column A
Dim Arr         As Variant              ' read data from the worksheet
'Dim R           As Long                 ' loop counter: sheet rows
Dim Ra          As Long                 ' array row number

'Macro code that you want to run.

With ThisWorkbook.Worksheets("Sheet1")

    Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
    'write range to array for speed
    Arr = .Range(.Cells(NwsFirstRow, 1), .Cells(Rl, NwsLast1)).Value 'extended to included Last value


    For Ra = 1 To UBound(Arr, 1) ' loop through array
        If Arr(Ra, NwsStock1) >= Arr(Ra, NwsLast1) Then 'compare new and last stock
            'if >= last, add change to positive history cell
            Arr(Ra, NwsAdd1) = Arr(Ra, NwsAdd1) + Arr(Ra, NwsStock1) - Arr(Ra, NwsLast1)
            Else
            'if < last, add change to positive history cell
            Arr(Ra, NwsSub1) = Arr(Ra, NwsSub1) + Arr(Ra, NwsLast1) - Arr(Ra, NwsStock1)
        End If
        ' copy new stock in last stock cell (for next run)
        Arr(Ra, NwsLast1) = Arr(Ra, NwsStock1)

    Next Ra

    'write array back to same range
    .Range(.Cells(NwsFirstRow, 1), .Cells(Rl, NwsLast1)) = Arr

End With

    ' enable this line of you want to save the change:-
    'ThisWorkbook.Save

    'Calls the timer macro so it can be run again at the next interval.
'    Call SetTimer
End Sub
[Now if you manually change the values in column C of Sheet1  (or your RTD update does that) whenmyMacro is run, the differences will appear in columns D and E (what you called Addition and Subtraction).

(Note I realised that counter R is now not required (since the code loops only through the array) so its Dim declaration statement is commented out.

In your production situation, you'll need to call the macro via timers (as we've shown before). When you clear values in Sheet1, don't forget to include column F. Given all the work is done in the array (apart from writing back to the cells once), I've also removed the lines turning Application.ScreenUpdating off and on 

Hope this finally fixes things for you.

Discuss

Discussion

Thank you very much John for your solution.

When i run the macros with your suggested solution, the result i am getting like below:
For example: Let's say, We start the number with 10 and when the cell is changed to 11, the result getting is 21 (In addition cell) and 1 (In Subtraction cell).

Where else, I am looking for result like below:
For example: Let's say, We Start the number with 10 and when the cell changed to 11, the result to be 1 (In addition cell) and no change in Subtraction cell.

Further, When the cell is changed to 9 from 10, then there would be no change in addition cell and the result to be -1 in Subtract cell.

Basically, I am looking to capture all the addition to the cell and all the subtraction to the cell.

Can I request for your extended help for the solution.

Regards,
SunilA (rep: 58) Sep 28, '21 at 2:21 pm
Sunil. I've pointed out why the error occurred but didn't do what you just described (which wasn't in the question you addressed to Variatus).

I don't think my guess was unreasonable since you said "I am looking for the Addition and subtraction for respective cells in same manner" and invented Excel functions called Add and Sub. 

Your new aim isn't hard to do but requires you to think. I'm getting fed up with trying to fix your work so won't do any more on this .
John_Ru (rep: 6142) Sep 28, '21 at 4:24 pm
Hi John,
I am really sorry for any kind of inconvenience caused to you.

The main aim of this query was looking for addition and subtraction for respective cell, but I don't know from where to start. So, I started to write code like in the said excel sheet but in between the error occurred (which you have solved) and I  didn't get the result which I am looking for.
Now, after you provided the solution for error, I run the macros for testing and found this results which mentioned in previous message but I am looking for something else, as I mentioned. 
I sincerely try to convey that there is no any kind of intention of changing the aim.

Hope you understand my point. 
I am sincerely looking for your valuable expert knowledgeable solution. 
I know you are right person for any kind of excel problem I came across, as you are genius of excel and I found solutions for each of my query in the past. Thank you for the same. 
I can say, Teach excel have very talented people in the group to help the community. 
I sincerely request you to Please help for the solution. 

Regards 
SunilA (rep: 58) Sep 29, '21 at 2:02 am
Sunil

My time is limited so precise questions help (as we've said before). Seems to me that you're looking for the last movement in the values in column C of your Sheet1 (i.e. by how much the stock went up or down), right?. If these are updated manually then a Worksheet_Change event macro could be used but are they updated from your remote link? Please EDIT your question to explain and I'll take one LAST look at your workbook.

BTW normally words like "valuable", "expert", "talented" and "genius" would be welcome but in your case it feels insincere (though you say "sincerely") and that you're just using them to get us to help more. Please avoid liberal use of them in future.
John_Ru (rep: 6142) Sep 29, '21 at 3:47 am
Hi John,
Thank you for your response.
Noted your last message.
I have edited the main question with revised excel sheet.
I need your support for solution.
Regards,
SunilA (rep: 58) Sep 29, '21 at 5:19 am
Sunil. Before I look (later), you did not revise your question to relflect my query above on whther or not  "...values in column C.... are updated manually ...(or)...are they updated from your remote link?"
John_Ru (rep: 6142) Sep 29, '21 at 6:10 am
The Values in Column C is updated automatically through RTD (Real time data).
Added to revised question.
SunilA (rep: 58) Sep 29, '21 at 6:23 am
See "EXTRA ANSWER (to revised question)"
John_Ru (rep: 6142) Sep 29, '21 at 8:08 am
Hi John,
I applaud you for your patience - you have far more than I for this question. I read it 4 or 5 times and still was unable to figure out what was wanted.
At one point SunilA states "For example: Let's say, We Start the number with 10 and when the cell changed to 11, the result to be 1 (In addition cell) " - that sure seems like subtraction to me.
Good luck John.
WillieD24 (rep: 557) Sep 29, '21 at 8:50 am
Thanks for the applause Willie but my patience is close to exhausted on this project.

On the example you cite, he means the stock was added to by 1 (what I've titled a "positive stock movement" in the revised file)
John_Ru (rep: 6142) Sep 29, '21 at 9:03 am
Hi John,
While testing the macro, I am facing error of Arr()= <subscript out of range> for below code:
.Range(.Cells(NwsFirstRow, 1), .Cells(Rl, NwsLast1)) = Arr()


Please help.

Regards,
SunilA (rep: 58) Sep 29, '21 at 10:39 am
Oops! I missed two lines out in the version posted earlier- have revised file and answer now
John_Ru (rep: 6142) Sep 29, '21 at 10:42 am
Hi John,
Thank you for your effort and solution.
Further, When I tested the macro, the result are coming, but it clears the previous changes done. I means the code is not capturing the historical data.

I am looking for the total of changes done in Column D and Column E.

I am expecting the historical changes to remain in Column D and E and whenever there is any new change whether in addition or subtraction that need to add to the historical data i.e. Column D and Column E.
For example: 
When the value 10 is added to Column C, than there to be addition of value 10 in Column D and when the value again increase by 10 than the Column D to show 20 and if column C is reduced by 10, than there to be no change in Column D i.e. Column D will remain of value 20 and the value in Column E to increase by 10 and so on.
The main purpose of running the code to capture the total of historical changes.
Please let me know for more understanding.
Regards
SunilA (rep: 58) Sep 29, '21 at 2:25 pm
Sunil. I give up.

Like Willie, I clearly cannot understand your garbled and inadequate descriptions.

This correspondence ends here.

I have wasted hours trying to help you but now refuse to do any more work on this. Final answer (and tomorrow I will probably delete this Answer.)
John_Ru (rep: 6142) Sep 29, '21 at 2:45 pm
Hi John,
I am really sorry and really appreciate from bottom of my heart for your efforts put in.
I am really very much thankful to you for all the efforts and solution you provided for me, but after testing the macro only, I will able to know that the macro giving the results I am looking for. 
You have solved all the problems in the past query and you the only person who understands my query and provided solutions every time. 
You bought me too close to my project. Without you, I will unable to come this close to my project. This might be the last project, I am asking for your help. Please don't leave in the middle of the project.
I don't know what to say.
Hope you understand. 
I will be always thankful to you.
Regards 
SunilA (rep: 58) Sep 29, '21 at 3:15 pm
I request to please don't delete the answer as it have great coding written and the file might be great learning for others.
SunilA (rep: 58) Sep 29, '21 at 3:39 pm
  Sunil. Luckily for you I woke early and felt bad about leaving you seemingly helpless (you seem to have learnt little from all the correspondence).   You mentioned "historical" yesterday but I was annoyed.I now see you said "...all the addition to the cell..." earlier but I didn't understand.   The solution is easy (and should be within your grasp to be honest). I recorded the cnmhnage in the array but to get the values to accumulate, you add the change to the current value. The line that read:
Arr(Ra, NwsAdd1) = Arr(Ra, NwsStock1) - Arr(Ra, NwsLast1)
needs to be:
Arr(Ra, NwsAdd1) = Arr(Ra, NwsAdd1) + Arr(Ra, NwsStock1) - Arr(Ra, NwsLast1)
and likewise for Arr(Ra, NwsSub1).   I've revised my answer to suit and, for clarity, I've changed the header rows C and D to read (using your terminology) "Positive stock movements until time" and "Negative stock movements until time".   I hope this works since this really is the last time I'll look at this. Also I will be cautious about any future questions from you since your questions are often unclear or expand rapidly (as they did when your Forum user name was Nitil). Please keep your future questions short but full and accurate.
John_Ru (rep: 6142) Sep 30, '21 at 12:14 am
Thank you so much John. 
I want to say you are a GEM like person with kind heart.
Thank you, Thank you so much.
I am blessed to connect with you through this forum.  
At the end of every project, you always gives me solution and will always  be remembered.
Once again Thanks.
SunilA (rep: 58) Sep 30, '21 at 12:55 am
Sunil.

Just before you finish the project, here's a minor improvement I think you will like. Change the code to add the lines in bold:
        
    'write array back to same range
    .Range(.Cells(NwsFirstRow, 1), .Cells(Rl, NwsLast1)) = Arr
    'add time to stock header
    .Cells(1, NwsStock1) = "Day factory stock (at " & Time & ")"
John_Ru (rep: 6142) Sep 30, '21 at 1:17 am
Thank you John
SunilA (rep: 58) Sep 30, '21 at 1:25 am
Hi John,
I tested the macro, In my production, there is formula in Column C (the value Automatic derived from RTD). So, when the macro run, it clears the formula every time from the Column C.
Can you please able to help me here, if possible.
Regards,
SunilA (rep: 58) Sep 30, '21 at 7:36 am
Sunil. This is crazy- you post a question file (with no formula in column C), there's a long discussion (as your question changes/ clarifies) then you say the file in the question was NOT representative of the sheet you want to use it on in reality. That means re-working the macro by someone..

Compare this question with another recent one with translation problems fixing error permission denied. Even when I had to guess what was going on, it was a short Q&A with a solution after a few steps.

I don't want to re-work this (or see another iteration of this question/ file) but suspect you just need to change the column numbers in the enumeration section (Enum Nws) to avoid your production column C (and allow for the hidden column). Good luck!
John_Ru (rep: 6142) Sep 30, '21 at 9:10 am
Hi John,
I have tried your solution in my production but i was unsuccessful. I also put formula in your solution file and run macro, there also i was unsuccessful. 
I don't know much of VBA macro and you know how to crack the solution. You only have the solution for this problem and with your knowledge, i feel 99% job is done, it will left only for 1% to complete.
Hence, I am really in need of your help.
Can I request you to please, please, please help me this last time in this project at your convenient time, else i will unable to use this file for the said project, which i feel very bad that all our effort will go for vain.
In future, I will keep in my mind and will make double sure before writing question and try to keep the question short, clear, precise and to the point, but this time please help me. Please grant my request this time.

Regards,   
SunilA (rep: 58) Sep 30, '21 at 10:17 am
Sunil, I won't be helping on this. There was no mention of a formula nor any sign of one in your spreadsheet.so my solution worked on that basis.

It really is up to you now, I won't be adding to this discussion.

Good luck, as I said.

@Don - any comments to make on my or this user's behaviour here?
John_Ru (rep: 6142) Sep 30, '21 at 10:23 am
Hi John/ Don, I am really sorry, if my behavior was wrong.
Thank you John for your kind help.
Regards
SunilA (rep: 58) Sep 30, '21 at 10:56 am
Hi John,
Please help once.
SunilA (rep: 58) Oct 1, '21 at 7:20 am
Sunil. Your apologies above ring hollow. 

You are not listening (to my suggestions to fix whatever production file you have or to the finality of my statement).

This question is far too long and needs to end now. Please do NOT add further to this discussion.
John_Ru (rep: 6142) Oct 1, '21 at 7:45 am
Add to Discussion


Answer the Question

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