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
Related
Apologies in advance as this is my first time posting something on this site and am not the best at explain issues.
I have a spread sheet, this has production data such as meters daily, meters monthly etc. These values are updated by adding TAGS from a PLC using Rockwell VantagePoint Excel add-in (if your unfamiliar with this it shouldn't matter this part is not what I am struggling with)
I need I way to copy data from one cell to another cell on the same sheet at month end. Basically the Meters monthly field needs to copied into another cell at the end of the month to record meters run for that month. The monthly meters run resets back to 0 at the end of the month.
Basically I need to copy the value in J7 into the corresponding month in W column at the end of that month. If it could ignore the year that would be advantageous as I don't need it to keep the old values and would mean I just need one column.
I have some experience at MS-Excel, also VBA but mainly in MS-Access never in MS-Excel. If answers could be explained as simply and hands on as possible it would be appreciated.
After Googling the issue I came across this formula and changed the ranges to fit my sheet but Excel doesn't like it saying it contains an error
=QUERY( A1:B6; "select B where A =date """&TEXT(TODAY();"yyyy-mm-dd")&""" "; 0
Sorry again if I haven't explained myself properly.
If your workbook isn't guaranteed to be open at the end of each month I would update the value every time it gets opened, like(Should be placed in ThisWorkbook):
'Runs when you open the workbook
Private Sub Workbook_Open()
'Loops through U3 to the last used cell in that column
For Each c In Range(Cells(3, 21), Cells(Rows.Count, 21).End(xlUp))
'Applies the J7 value to the current month and exits the sub
If Month(c) = Month(Now) Then c.Offset(, 2).Value = [J7]: Exit Sub
Next c
End Sub
Also, not that it matters but, I would apply the following formula in U3:U14 to always get the correct dates:
=EOMONTH(DATE(YEAR(TODAY()),ROW()-2,15),0)
Okay, I'm still not super sure what the question is and I know more Access VBA than Excel VBA, but here's something that might help to find a solution.
You can make a check date function that returns a Boolean value:
Public Function EoMonthCheck() As Boolean
Dim eo_month As Date, today As Date
eo_month = Format(WorksheetFunction.EoMonth(Now(), 0), "yyyy-MM-dd")
today = Format(Now(), "yyyy-MM-dd")
If today = eo_month Then
EoMonthCheck = True
Else
EoMonthCheck = False
End If
End Function
And the,, to add a value to the "W" column, we might use something like this:
Public Function AppendValue(Optional target_cell As String = "J7")
''' This could be a subroutine, too, I guess, since we're not returning anything.
Dim i As Integer
''' Activate whatever sheet you want to work with
Worksheets("Sheet1").Activate
If EoMonthCheck() = True Then
''' Look up the bottom of the 'W' column and find the first non-empty cell
''' Add 1 to that cell to get you to the next cell (the first empty one).
i = Cells(Rows.Count, "W").End(xlUp).Row + 1
''' Set the value of that empty cell in the 'W' column to the value of 'J7'
''' which will happen after we evaluate whether it is the end of the month.
Cells(i, "W").Value = Range(target_cell).Value
End If
Then, you could maybe trigger that each time the workbook opens.
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.
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'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!
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(),"")