1

我需要一些代码来每天运行一次宏,不管你打开文件多少次宏在哪里。

如果文件没有打开一天它不必运行宏,只需在打开时执行。

我猜它必须有一个“内部”变量或类似的东西,它可以保存宏今天是否已经运行的信息。

此外,为了让它更难,我想,宏每天都会打开一个不同的工作簿。

有什么想法吗。

我是新手,如果这很清楚,请原谅我。提前致谢。

编辑:我在这里找到了一些代码:

这似乎可以做到,但你必须创建一个额外的工作表,我不想那样做。

这是代码:

Private Sub Workbook_Open()
Dim rngFindTodaysDate As Range
    With ThisWorkbook.Worksheets("Status")

        On Error GoTo X
        Set rngFindTodaysDate = .Range("A1").End(xlDown).Find(Date)
        If rngFindTodaysDate Is Nothing Then
            .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row + 1) = Date

            '''''  your Code  Here

        End If
    End With
    X:
End Sub
4

4 回答 4

2

您可以使用 Windows 任务计划程序每天自动打开一次文件。这里有一个非常好的分步教程,其中包含所需的 VB 脚本代码。

如果用户也可能手动打开文件,您将需要一个状态变量来记录该宏是否已在当天运行。最好的选择可能是有一张专门用来记录这一点的纸。也许叫它RunTimes。然后,您可以将以下行添加到您的Workbook_Open事件中:

If Date > Application.Max(Sheets("RunRecords").Range("A:A")) Then
    Call YourMacroName
    Sheets("RunRecords").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = Date
End If
于 2012-11-28T08:28:02.507 回答
2

在您的工作簿中使用(命名的)范围,一个单元格,其中最后一次运行宏的时间和日期由宏本身存储:

Sheetx.Range("rLastRun").Value2 = Now()

将其添加到宏的末尾或至少以下检查之后,您的宏检查最后一次运行单元格值是否在今天之前。那么总数将如下所示:

If Sheetx.Range("rLastRun").Value2 < Date Then

    <your macro>

    Sheetx.Range("rLastRun").Value2 = Now()

End If

对于每次打开不同的文件,您必须更加具体,因为到目前为止提供的信息我们无法提供帮助。问自己以下问题:

  1. 文件属性中是否有任何逻辑?
  2. 除了时间戳之外,它是否每次都具有相同的名称(例如Input20121128.xlsInput20121127.xls
  3. 文档名称是否在有限的可能名称池中?
  4. 它总是在同一个文件夹中吗?
  5. 它是否有特定的创建者、日期、时间……?

提供信息后,您的文件查找将是:

Dim strInputfile As String

<other code>

strInputfile = "<standardfolderstring>" & Format(Date, "dd/mm/yyyy") & " Test.xlsx"
于 2012-11-28T09:06:40.000 回答
1

这是逻辑,请仔细研究。

在工作表目标的单元格中存储一个值:例如 0 以运行宏。然后当触发宏时,将该值更改为:例如 1。然后无论工作表打开多少次并调用宏,因为单元格值为 1,宏将退出并且不完成整个过程

于 2012-11-28T08:36:14.920 回答
1

就我个人而言,我更喜欢其他人建议解决这个问题的想法......可能使用单个单元格,填充当前日期并将日期涂成白色以隐藏它......如果不试试这个:

如果您不想拥有工作表,则可以使用外部文本文件,例如在同一目录中。当 XLS 打开时,它将读取文本文件以查看当前日期,然后如果它与今天不匹配,则运行您每天一次的代码并将文本文件更新为今天的日期,否则什么也不做。

Public txt_file_location As String
Public txt_file_name As String
Private Sub Workbook_Open()

    txt_file_location = "C:\Documents and Settings\Chris\Desktop"
    txt_file_name = "test.txt"
    Dim dateToday As Date
    Dim dateInFile As Date
    dateToday = Date ' will be used for both comparison and for writing to txt file if need be
    dateInFile = txtfile_read(txt_file_location, txt_file_name)    ' On open - read the text file to check what the current date is.

    If (dateToday <> dateInFile) Then

        ' ok the date in the text file is different to today's date, so your script needs to be called here

        Call do_some_work ' a function that runs once a day...

        ' Now we need to update the textfile to todays date to prevent rerunning
        Call save_to_text_file(txt_file_location, txt_file_name, dateToday)
    Else
        MsgBox ("The script has already ran today")
    End If

End Sub
Sub do_some_work()

    ' here could be one of the functions that needs to run once a day
    MsgBox ("Some work was done!")

End Sub
Function txtfile_read(txt_file_dir, file_name)
    Dim iFileNumber As Long
    Dim strFilename As String
    strFilename = txt_file_dir & "\" + file_name
    iFileNumber = FreeFile()
    Open strFilename For Input As #iFileNumber
    Dim txt As Variant
    Do While Not EOF(iFileNumber)
        Line Input #iFileNumber, myLine
        txtfile_read = myLine
    Loop
    Close #iFileNumber
End Function
Function save_to_text_file(txt_file_dir, file_name, content_to_be_written)
    Dim iFileNumber As Long
    Dim strData As String
    Dim strFilename As String
    strFilename = txt_file_dir & "\" + file_name
    iFileNumber = FreeFile()
    Open strFilename For Output As #iFileNumber
    Print #iFileNumber, content_to_be_written
    Close #iFileNumber
End Function
于 2012-11-28T09:09:52.490 回答