• Date File Modified (2002)

    Author
    Topic
    #422491

    Is there a method by which a date can be automatically updated each time the file is changed. For example, if I have todays date in cell A1 and then I open the file and modify it tomorrow, is it possible to automatically change the date to tomorrows date when this change takes place?

    Viewing 1 reply thread
    Author
    Replies
    • #963759

      (Edited by sdckapr on 01-Aug-05 10:26. Oops, forgot to disable events, to prevent recursive calls…)

      You could add some code, like this, to the thisworkbook object:

      Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
          Application.enableevents = false
          Worksheets(1).Range("A1").Value = Date
          Application.enableevents = true
      End Sub

      Change the sheet to store the date as appropriate. Beware, any change to any cell will change the date. Having code like this in the workbook, will clear the UNDO stack (you will no longer have undo).

      Steve

    • #963764

      You can create a Workbook_BeforeSave event handler:
      – Activate the Visual Basic Editor (Alt+F11)
      – Double click ThisWorkbook.
      – Type or copy/paste the following code, modifying it as needed:

      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      WorkSheets(“Sheet1”).Range(“A1”) = Date
      End Sub

      – Each time the workbook is saved, the value in cell A1 on Sheet1 will be updated; if the workbook is closed without saving it, the value will remain unchanged.

      • #963784

        thumbupA before save makes more sense than a change event.

        That is what I get for answering the question instead of determining the “real question” blushSteve

      • #963786

        Hans, thanks. I have tried, but must be doing something incorrectly.

        I have the target file open. Select Alt+f11 which opens VB. I paste the code into the editor then go back to the file and make change, save but don’t see anything happen in A1. I may be missing it by not double clicking “ThisWorkbook” but don’t see that as an option…

        • #963790

          It won’t work if you put the code in a standard module, it MUST be in ThisWorkbook. See screenshot below.

          • #963816

            Get this: Runtime error 9. Subscript out of range…

            • #963819

              Do you have a worksheet named “Sheet1” in the workbook? If not, then you need to modify the code to change the sheet name to the sheet where you want the date inserted.

            • #963830

              Tried but still get error. Here is file.

            • #963840

              (Edited by sdckapr on 01-Aug-05 15:02. Added PS)

              Try:

              Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
                  Worksheets("DetailFinancialAnalysis").Range("A1") = Date
              End Sub

              Steve
              PS. Or are you after:
              Worksheets(“DetailFinancialAnalysis”).Range(“B2”) = Date

            • #963845

              I don’t understand – there is no code in your workbook. No wonder it doesn’t work. confused

            • #963853

              Your workbook did not have any code in it, so I can not tell you what you were doing wrong. Try the attached workbook.

            • #963856

              whisperYou did not attach a workbook…

              Steve

            • #963858

              Thanks. I am having connection problems, and the attachment apparently got dropped when the link went down again.

            • #963993

              I had code in it, but had to delete to get file small enough to send. This works. The .value at the end of the 2nd line must have been the difference.

              Thanks for all the help and sorry if I caused any confusion.

            • #963820

              In

              WorkSheets(“Sheet1”).Range(“A1”) = Date

              you must edit “Sheet1” to the name of an actual existing woprksheet in the workbook.

    Viewing 1 reply thread
    Reply To: Date File Modified (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: