Skip i'r prif gynnwys

Sut i ailadrodd neu ddolennu Macro bob X munud yn Excel?

Wrth weithio gyda Microsoft Excel, efallai y bydd angen i chi greu Macros i gyflawni rhai gweithrediadau. Er enghraifft, rydych chi am greu Macro i gopïo ystod o ddata yn awtomatig i le newydd. Gan y bydd y data'n cael ei newid yn aml, mae angen i'r Macro hwn redeg yn awtomatig bob 5 munud heb ei sbarduno â llaw er mwyn cydamseru'r ddwy ystod hon o ddata. Sut i'w gyflawni? Gall dull yn yr erthygl hon eich helpu chi.

Ailadroddwch neu dolenwch Macro bob X munud yn Excel


Ailadroddwch neu dolenwch Macro bob X munud yn Excel

Gall y cod VBA canlynol eich helpu i ailadrodd Macro bob X munud yn Excel. Gwnewch fel a ganlyn.

1. Gwasgwch Alt + F11 allweddi ar yr un pryd i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Yn y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, cliciwch Mewnosod > Modiwlau. Yna copïwch a gludwch y cod VBA isod i'r Côd ffenestr. Gweler y screenshot:

Cod VBA: Ailadroddwch neu dolenwch Macro bob X munud yn 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

Nodyn: Yn y cod, amnewidiwch y llinell hon 'Mewnosodwch eich cod yma gyda'r cod byddwch chi'n rhedeg bob X munud.

3. Gwasgwch y F5 allwedd i redeg y cod. Yn y popping up Kutools ar gyfer Excel blwch deialog, nodwch yr amser egwyl y byddwch yn ailadrodd y macro yn seiliedig arno, ac yna cliciwch ar y OK botwm. Gweler y screenshot:

O hyn ymlaen, bydd y Macro penodol yn rhedeg dro ar ôl tro bob 5 munud yn eich llyfr gwaith.

Nodyn: Os oes angen i chi atal gweithredu'r macro a newid cyfwng y cylch, copïwch y cod VBA isod i'r un peth Modiwlau ffenestr a gwasgwch y F5 allwedd i redeg y cod. Yna bydd y Macro yn cael ei stopio, ail-redeg y cod uchod i nodi cyfwng newydd.

Cod VBA: Stopiwch weithredu'r macro

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

Tab Swyddfa - Pori Tabbed, Golygu a Rheoli Llyfrau Gwaith yn Excel:

Mae Office Tab yn dod â'r rhyngwyneb tabbed fel y gwelir mewn porwyr gwe fel Google Chrome, Internet Explorer fersiynau newydd a Firefox i Microsoft Excel. Bydd yn arf sy'n arbed amser ac yn unigryw yn eich gwaith. Gweler y demo isod:

Cliciwch i dreialu Office Tab am ddim!

Tab Office ar gyfer Excel


Erthyglau perthnasol:

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
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