Gå til hovedindhold

Hvordan gentager eller sløjfer jeg en makro hvert X. minut i Excel?

Mens du arbejder med Microsoft Excel, skal du muligvis oprette makroer for at udføre nogle handlinger. For eksempel vil du oprette en makro til automatisk at kopiere en række data til et nyt sted. Da dataene vil blive ændret ofte, skal du bruge denne makro til at køre automatisk hvert 5. minut uden manuelt at udløse den for at synkronisere disse to dataområder. Hvordan opnås det? Metoden i denne artikel kan hjælpe dig.

Gentag eller loop en makro hvert X. minut i Excel


Gentag eller loop en makro hvert X. minut i Excel

Den følgende VBA-kode kan hjælpe dig med at gentage en makro hvert X. minut i Excel. Gør som følger.

1. Trykke andre + F11 nøgler på samme tid for at åbne Microsoft Visual Basic til applikationer vindue.

2. i Microsoft Visual Basic til applikationer vindue, klik venligst indsatte > Moduler. Kopier og indsæt derefter nedenstående VBA-kode i Kode vindue. Se skærmbillede:

VBA-kode: Gentag eller loop en makro hvert X. minut i 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

Bemærk: I koden skal du udskifte denne linje 'Indsæt din kode her med koden kører du hvert X. minut.

3. Tryk på F5 nøgle for at køre koden. I pop op Kutools til Excel dialogboksen, skal du indtaste den intervalltid, du vil gentage makroen baseret på, og klik derefter på OK knap. Se skærmbillede:

Fra nu af kører den bestemte makro gentagne gange hvert 5. minut i din projektmappe.

Bemærk: Hvis du har brug for at stoppe udførelsen af ​​makroen og ændre cyklusintervallet, skal du kopiere nedenstående VBA-kode til den samme Moduler vindue og tryk på F5 nøgle for at køre koden. Derefter stoppes makroen. Kør ovenstående kode igen for at angive et nyt interval.

VBA-kode: Stop udførelsen af ​​makroen

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

Office-fanen - Tabbed-browsing, redigering og styring af projektmapper i Excel:

Office-fanen bringer fanebladet som vist i webbrowsere som Google Chrome, Internet Explorer nye versioner og Firefox til Microsoft Excel. Det vil være et tidsbesparende værktøj og uerstatteligt i dit arbejde. Se nedenstående demo:

Klik for gratis prøveversion af Office Tab!

Office-fanen til Excel


Relaterede artikler:

Bedste kontorproduktivitetsværktøjer

🤖 Kutools AI Aide: Revolutionér dataanalyse baseret på: Intelligent udførelse   |  Generer kode  |  Opret brugerdefinerede formler  |  Analyser data og generer diagrammer  |  Aktiver Kutools funktioner...
Populære funktioner: Find, fremhæv eller identificer dubletter   |  Slet tomme rækker   |  Kombiner kolonner eller celler uden at miste data   |   Runde uden formel ...
Super opslag: VLookup med flere kriterier    Multiple Value VLookup  |   VOpslag på tværs af flere ark   |   Fuzzy Lookup ....
Avanceret rulleliste: Opret hurtigt rulleliste   |  Afhængig rulleliste   |  Multivælg rulleliste ....
Column Manager: Tilføj et bestemt antal kolonner  |  Flyt kolonner  |  Skift synlighedsstatus for skjulte kolonner  |  Sammenlign områder og kolonner ...
Fremhævede funktioner: Grid fokus   |  Designvisning   |   Stor Formel Bar    Arbejdsbog & Ark Manager   |  Ressourcebibliotek (Autotekst)   |  Datovælger   |  Kombiner regneark   |  Krypter/Dekrypter celler    Send e-mails efter liste   |  Superfilter   |   Specielt filter (filter fed/kursiv/gennemstreget...) ...
Top 15 værktøjssæt12 tekst Værktøjer (tilføje tekst, Fjern tegn, ...)   |   50 + Chart Typer (Gantt kort, ...)   |   40+ Praktisk formler (Beregn alder baseret på fødselsdag, ...)   |   19 Indsættelse Værktøjer (Indsæt QR-kode, Indsæt billede fra sti, ...)   |   12 Konvertering Værktøjer (Tal til ord, Valutaomregning, ...)   |   7 Flet og del Værktøjer (Avancerede kombinere rækker, Opdel celler, ...)   |   ... og mere

Overlad dine Excel-færdigheder med Kutools til Excel, og oplev effektivitet som aldrig før. Kutools til Excel tilbyder over 300 avancerede funktioner for at øge produktiviteten og spare tid.  Klik her for at få den funktion, du har mest brug for...

Beskrivelse


Fanen Office bringer en grænseflade til et kontor med Office, og gør dit arbejde meget lettere

  • Aktiver redigering og læsning af faner i Word, Excel, PowerPoint, Publisher, Access, Visio og Project.
  • Åbn og opret flere dokumenter i nye faner i det samme vindue snarere end i nye vinduer.
  • Øger din produktivitet med 50 % og reducerer hundredvis af museklik for dig hver 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