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!
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 two cells A1 and A2. What I want is for A1 to take the current date mm/dd/yyyy if A2 has a value bigger than zero (0). For this I used the following formula in A1:
=IF(E2>0;(TODAY()))
So if a cell in A2 is for example 60 then A1 will be 11/03/2015.
If I save this and reopen it the next day the values in A1 will automatically change into the new date. This is something that I don't like as I want to use this method to keep track of my expenses each day. So if I type an amount in A2 I want A1 to give me the current date and keep it.
So how can I get the current date without worrying that every time I reopen the file they will change?
What you're looking for is a macro, not a formula. I am assuming you're performing this operation on COLUMNS not ROWS. EG: A1 and B1, NOT A1 and A2.
What is the difference?
Macro: Can be triggered one time on an event.
Formula: Given a input, it constantly re-evaluates the result.
Step 1) Press Alt+F11, this will open up the code editor in VBA.
Step 2) Copy and paste the code below into your 'sheet' that your formula resides in.
Step 3) Save your workbook as a macro-enabled spreadsheet. (xlsm).
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
If (Target.Columns.Count = 1 And Target.Column = 2 And Target.Rows.Count = 1) Then
'Check every row, currently this will get executed once
For Each cell In Target.Cells
'Make sure the value is a number
If (IsNumeric(cell.Value)) Then
'Make sure the value is greater than 0
If (cell.Value > 0) Then
'Check to make sure the date column is empty, so it only ever gets written once
'Note: You can remove the if statement below, if you want it to get re-evaluated every time you make a change
If (Target.Worksheet.Cells(cell.row, 1).Value = "") Then
'Using the current modified row, set column 1 to Today
'Note: Now() is the same as Today in VBA
Target.Worksheet.Cells(cell.row, 1).Value = Now()
End If
End If
End If
Next cell
End If
End Sub
Note: This macro only gets executed if you modify one row at a time, you can change it to work on multiple cells by removing the 'Target.Rows.Count = 1' part of the FIRST 'if' statement.
Note2: The last nested 'if' statement can be removed if you want the date to be re-evaluated any time a change is made to column 2. (aka Column B, in VBA columns are referenced via number, not letters).
Note3: If you really need it to work via rows, try to understand this macro by stepping through it with the debugger, and reconstruct it. Keying off of Rows instead of Columns.
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?
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(),"")
here is my problem with the execution of an excel formula.
These are the requirements:
I have several lines in an excel
sheet.
At the end of each line there is a
field called "Month".
If someone enters new values in this
line, the current month should be
inserted.
If the values get modified, the month
must not be changed.
To include the current month, I use some formula like this (A10 is just a random field which will be filled):
=IF(A10<>0; MONTH(NOW()); "")
Now I need some condition to check if the field contains a month allready, and if it is so, do nothing.
I tried the following (A15 should contain the month):
=IF(A15 <> ""; IF(A10<>0; MONTH(NOW()); "") ; )
To decide if the field should be changed by the formular, I have to check its current value. The problem is, that this will end up in an circular reference to the field itself.
Has anyone a working solution for this kind of problems?
I try not to use any VBA code in there.
You can't do it with formulas, you have to use VBA. The formula can't refer to itself.
The other problem is that NOW will change every time you recalculate. So even with your first formula, the month will not remain static.
If you decide you can live with VBA, right click on the sheet tab and choose View Code. The code you'll need will look something like this
Private Sub Worksheet_Change(ByVal Target As Range)
'We only care if cells are changed in a certain range
If Not Intersect(Target, Me.Range("A1:G10")) Is Nothing Then
'Only do this if there's nothing in row 15 for whatever column
'was changed
If IsEmpty(Me.Cells(15, Target.Column).Value) Then
'Put the month in row 15 of the current column
Me.Cells(15, Target.Column).Value = Month(Now)
End If
End If
End Sub