Runs once, then never again - excel

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

Related

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

Automatically Copying Value & Format - Excel

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)

how to solve this difficult Excel formula

I'm working on excel file, I have 2 columns A and B, I've set the value of B to this statement =IF(A1=1,NOW(),"") and every thing is going well, when I write 1 B will equal to Current Date, and if write anything else B will equal to nothing,
the problem is:- every time I set A to 1 the values for B column will change to the current date, I want only the current B for example B5 to change to current date not all Bs.
so can anyone help me...
It sounds like you are looking for a timestamp for when the cell in column "A" had data written into it. If so, you may want to try a VBA trigger on the worksheet. Please try pasting the below code into the worksheet you want to target. ( Alt-F11 when excel is open, then double click the relevant worksheet )
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
Target.Offset(0, 1).Value = Now()
End If
End Sub
If you truly want the timestamp in column B to generate ONLY when the value in column A is set to a numeric value of 1, then substitute the second line of code with this:
If Not Intersect(Target, Range("A:A")) Is Nothing And Target.Value = 1 Then
You will not be able to get the =NOW() formula to stop showing the current system date & time whenever the workbook re-calculates without copy-pasting only the value from that cell manually & overwriting the formula ( what #pnuts suggested in the comments ) . I hope this vba workaround is what you need, though!

Refer cell value to range of cells on another sheet

Looking for a formula for specific range of values to a reference range on another sheet...
i.e. if cell A1 sheet 1 contains a specific value, then i would like it to find that value on column A of sheet 2 and result with equivalent row value of column B
I have a range on sheet 2 showing temperatures in column A with equivalent adjustment values in column B to go with each particular temperature.
I would like a formula that automatically fills in the temperature adjustment value into cell K23 on sheet 1 when I input the temperature into cell J22 (to be more specific)
Thanks for any help in advance!!!!
Here is some 'bare bones' code with some main concepts for you to start work with.
You will notice that you have received a number of downvotes for your Q. This is likely becase the spirit of this site is for you to demonstrate that you have made some attempt and/or research to 'help yourself' before posting your Q. For this reason I have not included any explanation.
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.Intersect(Target, Range("J22")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
adj = Application.VLookup(Target.Value, Sheets("Sheet2").Range("A1:B10"), 2, False)
If IsError(adj) Then
Range("K23") = 0
Else
Range("K23") = adj
End If
End Sub

Excel cell value update macro

I have a spread sheet for tracking different savings, so column A has a name, B has a currency value. I want to be able to enter a value in column C and have it update the B cell next to it, then return to 0. For example:
B1 = £50.00
I type -£12.00 in C1
B1 = £38.00
C1 = £0.00
I thought there would be a built in function, but I can't find one. I think I will need to write a macro to do this. Can anyone show me how this would be done?
You'll want to add the following code to the sheet your working on. Right Click the sheet name Tab and choose View code and paste this in.
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 = 3 Then 'Only Runs if Cell being changed is in column C, Might need to be Columns
Application.EnableEvents = False ' Stop macro changes calling function repeatedly
FirstNum = Target.offset(-1,0).Value ' Value in Column B
SecNum = Target.Value ' Value being typed in C
Target.Offset(-1,0).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
Application.EnableEvents = True ' Re-enable the macro call
End if
End Sub
Seems like you are wanting to do something here that is a little different to the way that excel would normally work. Suggest you use more rows then you wouldn't need a macro. e.g. B1 = $50 C1=-$12
then B2==IF(C1<>"",B1+C1,""). Then you copy cell B1 down to propegate the formulae in cells below. You enter you next value in C2. Does this do what you need?

Resources