2012-11-28 184 views
1

我需要一些代碼來每天運行一次宏,無論打開文件的次數是多少次都無關緊要。VBA每天運行一次宏一次

如果文件有一天沒有打開,它不必運行宏,只需在打開時執行。

它必須有一個內部的」變量或類似的東西,我想,是保持信息的宏是否alredy運行不了這一天。

此外,使其更困難,我想,宏提前打開不同的工作簿中的每一天

任何想法

我是新手,所以請原諒我,如果是這樣的話清晰感謝

編輯:。。我發現了一些代碼here

似乎這樣做,但你必須創建一個額外的表,我想不這樣做。

下面是代碼:

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 
+0

你期望你的宏做什麼?如果你需要運行任何一天一次,把一個按鈕,告訴別人一下就可以每天一次,連他們打開該文件,100倍...... OMG目前還不清楚有什麼需要後悔:( – bonCodigo

+0

我需要我的宏運行每天只有一次,打開文件的次數並不重要,因爲它必須自動執行,所以我不能告訴用戶這麼做,如果你需要更多信息,請讓我知道 – themolestones

+0

我會說你保持一個值:如0在這片單元然後運行宏時,更改該值:如1那麼無論多少次單開和宏被調用,sicne細胞衣被合計爲1,宏將退出,而不是完成全過程。 – bonCodigo

回答

1

在您的單元格中使用(命名)範圍在宏運行最後時間&日期是由宏本身保存工作簿:

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" 
+0

嗨,是的,就是這樣。 1 - >這些文件有一個邏輯名稱。 2 - >只是改變日期。 4 - >始終在同一個文件夾中。該名稱將會像「2012年11月28日Test.xlsx」和日期更改dialy。如果我需要提供更多信息,請讓我知道。謝謝。 – themolestones

+0

在底部添加代碼。如果這回答你的問題,請打勾,這樣大家都知道你有幫助,不需要進一步的幫助解決這個問題。如果不是,請進一步說明這個問題。如果您有新問題,請開始一個新問題。 –

2

您可以使用Windows任務計劃程序來自動打開文件每日一次。有一個非常好的step-by-step tutorial here包含所需的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 
+0

我發現你並不熱衷於有額外的紙張。你可以隱藏它,或者如果你的宏在每次運行時都可以預測地改變它,那麼你可以在If語句中用它作爲你的條件。 –

+0

謝謝,我想我會去找那個解決方案來隱藏工作表,但我認爲它會有一個「內部變量」來保持這種信息,而不必向表單中添加額外的數據或類似的東西。 – themolestones

1

這裏是邏輯,請調查一下。

存儲值:例如0在工作表目標中的單元格中運行宏。然後,當觸發宏時,將該值更改爲:例如1.然後,無論工作表打開多少次並調用宏,單元格值爲1,宏將退出並且不會完成整個過程。

+0

謝謝,但就像我說過的,我正在尋找一些不會涉及細胞變化的東西,但無論如何,我會感謝您的努力。 – themolestones

+0

如果你會保留一張隱藏的紙張:)這是值得的做最簡單/有效的方式,如果這樣做的工作。您可以在Worksheet打開事件中調用宏。但是,如果有更多的選擇,您應該選擇相應的選項,我會把它留給你。 ;) – bonCodigo

+0

好的,我會嘗試這種方式。謝謝 – themolestones

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 
+0

謝謝,我真的很感謝你的時間和精力。問候 – themolestones

+0

無後顧之憂 - 在咖啡釀造過程中燃燒一段時間是一種不錯的方式!快樂編碼:) – Chris