Excel - keep sum value when deleting value from cell - excel

I have very basic knowledge with ms excel, I don't really need it. But at the moment I could really use it. My problem is with summing. I have 13 cell which are summed.
Now every time I delete the value or change it keeps recalculating my sum cell. I would like that it keeps adding i.e. I have cells from D1 to D13 and I sum it with SUM(D1:D13) now if I put in D2->3 and in D5->2 the sum is 5 if I change D5 to 3 the sum will be 6 but I would like it to be 8. So it keeps adding the values if cells.
I really appreciate any help

This is my solution using vba:
Private Sub Worksheet_Change(Byval Target as Range)
if not intersect(Range("D13,D20"),target) is nothing then
Range("D37") = Range("D37") + target.value
end if
end sub

Related

Runs once, then never again

I posted a question to Stack Overflow about creating a Excel Macro to add/subtract a value in a column from the previous column, then clear the current column.
Excel cell value update macro
The code worked find for years in office 2007, but no longer works in office 365 (2016/2019).
If F1 has a value of £100.00, and I type -50 into G1, F1 changes to £50.00 and G1 is empty.
Each cell in column F is changed by an amount entered into the row in column G.
In Office 365 versions of Excel this macro executes once then won't run again until I close an re-open the document. Has something changed about the way macros work that would cause this?
Edit:
Here is the code (attached to sheet 1), slightly modified from my previous question to work on column G.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FirstNum As Currency 'Long is for number currency should help keep format
Dim SecNum As Currency
If Target.Column = 7 Then 'Only Runs if Cel l being changed is in column C, Might need to be Columns
FirstNum = Target.Offset(0, -1).Value ' Value in Column B
SecNum = Target.Value ' Value being typed in C
Target.Offset(0, -1).Value = FirstNum + SecNum ' Makes Cell B equal to difference of previous value and value typed in C
'MsgBox ("Difference Found") ' Just to display code worked Remove when confirm code works
Target.Clear ' Clears Value you typed
End If
End Sub
I don't have enough rep to do a comment...
Without seeing your full code it's hard to know. But if your code has
application.enableEvents = false
You will need to to set it back to True eventually. Or worksheet change events won't fire off until the workbook is closed and opened again
I think it is nothing wrong with Office 365...
In order to work as you explain, the code must look like:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FirstNum As Currency
Dim SecNum As Currency
If Target.Column = 3 Then 'Only Runs if Cell being changed is in column C, Might need to be Columns
FirstNum = Target.Offset(0, -1).Value ' Value in Column B
SecNum = Target.Value ' Value being typed in C
Target.Offset(0, -1).Value = FirstNum - SecNum ' Makes Cell B equal to difference of previous value and value typed in C
Target.Clear
End If
End Sub
But, you must understand that this code works only for changing cells in column C:C.
The variant you received six years before worked only in the same column, but on vertical. I mean, changing of C2, it used to modify C1. But it had a bug. If you try modifying of C1 it will try to set an Offset of -1, row zero does not exist in Excel and returns an error...
I cannot understand how the event functioned at least once for ranges G1, F1. It could work only if some links exists in the sheet, or you adapted the code and in such a case the real used code will give us the possibility to analize and tell you, not supposing, where the problem is. I do not think it is a matter of Office version, but who knows...
It sounds like a formula would suffice. However a cell cannot refer to itself, you could try,
E1: =F1+G1
Where E1 is a new cell with the sum of F1 and G1, because you are entering its sign operator i.e. - or + the you can leave it as addition in cell E1. To use this for a column just drag from the lower left square on the cell E1 all the way down to the row number you want. This will refresh every time a value changes.
excel image

Sum a value from diferent sheets and return the result on same cell

So, I'm trying to sum a cell from different sheets but I want the sum to be on the same cell or at least the result. The cell that is first summing, is gonna get deleted after the sum.
In this case, the cell A1 is gonna get deleted.
The code only pastes the formula but doesn't do it.
Private Sub C2_Click()
Sheets("Prueba").Range("A1").Formula = "Sum(Prueba!A1, Reporte!C5)"
End Sub
No error messages though.
Well as Cybernetic.Nomad said you need to use the equal sing = in from of the formula you want. Remember, this is like you where typing in the cell that formula.
Another tip: you can use this
Private Sub C2_Click()
Sheets("Prueba").Range("A1").value= Evaluate("=Sum(Prueba!A1, Reporte!C5)
End Sub
This way you tell VBA to get the value returned from the SUM and put it inside the cell A1 as a value, not formula.
Another tip:
Private Sub C2_Click()
Sheets("Prueba").Range("A1").Formula= "=SUM(Prueba:Reporte!A1:C5)"
End Sub
This way you can SUM across the sheets and all the sheets in between those sheets any value in the range A1:C5. Where you have "Prueba", "Pueba01", "Pueba02", "Prueba03" and "Reporte", all the values in the range A1:C5, in the sheets between "Prueba" and "Reporte" will be summed. If there is any other sheet outside this to sheets wont be summed in the result.
Also you can use:
Private Sub C2_Click()
Sheets("Prueba").Range("A1").value= Evaluate("=SUM(Prueba:Reporte!A1:C5)")
End Sub

need formula with a temp variable

we have all added information in our heads and then written the results down,
for example a simple tally, yesterday I sold 10 apples, the day before I sold 5 for a total of 15, today I sold 5 for a total of 20, a simple equation we all do every day in our head without even thinking about it, the formula I believe would basically read: A+B=Bnew
where A would be the daily sale.
B would be the total sale
and Bnew is the new total.
how do I do this in excel without taking pages of running total lines, or fancy visual basic script.
I want to use at most 3 cell's
Cell-1 = changing variable, (the daily sale)
Cell-2 = the running total of all sales to which A1 will be added.
and if needed Cell-3 to hold the contents of Cell-2 like our memory holds it while we update the total.
a very simple math problem, but driving me nuts to try to get excel to do it, I have searched and searched but I don't even know the right question to ask.
thank you for your help
Given the restrictions:
No helper columns ("without taking pages of running total lines")
No VBA ("no fancy visual basic script")
"I want to use at most 3 cell's"
I think a remaining option is iterative calculation? I don't know, maybe someone else can think of a better solution. But regarding iterative calculation, here's how I've set my sheet up:
This uses two cells (ignoring the labels in row 1):
In cell A2, I have the formula =A2+B2
Cell B2 is blank (ready to have some number entered)
I then go to File > Options > Formulas > Enable iterative calculation > Change "Maximum Iterations" to 1 > OK
Any notifications regarding circular references should now disappear, and if I put a number in cell B2, cell A2 updates in the manner you described in your post. Hopefully, you can replicate this on your machine too.
The thing to note is the iterative calculation setting you'll change is application-wide, so I believe it affects all other workbooks. Something to keep in mind.
If your setup looks like this in Sheet1:
then a small vba macro in the worksheetmodule from Sheet1 should do the trick:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
Range("B2").Value = Range("B2").Value + Range("A2").Value
End If
End Sub
This subroutine will run after every worksheet change. It will check if the change was in cell A2. If that's the case the value in A2 will be added to the value in B2.
Assuming you're okay with a small amount of VBA code, you could try putting the code below into the worksheet module for the sheet where you're entering the data:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Assumes "daily sales" are entered into cell B2 and that "running total" is in cell A2
If Not (Intersect(Target, Me.Range("B2")) Is Nothing) Then
Me.Range("A2").Value2 = Application.Sum(Me.Range("A2").Value2, Target.Value2)
End If
End Sub
(Pressing Alt + F11 concurrently should open the editor, you can then find worksheet modules on the left).
If you then enter a number in cell B2, then cell A2 should update in a cumulative manner. If you enter a number in a cell other than B2 or A2, you should find cell A2 doesn't update (unlike the iterative calculation approach). We use Application.Sum instead of + operator to avoid Type Mismatch error, if either A2 or B2 contain non-numeric data.
Try it, see if it does what you're after.
Edit:
If you have multiple rows, you could try something like below.
Private Sub Worksheet_Change(ByVal Target As Range)
' Assumes "daily sales" are entered into column B and that "running total" is in column A
If Target.Column = 2 Then
Me.Cells(Target.Row, "A").Value2 = Application.Sum(Me.Cells(Target.Row, "A"), Target)
End If
End Sub

Solve for value without using Solver

I currently have the following in excel for other users.
The Number column does not change. Users will input values in Multiplier 1 and Multiplier 2 columns and they will get a value in Outcome column.
Is there an automatically way where the user can also input a value in Outcome column and the Multiplier 1 column will change?
Currently Number and Outcome column are protected, will I need to unprotect the Outcome column?
I recommend you use some VBA code using the Sub Worksheet_Change(ByVal Target as range) in the worksheet.
If the user enters a value in D2 it will put the formula "=D2/(A2*C2)" in B2
If the user enters a value in B2 it will put the formula "=A2*B2*C2" in D2
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Cells.Count > 1 Then Exit Sub
On Error GoTo LastLine
If Not Intersect(Target, Range("D2")) Is Nothing Then
Range("B2").Formula = "=D2/(A2*C2)"
ElseIf Not Intersect(Target, Range("B2")) Is Nothing Then
Range("D2").Formula = "=A2*B2*C2"
End If
LastLine:
Application.EnableEvents = True
End Sub
This can be done. Begin by noticing that your formula is so simple that you
Can easily solve for multiplier1 as a function of:
outcome divided by (number times multiplier2)
Next get tricky by adding a 1 pixel wide column that will contain your formula
for outcome to the right of (now an input not a formula) the original
outcome column.
Similarly add a 1 pixel wide col to the right of multiplier1 that contains
your new formula for multiplier1. Adjust data validation on original input
cells for multiplier1 and outcome to allow only one of the two to be
entered at a time (use isblank function appropriately in an excel custom data
validation rule for each cell).
Finally right align the two 1 pixel wide cells so that their calculated values
appear within the corresponding enterable cells when they are blank.
Yes it's tricky but it works.
Sorry this is just an outline but I have used this in an application and it
works great. No vba, so undo redo don't break. Try it you'll like it.

Excel - using a flag to control cell level calculation

I'm trying to figure out if there is a way to control calculation at a cell level. Here is what I'm basically trying to do in an If statement... I don't know how to "UseTheLastValue" of "DoThisBigLongCalc" though....
Cell A1 =IF(CalculateFlag = True, DoThisBigLongCalc, UseTheLastValueOfBigCalc)
Any help would be appreciated! The reason I'm doing this is I have a lot of formulas that I do want to calculate every time I hit F9, but also a whole lot that I don't because of their long calc time..... Thanks
This approach might help you,
1) Whatever cells you want to do a recalculate with F9, enter the formulas in the excel sheet directly. Even if you set the formula calculate method to manual, the data will be refreshed and recalculated when you press F9
2) For the cells which you do not want to do a recalculate, enter the formulas for those cells through VBA, so that whenever the code is executed, the formulas will be calculated. If not, the old value would still stay there on the cell (UseTheLastValueOfBigCalc)
This code could be triggered through a worksheet_change event which can detect a True/False on a particular cell and execute accordingly. If its true, just recalculate the formulas, if false just ignore.
Below is a code snippet which detects True in cell A1 and calculates accordingly,
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And Range("A1") = True Then
Range("B3") = Application.WorksheetFunction.Sum(Range("B1:B2"))
End If
End Sub
This code sums the values in B1 and B2 and prints in B3 only if True is entered in A1
Even if you change values in cells B1 or B2 the sum will not be recalculated, which means the old value can still be used unless A1 is changes to true.
Once True is entered, the formulas are calculated,
Hope this helps.

Resources