Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Adding value of two cells to get result in 3rd cell

  1. #1
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5

    Adding value of two cells to get result in 3rd cell

    I want to add the value of two cell to get output in 3rd cell with the help of VBA but the following code do not work.

    Sub add()
    Range("C2").Value = Range("A2").Value + Range("B2").Value
    End Sub

    I've already posted the question here https://excel.tips.net/T003217_Using...n_a_Macro.html
    and come to know that the VBA is correct.

    So the sample file is attached here.
    Attached Files Attached Files

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,389
    Rep Power
    10
    this is your worksheet

    _____ Workbook: Book1.xlsm ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    E
    F
    1
    2
    3
    5
    2
    4
    Worksheet: Sheet1

    This is your macro, as given by you in your uploaded file. It is incorrect. It will not work.
    Code:
    Sub add()
    Range("C8").Value = Range("B3").Value + Range("E2").Value
    End Sub
    

    This next macro below works fine for me. This is the correct macro for your worksheet
    Code:
    Sub add()
    Range("C8").Value = Range("B3").Value + Range("E3").Value
    End Sub
    Last edited by DocAElstein; 08-26-2020 at 06:57 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5
    Well, then there may be some fault by my side which I'm unable to find out.
    I'm using latest build of MS Office Pro Plus 2019. I should check the code with older version. It may help.
    Thank you!!

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,389
    Rep Power
    10
    Did you realise and understand that your macro was incorrect? The issue was that you were using an incorrect macro. The macro was incorrect. Your formula was incorrect. The macro in your uploaded workbook was incorrect. So it did not work for me for that reason.
    Your macro was using "E2" which was incorrect. You should have been using "E3"
    In your uploded workbook, your data was in row 3 , so you must use B3 and E3. Your macro was using B3 and E2. That was the problem.
    Last edited by DocAElstein; 08-26-2020 at 06:56 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5
    Yes Sir! I've realised the mistake immediately after seeing your code. And I'm thankful that the mistake was highlighted properly with red color to bring to my notice.
    But, actually, that was typed E2 instead of E3 just by mistake. The real problem is something else. Because even after correcting the code, it did not work for me.
    As shown in the image, the cell C8 remain empty always for me.
    Annotation 2020-08-26 205837.jpg

    Before posting my problem here and tips.net, I've searched a lot and try many codes, but nothing works. One of the code, I tried was-

    Sub Addition()

    Dim Add As Integer
    Dim var1 As Integer
    Dim var2 As Integer
    var1 = Range("B3").Value
    var2 = Range("E3").Value
    Add = var1 + var2
    Range("C8").Select
    ActiveCell.FormulaR1C1 = Add

    End Sub


    But it, too, failed to work for me.
    2nd.jpg


    Then only I reached to the conclusion that the problem may be somewhere else!

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,389
    Rep Power
    10
    Try running this macro once, then try your macros again, there is a small chance that it might help
    Code:
    Sub Oops()
     Let Application.ScreenUpdating = True
     Let Application.Calculation = xlCalculationAutomatic
     Let Application.EnableEvents = True
     Let Application.DisplayAlerts = True
    End Sub
    Note also that your macro must be in the same workbook and in the worksheet object code module of the worksheet in which you have the data to be added.
    Last edited by DocAElstein; 08-26-2020 at 09:42 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  7. #7
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5
    Yes..it works! Thank you!

    But there is a problem in the code

    Sub add()
    Range("C8").Value = Range("B3").Value + Range("E3").Value
    End Sub

    In order to get the result, I must run the code every time manually.

    What I want is, Real time update, means...
    as soon as I enter a value in the cell B3 or E3, the value should be updated in the cell C8 automatically and immediately, without running the code manually.

  8. #8
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,389
    Rep Power
    10
    The macro will need to be an Event macro, like the ones we have done previously
    Last edited by DocAElstein; 08-26-2020 at 10:17 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  9. #9
    Member
    Join Date
    May 2020
    Posts
    66
    Rep Power
    5
    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("C8").Value = Range("B3").Value + Range("E3").Value
    End Sub
    This works fine.

    Thank you for the support and guidance!
    Last edited by DocAElstein; 08-28-2020 at 03:24 PM. Reason: Code Tags added - use # icon in editor window to get them

  10. #10
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,389
    Rep Power
    10
    That macro is OK, if it works for you. It will recalculate everytime you select any cell anywhere in the worksheet. So it is a little inefficient, The macro will be run many times when it is not needed to run.

    This might be a bit better,
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
     If Target.Address = "$B$3" Or Target.Address = "$E$3" Then
      Let Application.EnableEvents = False
      Let Range("C8").Value = Range("B3").Value + Range("E3").Value
      Let Application.EnableEvents = True
     Else
     
     End If
     
    End Sub
    But you can use the one you prefer
    Last edited by DocAElstein; 08-26-2020 at 10:29 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

Similar Threads

  1. Replies: 3
    Last Post: 02-11-2014, 08:31 PM
  2. Replies: 2
    Last Post: 07-23-2013, 06:54 PM
  3. Adding specfic number to a available number in previous cell.
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 2
    Last Post: 03-02-2013, 01:55 AM
  4. Replies: 2
    Last Post: 01-24-2013, 09:03 PM
  5. Trim Text after 3rd Underscore but retain format
    By trankim in forum Excel Help
    Replies: 4
    Last Post: 05-13-2012, 10:44 AM

Posting Permissions

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