如何一天只运行一次VBA事件



我将脚本录制到宏文件中,并使用VBA工程(personal.xlsb(调用该特定宏。每次打开任何Excel时(Workbook_open(。

但现在我只想限制每天打一次电话。例如,如果单个宏每天调用该特定宏一次,则事件将推迟到第二天,并且在打开任何Excel文件时都不会调用该宏。

请告诉我如何禁止我的活动每天只表演一次,下一次活动应该在第二天举行。

谢谢

Sub run()
Application.run "'C:Users850051636OneDrive - GenpactDesktopWorkingProject 5 WIP Stock TransferWorking QPA_Stock Transfer_Draft_v1.0.xlsm'!Module1.test"
End Sub

如下:

Sub test()
If Sheet1.Range("F1").Value <> Date Then
Sheet1.Range("F1").Value = Date 'save the date to your sheet
'<put the rest of your code here>
ActiveWorkbook.Save 'you can choose where you save
End If
End Sub

如果不存在,此函数将通过名称PropNameThisWorkbook添加Date类型的CustomDocumentProperty。如果提供的日期是按其价值提供的,则提供的日期将替换任何以前存在的日期。如果在没有值的情况下调用函数,它将返回指定属性的任何值。在这种模式下,如果属性不存在,则返回一个空白。

Private Function SetGetProperty(ByVal PropName As String, _
Optional ByVal PropVal As Date) As Variant
' SSY 050 ++
' return the value of the property

' if PropVal is not omitted,
' assign PropVal to document Property(PropName)
' create a custom property of Date type if it didn't exist

Dim Pp As DocumentProperty
On Error Resume Next
With ThisWorkbook               ' change to suit
Set Pp = .CustomDocumentProperties(PropName)

If IsDate(PropVal) Then
If Err.Number Then
.CustomDocumentProperties.Add _
Name:=PropName, LinkToContent:=False, _
Type:=msoPropertyTypeDate, Value:=PropVal
Else
Pp.Value = PropVal
End If
End If
End With
SetGetProperty = Pp.Value
End Function

请将以下代码添加到Workbook_Open过程中(例如(。

If SetGetProperty("LastDone") < Date Then
' run your update here
SetGetProperty "LastDone", Date
End If

在第一行中,函数询问属性"的值;LastDone";。您可以选择任何有效的名称。如果属性不存在,测试将评估为False。在这种情况下,或者如果属性中的日期在今天之前,则会运行更新,并且属性将设置为created/set为当前日期。

最新更新