how to make an excel cell to show today's date - excel

I was trying to make an excel cell print today's date like this
=DATE(2013,3,23)
But it only prints that date until some user changes the numbers.
So please help me!

=Today()
This formula is dymanic and will always show the current date.
You could do this, or use the Date() formula, and then copy/paste values.
But even if you do this, the user can still "change the numbers" after the fact, so you can't avoid that unless you protect the sheet, or implement some sort of event-based macro to maintain the desired date.
Here is a simple macro that will always put today's date in Cell A1 no matter what the user does to it. Place this in the Worksheet's code module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub 'This will only trigger the event if the cell A1 changes. You can modify this as needed.
Target.Value = DateSerial(Year(Now()), Month(Now()), Day(Now()))
End Sub

Related

date in a cell to change when any cell in that row is modified

I'm relatively new to excel and need some advice. I'm using excel 2016. I have read through other posts and can't find anything that really matches. Does anyone know how to get a cell in a row to change to todays date only when any other cell in that row is changed? And secondly if that date in that cell is more than week old and the value of another cell in the same row is "open" then change the fill color? thanks for any insight you can provide.
You'll need some VBA for this. Get into Visual Basic Editor (VBE) with Alt+F11. In your VBAProject (your workbook in the Project window) double click the sheet that contains the cell in which you are wanting to detect a change. Then add the following::
Private Sub Worksheet_Change(ByVal Target As Range)
'Detect if cell A1 has had a change
If Not Intersect(Target, Range("A1")) Is Nothing Then
'Change cell B2 to todays date
Range("B2").Value = Now()
'Detect if value in cell C2 is changed to "Open"
ElseIf Not Intersect(Target, Range("C2")) Is Nothing Then
'Is the date in B2 older than a week from now?
If DateAdd("d", -7, Now()) > Range("B2").Value Then
'Change the cell color of B2
Range("B2").Interior.Color = RGB(0, 255, 255) 'Cyan... it's so beautiful
End If
End If
End Sub
That subroutine is a special subroutine that runs when any cell changes value in the worksheet in which the subroutine is placed. We detect if a change was made to a specific cell with that awful If Not Intersect(... line. Then it's pretty straight VBA to change values and colors of cells and do some date arithmetic and tests and whatnot.

Excel: Run A Macro On Cell Click

I am using the following code to insert todays date into a cell when a user clicks onto another cell in that row.
At the moment the code is set out like so; if a user clicks cell AQ8 then insert date into cell AS8.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Address = "$AQ$8" Then
Range("AS8").Value = DATE
End If
End Sub
however, now i want to change this code slightly so that when a user changes any AQ cell in either row then the subsequent AS cell has the date inserted into the appropriate cell for that row. So if cell AQ9 is clicked then AS9 shows todays date and when they click AQ10 then AS10 shows todays date and so on. Would this be written as a target row function? and if so how would i write something like this? Can someone please show me how i could get this to do what i need, thanks in advance
If you don't want to refer to columns by number, i.e. you want to use "AQ" instead of 43, then the following should work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = Range("AQ1").Column Then
Range("AS" & Target.Row).Value = Date
End If
End Sub
The reference to row 1 in AQ1 is a dummy, since the code just selects the .Column of that range.
I'm using Worksheet_Change because you said "when a user changes any AQ cell". If you only want this to happen when a user clicks on a cell, then you can keep using the Worksheet_SelectionChange event.
Update like that:
If Target.Column = 43 Then
Range("AS" & Target.Row).Value = Date
End If
every rows in column AQ = 43 are updated with the date.

Excel IF statement setting a fix value

I just want to know the right syntax for this IF statement.
I just want to set a specific value to a cell.
My syntax is:
=IF(HOUR(F2)>6,F2=6)
Correct me if I miss something.
You could perhaps use the Data Validation feature of Excel. For example, in the below scenario, I'm putting this data validation on cell B2. So I select it first and go to Data > Data Validation:
Then I pick some options:
A user will be able to enter only a value between 0 and 6 inclusive.
A macro that could do that would be:
To use this macro, right click on the sheet tab and click on 'View Code'. Copy and paste the below, run (play button) and save.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$2" Then
If Target.Value > TimeValue("06:00") Then
Target.Value = TimeValue("06:00")
End If
End If
End Sub
Whenever a time above 6:00 am will be entered, the macro will automatically change it to 6:00 am. Formatting of the cell may be required.

Workbook_SheetChange not triggered by change from formula in other worksheet

I'm trying to imlpement a code that displays a message when a certain condition is met. In this case, it should happen when Sheet2's A1's value is equal or bigger than 1000. This value, on the other hand, is defined by a formula located in Sheet1. I tried implementing a solution based on this thread: How can I run a VBA code each time a cell get is value changed by a formula?
So I got this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range("A1")
If Not Application.Intersect(updatedCell, Range("A:A")) Is Nothing Then
If updatedCell.Value >= 1000 Then
MsgBox "Something requires attention"
End If
End If
End Sub
When I change the value of A1 through something from Sheet2, it works, but if for example I define it as =Sheet1!A7, and change Sheet1's A7, nothing happens.
How could I make it work?
Well, the linked thread deals with the problem that you want to find out the cell that is recalculated by the current change. (Anyway, the Dependents method only works for formula on the active sheet so this would not work across sheets).
In your case, you already know that you only want to monitor one specific (formula) cell.
So, I'd simply go with this:
Put this code in sheet 1 if you know that Sheet2!A1 only depends on values on sheet1.
Just catch all changes and look at your cell each time:
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("Table2").Range("A1").Value >= 1000 Then
MsgBox "Something requires attention"
End If
End Sub
Make sure that you use Worksheets(...).Range - a blank Range can be the source of sleepless nights of error-hunting, it refers to the worksheet where the code is located if the code is in a worksheet module and to the active sheet if it is in another code module.

Copy value of cell to another cell to record vehicle mileage

I'm working on a spreadsheet to record vehicle mileage. I'd like to manually enter starting mileage in F6 and ending Mileage in G6. I'd like the value of G6 to then automatically be copied to F7 and I'd enter the ending mileage in G7- and so on. The problem I have is dealing with weekends and holidays when 2 or 3 blank cells may occur. I've tried using something like =if(D9=""," ", G6), but this doesn't work.
I've also tried this function in VBA: =IF(D9=""," ",LastNonBlankCell(G8:G39)) [D9 is a datefield] and only ended up with 0's.
Function LastNonBlankCell(Range As Excel.Range) As Variant
Application.Volatile
LastNonBlankCell = Range.End(xlDown).Value
End Function
Do you really need the blank rows for weekends or holidays? If not, set D8 to the formula =workday.intl(D7,1) then copy it down as far as needed. If you need to allow for holidays, see the Excel documentation for WORKDAY.INTL. The list of dates will now contain only workdays, so the ending mileage for one row can be copied down to the next with no special handling.
This makes weekends and holidays harder to see at a glance. That can be addressed using conditional formatting. Setup formatting for the range D8:D<whatever>, use the formula =D8>(D7+1), and set the formatting to whatever stands out for you.
The following VBA macro will achieve your end goal in a slightly different way than what you asked about.
After you have installed the macro, when you double-click on a cell in column G, the macro will copy the last entered ending mileage to the cell to the left in column F.
For example, if the last ending mileage entry was in cell G6 and you double-click in cell G9, that entry will be copied to cell F9. You can then enter the new ending mileage in cell G9.
Private Sub WorkSheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("G:G")) Is Nothing Then
Selection.Offset(0, -1).Value = Selection.End(xlUp).Value
Else
Exit Sub
End If
End Sub
To install the macro, right-click on the worksheet's name tab, select "View Code", and paste in the macro code. It won't work if you put the code into a regular module.

Resources