Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何在Excel中每X分鐘重複或循環一次宏?

在使用Microsoft Excel時,您可能需要創建宏以實現某些操作。 例如,您要創建一個宏以自動將一系列數據複製到新位置。 由於數據將經常更改,因此您需要此宏每5分鐘自動運行一次,而無需手動觸發它以同步這兩個數據范圍。 如何實現呢? 本文中的方法可以為您提供幫助。

每隔X分鐘在Excel中重複或循環一次宏


每隔X分鐘在Excel中重複或循環一次宏

下面的VBA代碼可以幫助您每隔X分鐘在Excel中重複一次宏。 請執行以下操作。

1。 按 其他 + F11 鍵同時打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口,請點擊 插入 > 模塊。 然後將以下VBA代碼複製並粘貼到 推薦碼 窗口。 看截圖:

VBA代碼:在Excel中每X分鐘重複或循環一次宏

Sub ReRunMacro()
Dim xMin As String

'Insert your code here
    xMin = GetSetting(AppName:="Kutools", Section:="Macro", Key:="min", Default:="")
    If xMin = "Exit" Then
    SaveSetting "Kutools", "Macro", "min", "False"
    Exit Sub
    End If
    If (xMin = "") Or (xMin = "False") Then
      xMin = Application.InputBox(prompt:="Please input the interval time you need to repeat the Macro", Title:="Kutools for Excel", Type:=2)
      SaveSetting "Kutools", "Macro", "min", xMin
    End If
    If (xMin <> "") And (xMin <> "False") Then
      Application.OnTime Now() + TimeValue("0:" + xMin + ":0"), "ReRunMacro"
    Else
      Exit Sub
    End If
End Sub

備註:在代碼中,請替換此行 '在這裡插入您的代碼 使用代碼,您將每X分鐘運行一次。

3。 按 F5 鍵來運行代碼。 在彈出 Kutools for Excel 對話框,請輸入您要重複執行宏的間隔時間,然後單擊 OK 按鈕。 看截圖:

從現在開始,某個宏將在您的工作簿中每5分鐘重複運行一次。

備註:如果您需要停止執行宏並更改週期間隔,請複制以下VBA代碼到同一宏中 模塊 窗口,然後按 F5 鍵來運行代碼。 然後宏將停止,請重新運行上面的代碼以指定新的間隔。

VBA代碼:停止執行宏

Sub ExitReRunMacro()
SaveSetting "Kutools", "Macro", "min", "Exit"
End Sub

Office Tab -Excel中的工作簿的選項卡式瀏覽,編輯和管理:

Office選項卡將選項卡式界面帶到Microsoft Excel中的Web瀏覽器(如Google Chrome,Internet Explorer新版本和Firefox)中。 這將是一種節省時間的工具,並且在您的工作中不可替代。 參見下面的演示:

單擊免費試用Office選項卡!

Excel的Office選項卡


相關文章:


最佳辦公效率工具

Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
  • 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
  • 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
  • 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
  • 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
  • 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
  • 超過300種強大功能。 支持Office / Excel 2007-2019和365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能30天免費試用。 60天退款保證。
kte選項卡201905

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 每天將您的工作效率提高50%,並減少數百次鼠標單擊!
officetab底部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    creativeperson97 · 3 months ago
    I am pasting the code below in which I have replaced the line to enter the code with my code. The error I am getting is- Compile error: Expected End Sub. Kindly help.

    Sub ReRunMacro()
    Dim xMin As String
    Sub Refresh()
    '
    ' Refresh Macro
    '

    '
    Sheets("Sheet1").Select
    ActiveWorkbook.RefreshAll
    Sheets("Pivot-Dash").Select
    End Sub


    xMin = GetSetting(AppName:="Kutools", Section:="Macro", Key:="min", Default:="")
    If xMin = "Exit" Then
    SaveSetting "Kutools", "Macro", "min", "False"
    Exit Sub
    End If
    If (xMin = "") Or (xMin = "False") Then
    xMin = Application.InputBox(prompt:="Please input the interval time you need to repeat the Macro", Title:="Kutools for Excel", Type:=2)
    SaveSetting "Kutools", "Macro", "min", xMin
    End If
    If (xMin <> "") And (xMin <> "False") Then
    Application.OnTime Now() + TimeValue("0:" + xMin + ":0"), "ReRunMacro"
    Else
    Exit Sub
    End If
    • To post as a guest, your comment is unpublished.
      crystal · 3 months ago
      Good day,
      You need to remove the Sub line and the End Sub line from your code.
      Sub Refresh() ' ' Refresh Macro ' ' Sheets("Sheet1").Select ActiveWorkbook.RefreshAll Sheets("Pivot-Dash").Select End Sub
      Change to:
      ' ' Refresh Macro ' ' Sheets("Sheet1").Select ActiveWorkbook.RefreshAll Sheets("Pivot-Dash").Select

  • To post as a guest, your comment is unpublished.
    LIMCA · 9 months ago
    Hi Jack,
    By mistake I entered 0.5 mins and it shows an error,
    I mean xMin is taken as 0.5 minutes.
    How to get rid and change it to 1 min?

    • To post as a guest, your comment is unpublished.
      crystal · 9 months ago
      Hi LIMCA,
      Sorry for the inconvenience. The code does not support entering decimals.

      Please stop the the looping by running the below VBA code, and then rerun the looping code and enter 1 into the popping up dialog box.

      Sub ExitReRunMacro()
      SaveSetting "Kutools", "Macro", "min", "Exit"
      End Sub
  • To post as a guest, your comment is unpublished.
    MOhammed · 10 months ago
    How to create a macro in excel with continuous loop and pressed key would be only pg up and pg down
  • To post as a guest, your comment is unpublished.
    davart · 10 months ago
    Hi very useful, however I think I messed up and set the time as 0.5 and now I cannot chage it, any ideas how to change the xMin Setting?
  • To post as a guest, your comment is unpublished.
    David · 10 months ago
    Hi, I messed up and set the time as 0.5 and now I cannot chage it, any ideas how to change the xMin Setting?
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi David.
      As we mentioned at the end of the post, if you need to stop the execution of the macro and change the interval of the cycle, please copy the below VBA code into the same Module window and press the F5 key to run the code. Then the Macro will be stopped, please rerun the above code to specify a new interval.

      Sub ExitReRunMacro()
      SaveSetting "Kutools", "Macro", "min", "Exit"
      End Sub
      • To post as a guest, your comment is unpublished.
        David · 10 months ago
        Hey, Thanks! but im afraid the marco doenst even run, when I press F5 excel sends me this error msgs: Run time error 13 'Type Missmatch".

        I deleted it, but If I try to create it again the same error msg appears, my only guess is that I set the time as 0.5 and in the code, so no idea how to modify it now.

        Thanks Again for your help.
  • To post as a guest, your comment is unpublished.
    Padma · 10 months ago
    Hello, Just help me out on "insert your code here". I am a novice and need help on what to fill and how ! Thanks
    I am not getting it right

    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi Padma,
      The "insert your code here" indicate the VBA code you use to achieve some operation in Excel. Supposing you need to apply VBA to move a row to another sheet based on a specific value, and want to run the code every X minutes automatically, you need to place the VBA code here.
  • To post as a guest, your comment is unpublished.
    Mohit · 1 years ago
    Hey

    I have a query, your codes works perfectly fine and copies the data in my next columns every 5 minutes but after 3 hours all my columns get full as it is 2003 excel. Could you please help me with a code to check if no other column is vacant then create a new sheet and copy paste data into that sheet
  • To post as a guest, your comment is unpublished.
    ram2093@gmail.com · 1 years ago
    Please provide a Code for :


    "How to repeat or loop a Macro every X Seconds in Excel"


    Thank you
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi,
      Please change the line "Application.OnTime Now() + TimeValue("0:" + xMin + ":0"), "ReRunMacro"" to
      Application.OnTime Now() + TimeValue("0:" + "0:" + xMin), "ReRunMacro"
  • To post as a guest, your comment is unpublished.
    ebolshakovv@gmail.com · 2 years ago
    Tell me more how to stop the execution of the macro and change the interval of the cycle?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Good daty,
      A new VBA code which can stop the macro has been added at the end of the article. Please have a try. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    jack1 · 2 years ago
    Tell me more how to stop the execution of the macro and change the interval of the cycle?
  • To post as a guest, your comment is unpublished.
    Ryan · 2 years ago
    Hi!
    This is great, but is there a way to get it to loop back up to the first row of the spreadsheet before beginning again?


    Thanks!
  • To post as a guest, your comment is unpublished.
    JIm · 3 years ago
    Thanks for this, works a treat. I'm a novice to coding so please forgive me if this is simple...

    How do I get the dialog box to appear again? I have it stuck on a 1 minute loop. Thanks.
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good Day,
      Just break the code and run it again.
  • To post as a guest, your comment is unpublished.
    primeacademyresource@gmail.com · 3 years ago
    Great one! Please how do i stop the loop after say 10 times?
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good Day,
      You can get into the Code window to break the code (click on the Break button) manually to stop the loop.
      • To post as a guest, your comment is unpublished.
        Jack · 2 years ago
        Good day! I did not understand how to break the macro code to set the loop execution time again.Could you describe in more detail how to do this? Where is the value of the run cycle time recorded?
        • To post as a guest, your comment is unpublished.
          crystal · 2 years ago
          Hi Jack,
          My mistake.
          A new VBA code which can stop the macro has been added at the end of the article. Please have a try. Thank you for your comment.