是否可以限制/锁定excel中的命令按钮,以便每周只使用一次?

时间:2016-01-02 05:01:52

标签: excel vba

我正在根据父母的业务开展Excel学校计划。基本上我必须创建它来以数字方式组织他们的业务数据(以前在纸上)。事情是,企业的客户通常每个月付款,所以我想每周更新他们所需的付款。我已经创建了一个命令按钮,用于更新客户端所需的付款,但我需要一种方法,只能每7天点击一次。有没有办法在Excel中执行此操作(使用或不使用VBA)。

1 个答案:

答案 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