Sunday, August 2, 2009

Excel 2003 sum function question ...?

I'm a C++ programmer %26amp; have limited experiment in Excel.so I give u an example of what i need to do :





I want a function to sum B2's current value with new A1 value,for example :





cell A1 = 3


I set a function to save this value in B2.so now, I have B2=3


now I change A1 value to 5 (A1=5) %26amp; I want to have : B2=3 + 5 which means B2=8.this has to go as many times as i want

Excel 2003 sum function question ...?
You really need a VBA (Visual Basic for Applications) macro for that. You aren't going to be able to do that with just regular Excel functions because you have to have a way to detect the update and know that the update is to A1.





Instructions:





In Excel, press Alt + F11. In the Visual Basic Editor window go to the Project window and double-click on the worksheet you want it to add like that. Then paste in the following code on the code window that popped up after you double-clicked the worksheet name. I even added some portions to avoid errors, like just in case your user accidentally doesn't put in a number.








Private Sub Worksheet_Change(ByVal Target As Range)


Dim Msg1 As String, Msg2 As String


Dim MyTitle As String


Dim MsgType


Dim OldValue As Double





Msg1 = "Cell A1 must be a number."


Msg2 = "Cell B2 must be a number."


MyTitle = "Error Message"


MsgType = vbOKOnly + vbExclamation





If Intersect(Target, [A1]) Is Nothing Then


Exit Sub


ElseIf IsNumeric([A1].Value) = False Then


MsgBox Msg1, MsgType, MyTitle


Exit Sub


ElseIf IsNumeric([B2].Value) = False Then


MsgBox Msg2, MsgType, MyTitle


Exit Sub


End If





OldValue = [B2].Value





[B2].Value = OldValue + [A1].Value





End Sub
Reply:You can't have the same cell do two different functions. You should try having it in a different cell, like B3 where B3= B2 + A1.





You can manipulate your new cell much more easily than you can in the same cell.
Reply:Select the cell you want it to appear in and use = SUM(A1+B2)





I think anyway. Been a while.
Reply:One way is to create a user-defined function so that you can trace when a1 was updated. You see, Excel interprets an update as a correction.





Another way is to put your updates on different cells --one update - one cell. Then add these to b2. So, the formula for b2 then would be like = a1+a2+a3+a4+a5......

land survey

No comments:

Post a Comment