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应用程序 窗口。

2。 在里面 Microsoft Visual Basic应用程序 窗口,请点击 插页 > 模块。 然后将以下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 · 1 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 · 29 days 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 · 7 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 · 7 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 · 8 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 · 8 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 · 8 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 · 8 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 · 8 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 · 8 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 · 8 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 · 10 months 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.