makro that sums every new number entered in cell

M

Mario

my problem:
i want to enter a number in A1
and everytime when u enter a number in a1 the macro adds the new number to
the old one BUT in the end there sould be the hole thing in the cell as
code...


A1 A1 - 0
input "14" in A1 A1 - 14
input "10" in A1 A1 - 24
input "-2" in A1 A1 - 22

at the end A1 = "14 + 10 - 2 " = 22

so that u can controll what is typed in

kind regards
mario
 
E

Earl Kiosterud

Mario,

Although this can be done, reconsider. The cell would eat up what you've
typed in, and you'll have no way of knowing what's been typed in and what
hasn't. Good practice is to put the entries in a list (often with
identifying information, like date, invoice number, whateer), and SUM the
list.
 
J

J.E. McGimpsey

Actually, I think the OPs proposal would preserve the data. Here's
one way:

Put this in the worksheet code module (right-click on the worksheet
tab, choose View Code, paste the code in the window that opens,
then click the XL icon on the toolbar to return to XL):


Option Explicit
Dim gvOldFormula As Variant

Private Sub Worksheet_Activate()
With Range("A1")
If .HasFormula Then
gvOldFormula = .Formula
Else
gvOldFormula = "="
End If
End With
End Sub

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Count > 1 Then Exit Sub
If .Address(False, False) = "A1" Then
If IsNumeric(.Value) Then
Application.EnableEvents = False
.Value = gvOldFormula & "+" & .Value
Application.EnableEvents = True
gvOldFormula = .Formula
End If
End If
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top