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
Related
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
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
Automatically Copying one/many cell's value & colour format to another cell/range of cells specified by myself. The other cell also needs to update every time the first cell changes its value or format.
I've been able to see just one or two threads with answers to this question but it still doesn't serve my purpose. Can any genius help me out in here? I don't quite understand why a basic thing like this has taken me 1 full day to figure out (the answer shouldn't be that I'm not smart :D)
Attaching an Excel example of what I want to achieve.
I want to be able to;
Update the Sheet 2 with the data from Sheet 1. (C4 in sheet 2 has to be green and have the value 5). Also, if C4 in Sheet 1 changes its value and colour, I want C4 in Sheet 2 to change automatically)
Now, the above is not only my concern. I have many cells in Sheet 1 which I want to be able to select and have them copied into Sheet 2 in respective places. (eg - c4, d4,e4 from sheet 1 ,.etc to be copied into c4, d4, e4 in sheet 2). Not necessarily I would want to choose the cells in a sequential fashion, but if there's a way for me to specify which cell needs to be copied into which cell of the other sheet, I'll be even more convinced.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ping As Boolean
If Intersect(Target, Range("A3")) Is Nothing Then
If ping = False Then
Range("A3").Copy
Range("C10").PasteSpecial Paste:=xlPasteFormats
End If
ping = True
Exit Sub
Else
ping = False
End If
End Sub
One solution can be with a user defined function. I could not try the code, but just to show the idea:
Public Function CopyFromTo(rngFrom As Range, rngTo As Range)
Application.Volatile True ' "A volatile function must be recalculated whenever calculation occurs in any cells on the worksheet"
rngFrom.Copy rngTo
CopyFromTo = rngFrom ' I am not sure what the function should return
End Function
For example, formula in cell B2 would be =CopyFromTo(A2, B2)
Hoping there is a way this can be done with a formula since I will be putting this on SharePoint as a shared workbook.
Column B contains Tasks, while Column E contains the Date and Time of when the Task was assigned. Is there a formula that would automatically enter the current date and time in Column E whenever someone entered data into column B?
Any assistance would be greatly appreciated.
Another way to do this is described below.
First, turn on iterative calculations on under File - Options - Formulas - Enable Iterative Calculation. Then set maximum iterations to 1000.
The 1000 iterations doesn't matter for this formula, but it stops excel getting stuck in an infinite loop for other circular references.
After doing this, use the following formula.
=If(D55="","",IF(C55="",NOW(),C55))
Once anything is typed into cell D55 (for this example) then C55 populates today's date and/or time depending on the cell format. This date/time will not change again even if new data is entered into cell C55 so it shows the date/time that the data was entered originally.
This is a circular reference formula so you will get a warning about it every time you open the workbook. Regardless, the formula works and is easy to use anywhere you would like in the worksheet.
This can be accomplished with a simple VBA function. Excel has support for a Worksheet Change Sub which can be programmed to put a date in a related column every time it fires.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Offset(0, 3).Value = "" Then
Target.Offset(0, 3) = Format(Now(), "HH:MM:SS")
End If
End Sub
A quick explanation. The following "if" statement checks for two things: (1) if it is the second column that changed (Column B), and (2) if the cell 3 columns over (Column E) is currently empty.
If Target.Column = 2 And Target.Offset(0, 3).Value = "" Then
If both conditions are true, then it puts the date into the cell in Column E with the NOW() function.
Target.Offset(0, 3) = Format(Now(), "HH:MM:SS")
Range.Offset
Range.Column
Not sure if this works for cells with functions but I found this code elsewhere for single cell entries and modified it for my use. If done properly, you do not need to worry about entering a function in a cell or the file changing the dates to that day's date every time it is opened.
open Excel
press "Alt+F11"
Double-click on the worksheet that you want to apply the change to (listed on the left)
copy/paste the code below
adjust the Range(:) input to correspond to the column you will update
adjust the Offset(0,_) input to correspond to the column where you would like the date displayed (in the version below I am making updates to column D and I want the date displayed in column F, hence the input entry of "2" for 2 columns over from column D)
hit save
repeat steps above if there are other worksheets in your workbook that need the same code
you may have to change the number format of the column displaying the date to "General" and increase the column's width if it is displaying "####" after you make an updated entry
Copy/Paste Code below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
Target.Offset(0, 2) = Date
End Sub
Good luck...
I'm afraid there is not such a function. You'll need a macro to acomplish this task.
You could do something like this in column E(remember to set custom format "dd/mm/yyyy hh:mm"):
=If(B1="";"";Now())
But it will change value everytime file opens.
You'll need save the value via macro.
You can use If function
Write in the cell where you want to input the date the following formula:
=IF(MODIFIED-CELLNUMBER<>"",IF(CELLNUMBER-WHERE-TO-INPUT-DATE="",NOW(),CELLNUMBER-WHERE-TO-INPUT-DATE),"")
Here is the solution that worked for me
=IF(H14<>"",NOW(),"")
I'm working on a spreadsheet to record vehicle mileage. I'd like to manually enter starting mileage in F6 and ending Mileage in G6. I'd like the value of G6 to then automatically be copied to F7 and I'd enter the ending mileage in G7- and so on. The problem I have is dealing with weekends and holidays when 2 or 3 blank cells may occur. I've tried using something like =if(D9=""," ", G6), but this doesn't work.
I've also tried this function in VBA: =IF(D9=""," ",LastNonBlankCell(G8:G39)) [D9 is a datefield] and only ended up with 0's.
Function LastNonBlankCell(Range As Excel.Range) As Variant
Application.Volatile
LastNonBlankCell = Range.End(xlDown).Value
End Function
Do you really need the blank rows for weekends or holidays? If not, set D8 to the formula =workday.intl(D7,1) then copy it down as far as needed. If you need to allow for holidays, see the Excel documentation for WORKDAY.INTL. The list of dates will now contain only workdays, so the ending mileage for one row can be copied down to the next with no special handling.
This makes weekends and holidays harder to see at a glance. That can be addressed using conditional formatting. Setup formatting for the range D8:D<whatever>, use the formula =D8>(D7+1), and set the formatting to whatever stands out for you.
The following VBA macro will achieve your end goal in a slightly different way than what you asked about.
After you have installed the macro, when you double-click on a cell in column G, the macro will copy the last entered ending mileage to the cell to the left in column F.
For example, if the last ending mileage entry was in cell G6 and you double-click in cell G9, that entry will be copied to cell F9. You can then enter the new ending mileage in cell G9.
Private Sub WorkSheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("G:G")) Is Nothing Then
Selection.Offset(0, -1).Value = Selection.End(xlUp).Value
Else
Exit Sub
End If
End Sub
To install the macro, right-click on the worksheet's name tab, select "View Code", and paste in the macro code. It won't work if you put the code into a regular module.