就我个人而言,我更喜欢其他人建议解决这个问题的想法......可能使用单个单元格,填充当前日期并将日期涂成白色以隐藏它......如果不试试这个:
如果您不想拥有工作表,则可以使用外部文本文件,例如在同一目录中。当 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