• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

Adding Timestamp When a Macro Runs on Excel 2010

Background: The attached two files are Excel 2010 (Loader.xlms) that needs to be updated and the other one is a text file (SampleData) storing the data set.  Sheet1 and Sheet2 work together on this file.  User clicks on the button (Refresh Data) on Sheet2 which runs the ImportProcess macro to update the data residing on Sheet1 by opening the text file and pulling the data into Sheet1. This macro makes two macro calls one is to delete the range before the update and the other to open the text file and pull data in. DataImport macro uses external data connection and import feature.  Data set on Sheet1 needs to be updated twice a day everyday.  

Question1:   I need to add a timestamp on Sheet2 under the header TIME STAMP in column A.  Each time the user runs the macro by clicking on the "refresh data" button on Sheet2 to update the data set on Sheet1 of the Loader.xlsm file time should be captured, Excel needs to create a timestamp for this action and also needs to keep a history for each update by displaying the timestamp.  The format of the timestamp should be displaying date, hour, min, second.

Question2:  Upon resolving the first question I will also need to create an auto update process so the user does not have to manually click on the button twice a day but rather macro should run on predetermined times by itself.

Thanks in advance for help
1 Solution
Patrick MatthewsCommented:
The answer to your first question is simple. Change the code in Module1 to read thusly:

Option Explicit

Sub ImportProcess()

    Call Timestamp
    Call DeleteDataRange
    Call ExternalDataImport

End Sub

Sub Timestamp()
    Dim LastR As Long
    With ThisWorkbook.Worksheets("Sheet2")
        .Range("1:1").NumberFormat = "yyyy-mm-dd hh:mm:ss"
        LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Cells(LastR + 1, 1) = Now
        .Cells(1, 1).Resize(LastR + 1, 1).Sort Key1:=.Cells(1, 1), Order1:=xlDescending, Header:=xlYes
    End With
End Sub

Open in new window

I suggest you go with a new question for the second item.  Running that macro automatically at preset times is done best via the Windows task scheduler.
I agree with the above comment regarding update I also prefer using vbs ,run it as cscript on specific times using windows task  scheduler.
but if you insist in using excel macros then the I think the best way to do it  is to make it check on workbook open  event when you validate time,
regarding time it is better to check for text file last modify date so refresh data is only valid if there is new version of the file
trusxlsolBusiness Systems AnalystAuthor Commented:
Thank you it works the way I wanted.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now