Take current date issue - excel

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.

Related

Emtpy a Cell If another Cell becomes empty

Good day dear community,
I currently have a problem with VBA/Excel that I can't find a solution to. What I want to achieve is not complicated, but I can't find a way.
Let's assume we have two columns. In any row of one column A the User enter a value and then I start a macro. This macro executes certain instructions. Among other things, this macro ensures that if a cell in column A has a value, then the value "Yes" is entered in the same row in column B. Now my problem: As soon as the user deletes the cell value in column A, the value "Yes" in column B should also be deleted. At first glance you might think that i can use this confdition:
=IF(A1="";"";yes)
The problem is that as soon as the user has entered a value in the cell, "yes" is immediately written in the cell, but this is not desired. Because this task should be taken over by the macro.
As a small side note: I have simplified my problem. Due to the structure of my project, only the macro is allowed to write "yes".
Thanks.
Evaluate Excel Formula in VBA
In your code, you will define the occupied range in column A, and apply the second line appropriately.
Option Explicit
Sub checkColumnRange()
' Some code
' Define the column range...
' e.g.:
Dim rg As Range: Set rg = Range("A1:A10")
rg.Offset(, 1).Value = Evaluate("IF(" & rg.Address(0, 0) _
& "<>"""",""Yes"","""")")
' Some code
End Sub

When 1 cell validates (IF condition), all other cells validate as well

I have two columns with (1) DATE/TIME COMPLETED and (2) STATUS.
(1) has a formula of =IF(B1="COMPLETED", NOW(), "")
NOTE: I have changed the B1, B2, etc.. corresponding to the row number.
It means if the (2) STATUS is "COMPLETED", it should show the current date/time in column (1), else it should show blank.
But when I COMPLETED a cell, all other cell with COMPLETED status validate/refresh as well, which marks all with same date and time value.
How can I update only the status for the cell that is updated, without changing the other cells in STATUS as well?
You really shouldn't rely on NOW() in this case, whatsoever. Excel re-validates for a number of different reasons. You could either create a macro that gets the current time and sets it as text, or do the same thing manually.
Alternatively, to convert existing formulas to literal values, follow this guide. This should be done for each row immediately following the completion of the task, so that Excel doesn't have the opportunity to incorrectly re-evaluate it.
now() gives you the current time as at the time of processing the formula. With it being a dynamic value, it will update everytime to the current date/time.
The only way I've found to be able to automate this is to use VBA (macros):
Private Sub Worksheet_Change(ByVal Target As Range)
' Assuming column 2 is actually columns "B". In case it's not, change target.column to the right column number
If Target.Column = 2 And UCase(Target.Text) = "COMPLETED" Then
'Assuming the datetime field goes to the column to the left of the above column.
Cells(Target.Row, Target.Column - 1) = Now
Else
If Target.Column = 2 Then Cells(Target.Row, Target.Column - 1).Clear
End If
End Sub
Hope this solves your issue.

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!

Excel Formula which places date/time in cell when data is entered in another cell in the same row

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(),"")

Conditional Self-Referencing Formula (only execute if cell value == "")

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

Resources