简体   繁体   English

在xlsm文件中执行的最后一个宏的VBA日期

[英]VBA date of last macro executed in a xlsm File

i want to write in 2 cells the following informations : 我想在2个单元格中写入以下信息:

Sheets("Dates").Range("A1").Value = Date '(today 's date)
Sheets("Dates").Range("A2").Value= ' ??? Date of macro executed 

Is there a code that can tell us when the macro is executed ? 是否有代码可以告诉我们何时执行宏? Not the date of last modified , so i can compare the two dates ? 不是上次修改的日期,所以我可以比较两个日期吗?

Thanks 谢谢

I do something similiar on a hidden worksheet with this code on run: 我在隐藏的工作表上执行了类似的操作,并运行了以下代码:

Dim LastRunDate As Date
Dim LastRunTime As Date
Dim CurrentDate As Date
Dim CurrentTime As Date

CurrentDate = Format(Now, "dd/mm/yyyy")
CurrentTime = Format(Now, "hh:mm:ss")

LastRunDate = WB1.Names("LastRunDate").RefersToRange
LastRunTime = WB1.Names("LastRunTime").RefersToRange
LastRunTime = LastRunTime + TimeSerial(0, 10, 0)

If CurrentDate <= LastRunDate Then
    If CurrentTime <= LastRunTime Then
        End
    End If
End If

Range(WB1.Names("LastRunDate")) = CurrentDate
Range(WB1.Names("LastRunTime")) = CurrentTime

So this just works out if the code has been run in the last 10mins. 因此,只要代码已在最近10分钟内运行,就可以解决。 I just force a save a little after that code 我只是在该代码之后强制保存一点

By last modified I'm presuming that you want the date of when the workbook was last saved? 经过最后修改,我假设您想要工作簿的最后保存日期? If so: 如果是这样的话:

Private Sub Workbook_Open()
Sheets("Dates").Range("A1").Value = NOW()
End Sub

Sub YourMacro()
'Whatever code the macro runs
Sheets("Dates").Range("A2").Value = NOW()
End Sub

Excel doesn't store data like this by default, but what you can do is create a very hidden sheet to store data between instances. Excel默认情况下不会存储这样的数据,但是您可以做的是创建一个非常隐藏的表以在实例之间存储数据。 In your case, make the hidden sheet with a row for each macro, then add code to the routine that will store the current date into a cell in that row, which you can later recall for your purposes 对于您的情况,在隐藏的工作表中为每个宏添加一行,然后将代码添加到例程中,该例程会将当前日期存储到该行的单元格中,以供以后使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM