Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, so basaically what I want to do is:
if I enter a new value into a cell, automaticaly add this number to an existing number of another cell. So when I add a new value into B24, the value of D24 automaticaly gets added up by the value, if no value is there dont do anything. Thanks for the quick help

What I have tried:

using Sumif but I needed to eselect a range, but i only need the value of one cell. Just Sum(B24;D24) gives me an error because of ciruclation...
Posted
Updated 30-May-18 4:39am
v3

1 solution

In the VBA editor (Alt-F11 to get to it) double click on the Sheet you want this to happen on.

Enter the following code:
VB
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This checks for any entry in column B
    If Application.Intersect(Range("B:B"), Range(Target.Address)) Is Nothing Then
        Exit Sub
    End If
    
    'Or check that it is the specific range you are interested in
    If Target.Address <> "$B$24" Then Exit Sub

    'If there is nothing currently in D24 then nothing more to do
    If IsEmpty(Range("D24")) Then Exit Sub  'Note you cannot use IsBlank

    'If user did not enter a number then there is nothing more to do
    If Not IsNumeric(Target.Value) Then Exit Sub

    'If the current value in D24 is not numeric then nothing more to do
    '(Never trust a user to enter the data you are expecting!)
    If Not IsNumeric(Range("D24").Value) Then Exit Sub
    
    'Add the new value to the running total
    Range("D24").Value = Range("D24").Value + Target.Value

End Sub
Hopefully the comments are enough to guide you on what is going on. The Worksheet_Change event is fired whenever something changes on that particular sheet.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900