Excel cell value update macro - excel

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?

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

Updating data in a cell with data from another cell when a specific outcome is reached

I am trying to make it so that when one cell (B1) reaches a value of 0, A1 which is text entered by user, is now replaced with the data in C1 which is also data entered by the user. Meaning that formulas cannot be placed in either A1 or C1 as they are data inputted by user (B1 already has formula calculating its value.)
Instinctly I want to use =IF(B1=0,A1=C1,"") but obviously that doesn't work as A1 = C1 is checking if they equal each other, not replacing A1 with C1.
There's probably going to be a simple solution to this but I cannot figure out a way to make it work.
There is no way to have formulas and user input reside in the same field. For this you would need a VBA solution like the following.
Using the macro editor (alt+f11) place the following inside the sheet where the code needs to trigger:
Option Explicit
Private Sub Worksheet_Change(ByVal rng As Range)
Dim sh As Worksheet
'only trigger on when column 2 experiences a change
If rng.Column = 2 And rng.Value2 = 0 Then
Set sh = rng.Parent
sh.Cells(rng.Row, 1).Value2 = sh.Cells(rng.Row, 3).Value2
End If
End Sub
Note that Column B is numbered as 2 here, and C is 3, etc..
This 'overwrite' will only trigger, when column B changes to 0, it will not force C into A when the user overwrites A afterwards. If you want it to trigger whenever A, B or C change, then rewrite the trigger condition to:
If rng.Column <= 3 And rng.Value2 = 0 Then
If this answer was helpful to you, please don't forget to vote. Thanks!
You don't need the equal sign when using a cell in a formula:
=IF(B1=0,C1,"")

Clear remaining cells when value is added

In Excel I have a Table with about 8 products(8 rows).
(the table is a drop down list and can be sorted on alphabet)
The table auto expands when a new product is added at the bottom of the list.
The table is set up like explained below:
Column B = Product Number
Column C = Product Name
Column D = Certain Value
Column E = Certain Value
Column D and E are Usually empty, I want only 1 cell in the whole range(D1:E8) to contain any value. If a new value is added, all the other Cell's in this range need to be cleared.
Is This possible by using a VBA Macro? (If so... Then how?)
For Example,
D3= "x". When a string "x" is entered in cell E6, all other cells (including D3 need to become empty). I'd like to do this by starting a VBA macro, so I could add some additional actions that need to happen after one of the products is selected with an "x"in Column D or E. I know this can be done by a userform as well, but would prefer to do it this way.
In the Worksheet module of the sheet you want this to affect use:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D1:E8")) Is Nothing Then
If Target.Cells.Count > 1 Then
MsgBox "Please edit one cell at a time!"
Else
Application.EnableEvents = False
newVal = Target.Value
Range("D1:E8").ClearContents
Target.Value = newVal
Application.EnableEvents = True
End If
End If
End Sub

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

Resources