Note: The other languages of the website are Google-translated. Back to English
Log på  \/ 
x
or
x
Tilmeld  \/ 
x

or

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:


De bedste Office-produktivitetsværktøjer

Kutools til Excel løser de fleste af dine problemer og øger din produktivitet med 80%

  • Genbruge: Indsæt hurtigt komplekse formler, diagrammer og alt, hvad du har brugt før; Krypter celler med adgangskode Opret postliste og send e-mails ...
  • Super Formula Bar (let redigere flere linjer med tekst og formel); Læsning Layout (let at læse og redigere et stort antal celler); Indsæt til filtreret rækkevidde...
  • Flet celler / rækker / kolonner uden at miste data; Split celler indhold; Kombiner duplikerede rækker / kolonner... Forhindre duplikerede celler; Sammenlign områder...
  • Vælg Duplicate eller Unique Rækker; Vælg tomme rækker (alle celler er tomme); Super Find og Fuzzy Find i mange arbejdsbøger; Tilfældig valg ...
  • Præcis kopi Flere celler uden at ændre formelreference; Auto Opret referencer til flere ark; Indsæt kugler, Afkrydsningsfelter og mere ...
  • Uddrag tekst, Tilføj tekst, Fjern efter position, Fjern mellemrum; Opret og udskriv personsøgningssubtotaler; Konverter mellem celler indhold og kommentarer...
  • Superfilter (gem og anvend filterskemaer på andre ark); Avanceret sortering efter måned / uge / dag, hyppighed og mere; Specielt filter af fed, kursiv ...
  • Kombiner arbejdsbøger og arbejdsark; Fletabeller baseret på nøglekolonner; Opdel data i flere ark; Batch Konverter xls, xlsx og PDF...
  • Mere end 300 kraftfulde funktioner. Understøtter Office / Excel 2007-2019 og 365. Understøtter alle sprog. Nem implementering i din virksomhed eller organisation. Fuld funktioner 30-dages gratis prøveperiode. 60-dages pengene tilbage garanti.
kte-fane 201905

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!
officetab bund
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 · 1 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 · 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.