How to Repeat or Loop a Macro Every X Minute in Excel?


With the use of macros, Microsoft Excel is a strong tool that lets users automate monotonous activities. Macros are sets of instructions that can be written or recorded to carry out a particular set of tasks. Some duties, however, must be performed on a regular basis. Consider a task that must be completed every 30 minutes, such as updating a dashboard or retrieving data from a web source.

This tutorial will show you how to set up an Excel macro to run automatically at predetermined times. By the time you've finished reading this manual, you'll be able to loop your macro effectively, optimise your workflow, and save a tonne of time and effort.

Repeat or Loop a Macro Every X Minute

Here we will first create a VBA module and then specify the time interval to complete the task. So let us see a simple process to learn how you can repeat or loop a Macro every X minute in Excel.

Step 1

Consider an Excel workbook.

First, right-click on the sheet name and select View code to open the VBA application.

Right-click > View Code.

Step 2

Then click on Insert and select Module, then copy the below code into the text box.

Insert > Module > Copy.

Code

Sub ReRunMacro()
   Dim xMin As String
   Dim appName As String
   Dim sectionName As String
   Dim keyName As String
   appName = "MyApp"
   sectionName = "MySection"
   keyName = "min"

   'Insert your code here
   xMin = GetSetting(appName:=appName, Section:=sectionName, Key:=keyName, Default:="")
   If xMin = "Exit" Then
      SaveSetting appName, sectionName, keyName, "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:="MyApp for Excel", Type:=2)
      SaveSetting appName, sectionName, keyName, xMin
   End If
   If (xMin <> "") And (xMin <> "False") Then
      Application.OnTime Now() + TimeValue("0:" & xMin & ":0"), "ReRunMacro"
   Else
      Exit Sub
   End If
End Sub

Step 3

Then click F5 to run the module. Then enter the interval time and click OK to complete the task.

This is how you can repeat a macro every x minute in Excel.

Conclusion

In this tutorial, we have used a simple example to demonstrate how you can repeat or loop a Macro every X minute in Excel to highlight a particular set of data.

Updated on: 27-Sep-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements