我正在根据父母的业务开展Excel学校计划。基本上我必须创建它来以数字方式组织他们的业务数据(以前在纸上)。事情是,企业的客户通常每个月付款,所以我想每周更新他们所需的付款。我已经创建了一个命令按钮,用于更新客户端所需的付款,但我需要一种方法,只能每7天点击一次。有没有办法在Excel中执行此操作(使用或不使用VBA)。
答案 0 :(得分:0)
这是您的选择。怎么样:
因此,首先,在每次代码运行后记录单元格中运行的日期和时间:
Option Explicit
Sub test()
Dim RunDateTime As Date
'standard code
'...
'note the date and time of the run
RunDateTime= Now()
'record the date and time of the run in a workings sheet in excel
Sheets("Workings").Range("A1") = RunDateTime
End Sub
然后在每次运行开始时将此时间戳与当前日期进行比较,以决定如何继续:
(下面的完整示例代码)
Option Explicit
Sub test()
Dim LastRunDateTime As Date
Dim DaysSinceLastRun As Integer
Dim proceed As Boolean
Dim RunDateTime As Date
' Find when last run
LastRunDateTime = Sheets("Workings").Range("A1")
' Depending on days since last run decide whether to proceed or not
If LastRunDateTime = 0 Then
MsgBox "Hasn't been run before so will proceed with main code"
proceed = True
Else
' Calculate how many days since last run
DaysSinceLastRun = Now() - LastRunDateTime
If DaysSinceLastRun < 7 Then
MsgBox "Code was only run on " & LastRunDateTime & " therefore not time yet to run again"
proceed = False
Else
MsgBox "Code was last run > 7 days ago therefore will proceed with running main code!"
proceed = True
End If
End If
'standard code
If proceed = True Then
'blahblahblah
MsgBox "Running main code!"
End If
'note the date and time of the run
RunDateTime = Now()
'record the date and time of the run in a workings sheet in excel
Sheets("Workings").Range("A1") = RunDateTime
End Sub