Link to home
Create AccountLog in
Avatar of Kevin Buckland
Kevin BucklandFlag for United States of America

asked on

Excel formula to report date modified

I have an Excel file (Daily Asset Allocation Report.xlsx) and I would like an Excel formula or script which reports (in cell D23, "Start Here" worksheet) the date and time that another Excel file was last modified.  

The filename/path of the file that I want to have Excel report the modified date for is always I:\Interfund Transfers\Interfund Transfer.xlsx.  

Thank you.
Avatar of Paul Cook-Giles
Paul Cook-Giles
Flag of United States of America image

here you go:

Worksheets("Start Here").Range("D23") = FileDateTime("I:\Interfund Transfers\Interfund Transfer.xlsx")

:)
Avatar of Kevin Buckland

ASKER

Sorry I know just enough to be dangerous... Can you tell me how and where to put this code?

Thank you
LOL... of course!  Do you know how to record a macro?
Yes, I can record a macro.
1. Open the workbook in which you want the date check  to run.  The "Record Macro" window will open.
2.  Start recording a macro;  name it "CheckInterfundTransfer.xlsxDate"  (or something;  you just need to be able to recognize it).
3.  Give it a shortcut key.
4.  Confirm the macro will be stored in "This Workbook"
5.  Click OK.

type a couple of letters, then stop recording.

6.  Press Alt-F11. The Microsoft Visual Basic for Applications window will open.  Find the macro you just recorded.
7.  There will be a line that looks something like this
                     ActiveCell.FormulaR1C1 = "abc"   (or whatever letters you typed)
delete that line, and paste
                     Worksheets("Start Here").Range("D23") = FileDateTime("I:\Interfund Transfers\Interfund Transfer.xlsx")

8.  Close the Microsoft Visual Basic for Applications window, and return to your workbook.
9.  Press the shortcut keys;  Start Here.D23 should be populated with the date and time of the last update.

Let me know if you get stuck.  :)
OK, I'm getting a compile error: Invalid Outside Procedure.  It highlighted "I:\Interfund Transfers\Interfund Transfer.xlsx"
ASKER CERTIFIED SOLUTION
Avatar of Paul Cook-Giles
Paul Cook-Giles
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I got something which will show you on Msg but I am still trying to get into Workbook Log:
Sub GetDate()

    Dim oFS As Object
    Dim strFilename As String

    'Put your filename here
    strFilename = "I:\Interfund Transfers\Interfund Transfer.xlsx"


    'This creates an instance of the MS Scripting Runtime FileSystemObject class
    Set oFS = CreateObject("Scripting.FileSystemObject")

    MsgBox strFilename & " was created on " & oFS.GetFile(strFilename).DateCreated & " was last modified on " & oFS.GetFile(strFilename).Datelastmodified



    Set oFS = Nothing

End Sub

Open in new window

This is another approach to get on D23, but it can't get log of modification:
Sub GetDate()

    Dim oFS As Object
    Dim strFilename As String

    'Put your filename here
    strFilename = "I:\Interfund Transfers\Interfund Transfer.xlsx"


    'This creates an instance of the MS Scripting Runtime FileSystemObject class
    Set oFS = CreateObject("Scripting.FileSystemObject")

    Cells(23, 4) = strFilename & " was created on " & oFS.GetFile(strFilename).DateCreated & " was last modified on " & oFS.GetFile(strFilename).Datelastmodified



    Set oFS = Nothing

End Sub

Open in new window

Check this with only Date & Time of Last Modification on D23:
Sub GetDate()

    Dim oFS As Object
    Dim strFilename As String

    'Put your filename here
    strFilename = "I:\Interfund Transfers\Interfund Transfer.xlsx"


    'This creates an instance of the MS Scripting Runtime FileSystemObject class
    Set oFS = CreateObject("Scripting.FileSystemObject")

    Cells(23, 4) = oFS.GetFile(strFilename).Datelastmodified

    Set oFS = Nothing

End Sub

Open in new window

Here you go...
Click on Update Result. You will get first date and time of your file. Open your source file / save / close, then click update result again, your second date and time will be update on top of the row. This way you can keep the log of your file, since last updated.
Sub GetDate()
Dim FSO As Object
Dim SourceFolder As String
Dim FileName As String
Dim Ws As Worksheet
Dim LR As Long
Set Ws = Worksheets("Sheet1")
LR = Ws.Range("B" & Rows.Count).End(xlUp).Row

SourceFolder = Ws.Range("B4").Value
Set FSO = CreateObject("Scripting.FileSystemObject")
FileName = SourceFolder & Ws.Range("B6").Value

Ws.Cells(8, 2) = FSO.GetFile(FileName).Datelastmodified

Set FSO = Nothing

If Ws.Cells(8, 2).Value = Ws.Cells(12, 2).Value Then
    Ws.Cells(12, 2).Value = Ws.Cells(8, 2).Value
    Ws.Range("A12:A" & LR).FormulaR1C1 = "=MAX(R11C1:R[-1]C)+1"
    Ws.Range("A12:A" & LR).Value = Ws.Range("A12:A" & LR).Value
Else
    Ws.Cells(12, 2).EntireRow.Insert Shift:=xlDown
    Ws.Cells(12, 2).Value = Ws.Cells(8, 2).Value
    Ws.Range("A12:A" & LR + 1).FormulaR1C1 = "=MAX(R11C1:R[-1]C)+1"
    Ws.Range("A12:A" & LR + 1).Value = Ws.Range("A12:A" & LR + 1).Value
End If
Ws.Range("A10").Select
End Sub

Open in new window

Please find attached for your reference.
Daily-Asset-Allocation-Report.xlsm
If you want to change the path - change in B4, and if you want to change the file name change the full filename with extension in B6. You can Keep log of any file by saving above workbook with another log book name.
Kevin, I'm reposting this in case you didn't see it earlier.  :)  
P

When you look at the VBA window, the macro should look like this:
Sub CheckInterfundTransferDate()
'
' MacrCheckInterfundTransferDateo2 Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
Worksheets("Start Here").Range("D23") = FileDateTime("I:\Interfund Transfers\Interfund Transfer.xlsx")
End Sub

Open in new window

If that's not the way it looks, and you can't get it to work, copy and paste the macro you created.
Thanks much, works like a charm!