Ga naar hoofdinhoud

Hoe herhaal of loop je elke X minuten een macro in Excel?

Als u met Microsoft Excel werkt, moet u mogelijk macro's maken om bepaalde bewerkingen uit te voeren. U wilt bijvoorbeeld een macro maken om automatisch een reeks gegevens naar een nieuwe plaats te kopiëren. Aangezien de gegevens regelmatig worden gewijzigd, moet deze macro automatisch elke 5 minuten worden uitgevoerd zonder deze handmatig te activeren om deze twee gegevensbereiken te synchroniseren. Hoe bereik je dit? De methode in dit artikel kan u helpen.

Herhaal of loop elke X minuten een macro in Excel


Herhaal of loop elke X minuten een macro in Excel

De volgende VBA-code kan u helpen om elke X minuten een macro in Excel te herhalen. Ga als volgt te werk.

1. druk op anders + F11 toetsen tegelijkertijd openen Microsoft Visual Basic voor toepassingen venster.

2. In de Microsoft Visual Basic voor toepassingen venster, klik dan Invoegen > Module. Kopieer en plak vervolgens de onderstaande VBA-code in het Code venster. Zie screenshot:

VBA-code: herhaal of loop elke X minuten een macro in Excel

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

Note: Vervang deze regel in de code 'Voer hier uw code in met de code die je elke X minuten uitvoert.

3. druk de F5 sleutel om de code uit te voeren. Bij het opduiken Kutools for Excel dialoogvenster, voer de intervaltijd in waarop u de macro wilt herhalen op basis van en klik vervolgens op OK knop. Zie screenshot:

Vanaf nu wordt de bepaalde macro elke 5 minuten herhaaldelijk in uw werkmap uitgevoerd.

Note: Als u de uitvoering van de macro moet stoppen en het interval van de cyclus wilt wijzigen, kopieer dan de onderstaande VBA-code naar dezelfde Module venster en druk op de F5 sleutel om de code uit te voeren. Vervolgens wordt de macro gestopt. Voer de bovenstaande code opnieuw uit om een ​​nieuw interval op te geven.

VBA-code: stop de uitvoering van de macro

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

Office Tab - Bladeren, bewerken en beheren van werkmappen met tabbladen in Excel:

Office Tab brengt de interface met tabbladen zoals te zien in webbrowsers zoals Google Chrome, Internet Explorer nieuwe versies en Firefox naar Microsoft Excel. Het zal een tijdbesparend hulpmiddel zijn en onvervangbaar in uw werk. Zie onderstaande demo:

Klik voor een gratis proefversie van Office Tab!

Office-tabblad voor Excel


Gerelateerde artikelen:

Beste Office-productiviteitstools

🤖 Kutools AI-assistent: Een revolutie teweegbrengen in de data-analyse op basis van: Intelligente uitvoering   |  Genereer code  |  Aangepaste formules maken  |  Analyseer gegevens en genereer grafieken  |  Roep Kutools-functies aan...
Populaire functies: Zoek, markeer of identificeer duplicaten   |  Verwijder lege rijen   |  Combineer kolommen of cellen zonder gegevens te verliezen   |   Ronde zonder formule ...
Super opzoeken: Meerdere criteria VLookup    VLookup met meerdere waarden  |   VOpzoeken over meerdere bladen   |   Fuzzy opzoeken ....
Geavanceerde vervolgkeuzelijst: Maak snel een vervolgkeuzelijst   |  Afhankelijke vervolgkeuzelijst   |  Multi-select vervolgkeuzelijst ....
Kolom Beheerder: Voeg een specifiek aantal kolommen toe  |  Kolommen verplaatsen  |  Schakel de zichtbaarheidsstatus van verborgen kolommen in  |  Vergelijk bereiken en kolommen ...
Uitgelichte functies: Raster focus   |  Ontwerpweergave   |   Grote formulebalk    Werkmap- en bladbeheer   |  resource Library (Auto-tekst)   |  Datumkiezer   |  Combineer werkbladen   |  Cellen coderen/decoderen    Stuur e-mails per lijst   |  Super filter   |   Speciaal filter (filter vet/cursief/doorhalen...) ...
Top 15 gereedschapsets12 Tekst Tools (toe te voegen tekst, Tekens verwijderen, ...)   |   50+ tabel Types (Gantt Chart, ...)   |   40+ Praktisch Formules (Bereken leeftijd op basis van verjaardag, ...)   |   19 Invoeging Tools (QR-code invoegen, Afbeelding invoegen vanaf pad, ...)   |   12 Camper ombouw Tools (Getallen naar woorden, Currency Conversion, ...)   |   7 Samenvoegen en splitsen Tools (Geavanceerd Combineer rijen, Gespleten cellen, ...)   |   ... en meer

Geef uw Excel-vaardigheden een boost met Kutools voor Excel en ervaar efficiëntie als nooit tevoren. Kutools voor Excel biedt meer dan 300 geavanceerde functies om de productiviteit te verhogen en tijd te besparen.  Klik hier om de functie te krijgen die u het meest nodig heeft...

Omschrijving


Office-tabblad Brengt een interface met tabbladen naar Office en maakt uw werk veel gemakkelijker

  • Schakel bewerken en lezen met tabbladen in Word, Excel, PowerPoint in, Publisher, Access, Visio en Project.
  • Open en maak meerdere documenten in nieuwe tabbladen van hetzelfde venster in plaats van in nieuwe vensters.
  • Verhoogt uw productiviteit met 50% en vermindert honderden muisklikken voor u elke dag!
Comments (32)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
klo mengulang Makro sebanyak 3 kali, atau seberapa kali yang kita mau, itu gi mana yahh ? trima kasih sebelumny...
This comment was minimized by the moderator on the site
Hi Rizal,

The following VBA code can help. Please give it a try. Thank you.
Notes: In the code, you need to configure the following lines to meet your needs:
1) In this line: If Val(xNum) = 3 Then
Here the number 3 represents the number of times you want to repeat the macro. After three times looping, the macro will stop. Please change it to the number of times you need.
2) In this line: Application.OnTime Now() + TimeValue("0:" + "0" + ":10"), "ReRunMacro"
The number 10 here means that the macro will repeat every 10 seconds. You can specify the hours, minutes and seconds as you need.
Sub ReRunMacro()
'Updated by Extendoffice 20230203
Dim xMin As String
Dim xNum As String
'Insert your code here

Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim J As Long
    Dim K As Long
    I = Worksheets("Sheet1").UsedRange.Rows.Count
    J = Worksheets("Sheet2").UsedRange.Rows.Count
    If J = 1 Then
    If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0
    End If
    Set xRg = Worksheets("Sheet1").Range("C1:C" & I)
    On Error Resume Next
    Application.ScreenUpdating = False
    For K = 1 To xRg.Count
        If CStr(xRg(K).Value) = "Done" Then
            xRg(K).EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A" & J + 1)
            J = J + 1
        End If
    Next
    Application.ScreenUpdating = True

    xMin = GetSetting(AppName:="Kutools", Section:="Macro", Key:="min", Default:="")
    xNum = GetSetting(AppName:="Kutools", Section:="Macro", Key:="Num", Default:="")
    If xMin = "Exit" Then
    SaveSetting "Kutools", "Macro", "min", "False"
    Exit Sub
    End If
    
    If xNum = "" Then xNum = "1"
    If Val(xNum) = 3 Then 'Here the number 3 represents the number of times you want to repeat the macro. After three times looping, the macro will stop
        xNum = 1
        SaveSetting "Kutools", "Macro", "Num", "1"
        Application.OnTime EarliestTime:=TimeValue("17:00:00"), Procedure:="ReRunMacro", Schedule:=False
        Exit Sub
    End If
    xNum = Str(Val(xNum) + 1)
    SaveSetting "Kutools", "Macro", "Num", xNum

    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:" + "0" + ":10"), "ReRunMacro" 'The number 10 here means that the macro will repeat every 10 seconds. You can specify the hours, minutes and seconds as you need.
    Else
      Exit Sub
    End If
End Sub
This comment was minimized by the moderator on the site
please make repetitions for the following commands, thanks..

Sheets("Sisa").Select
Range("D2:D12000").Copy
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Input").Select
Range("A3").End(xlDown).Offset(1, 0).FormulaR1C1 = "=R[-1]C+1"
Range("A3").End(xlDown).Offset(0, 21).Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(IF(RC[-17]="""",VLOOKUP(RC[-18]&VLOOKUP(RC[-15],Kayu!C[-20]:C[-19],2,)&RC[-13]+RANDBETWEEN(1,3)&""Belum LHP"",Sisa!C[-21]:C[-14],8,),RC[-17]),"""")"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A3").End(xlDown).Offset(0, 22).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]="""",IFERROR(VLOOKUP(RC[-19]&VLOOKUP(RC[-16],Kayu!C[-21]:C[-20],2,)&RC[-14]+1&""Belum LHP"",Sisa!C[-22]:C[-15],8,),""""),RC[-1])"
Range("A3").End(xlDown).Offset(0, 23).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]="""",IFERROR(VLOOKUP(RC[-20]&VLOOKUP(RC[-17],Kayu!C[-22]:C[-21],2,)&RC[-15]+2&""Belum LHP"",Sisa!C[-23]:C[-16],8,),""""),RC[-1])"
Range("A3").End(xlDown).Offset(0, 24).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]="""",IFERROR(VLOOKUP(RC[-21]&VLOOKUP(RC[-18],Kayu!C[-23]:C[-22],2,)&RC[-16]+3&""Belum LHP"",Sisa!C[-24]:C[-17],8,),""""),RC[-1])"
Range("A3").End(xlDown).Offset(0, 25).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]="""",IFERROR(VLOOKUP(RC[-22]&VLOOKUP(RC[-19],Kayu!C[-24]:C[-23],2,)&RC[-17]+4&""Belum LHP"",Sisa!C[-25]:C[-18],8,),""""),RC[-1])"
Range("A3").End(xlDown).Offset(0, 26).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[-1]="""",IFERROR(VLOOKUP(RC[-23]&VLOOKUP(RC[-20],Kayu!C[-25]:C[-24],2,)&RC[-18]+5&""Belum LHP"",Sisa!C[-26]:C[-19],8,),""""),RC[-1])"
Range("A3").End(xlDown).Offset(0, 4).Select
ActiveCell.FormulaR1C1 = _
"=IF(RC[22]="""",IFERROR(VLOOKUP(RC[-1]&VLOOKUP(RC[2],Kayu!C[-3]:C[-2],2,)&RC[4]+0&""Belum LHP"",Sisa!C[-4]:C[3],8,),""""),RC[22])"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
This comment was minimized by the moderator on the site
Compile error:

Expected End Sub

Mam przekopiowane dokładnie jak jest napisane wyzej i nie moge odnaleźć problemu
This comment was minimized by the moderator on the site
Hi Pawid,
Can you provide a screenshot of the error and the highlighted row in the vba code? The problem could not be reproduced in my case. Sorry for the inconvinience.
This comment was minimized by the moderator on the site
In Excel 365 I'm getting a Run-time error '13' Type mismatch on the following line: Application.OnTime Now() + TimeValue("0:" + "0:" + xMin), "ReRunMacro"
This comment was minimized by the moderator on the site
Hi Ron Franklin,I tried  it in Excel 365, but this problem could not be reproduced. 
This comment was minimized by the moderator on the site
có cách nào dừng macro khi tắt file và macro tự khởi động lại khi mở lại file không add
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Good day,You need to remove the Sub line and the End Sub line from your code.<div data-tag="code">Sub Refresh()
'
' Refresh Macro
'

'
Sheets("Sheet1").Select
ActiveWorkbook.RefreshAll
Sheets("Pivot-Dash").Select
End SubChange to:<div data-tag="code">'
' Refresh Macro
'

'
Sheets("Sheet1").Select
ActiveWorkbook.RefreshAll
Sheets("Pivot-Dash").Select
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
How to create a macro in excel with continuous loop and pressed key would be only pg up and pg down
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
Hello, Just help me out on "insert your code here". I am a novice and need help on what to fill and how ! ThanksI am not getting it right
This comment was minimized by the moderator on the site
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.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations