+ Reply to Thread
Results 1 to 5 of 5

summing up a cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2010
    Location
    Maldives
    MS-Off Ver
    Excel 2007
    Posts
    15

    summing up a cell value

    please let me know the code to add the value that i dynamically enter to a cell to another cell without changing the next cells summed value.


    it is that im using two cells. that is A1 and A2. im changing the value of A2 daily but at the same time i want to add this changing value to A1. the sum formula adds the value to A1 when i enter a value to A2 but at the same time it also deletes that figure when i delete the figure in A2. i want the added figure in A1 to be permanent neverthless i delete the figure in A2.



    please help me with this

  2. #2
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: summing up a cell value

    Paste this code into your worksheet module:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A2")) Is Nothing Then
            Range("A1") = Range("A1") + Range("A2")
        End If
    End Sub

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: summing up a cell value

    To prevent the Change event being needlessly invoked a second time (ie when A1 is altered) toggle Events - you might also want to use Val function to avoid possible coercion errors.

    Private Sub Worksheet_Change(ByVal Target As Range)
        On Error Resume Next
        If Not Intersect(Target,Range("A2")) Is Nothing Then
            Application.EnableEvents = False
            Range("A1") = Val(Range("A1")) + Val(Range("A2"))
            Application.EnableEvents = True
        End If
    End Sub
    (you could also add an IsNumeric test if you wanted to prevent numeric alpha strings being counted - eg 1Apple would add 1 to result in above form)

    P.S. Are whizbang and I now entitled to free holidays in the Maldives ?

  4. #4
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: summing up a cell value

    Ah. That makes sense, DonkeyOte. I never thought to turn off events to prevent needless processing. I guess I couldn't see the forest for all the trees.

  5. #5
    Registered User
    Join Date
    05-28-2010
    Location
    Maldives
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: summing up a cell value

    Quote Originally Posted by Whizbang View Post
    Ah. That makes sense, DonkeyOte. I never thought to turn off events to prevent needless processing. I guess I couldn't see the forest for all the trees.


    anyways thank you guys. it really solves my problem

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1