ไปยังเนื้อหาหลัก

จะทำซ้ำหรือวนซ้ำมาโครทุกๆ X นาทีใน Excel ได้อย่างไร

ในขณะที่ทำงานกับ Microsoft Excel คุณอาจต้องสร้าง Macros เพื่อให้สามารถดำเนินการบางอย่างได้ ตัวอย่างเช่นคุณต้องการสร้างมาโครเพื่อคัดลอกช่วงข้อมูลไปยังที่ใหม่โดยอัตโนมัติ เนื่องจากข้อมูลจะมีการเปลี่ยนแปลงบ่อยครั้งคุณจึงต้องให้มาโครนี้ทำงานโดยอัตโนมัติทุกๆ 5 นาทีโดยไม่ต้องเรียกใช้ด้วยตนเองเพื่อซิงโครไนซ์ข้อมูลทั้งสองช่วงนี้ จะบรรลุได้อย่างไร? วิธีการในบทความนี้สามารถช่วยคุณได้

ทำซ้ำหรือวนซ้ำมาโครทุกๆ X นาทีใน Excel


ทำซ้ำหรือวนซ้ำมาโครทุกๆ X นาทีใน Excel

รหัส VBA ต่อไปนี้สามารถช่วยให้คุณทำมาโครซ้ำทุกๆ X นาทีใน Excel กรุณาดำเนินการดังนี้

1 กด อื่น ๆ + F11 ในเวลาเดียวกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน โปรดคลิก สิ่งที่ใส่เข้าไป > โมดูล. จากนั้นคัดลอกและวางโค้ด VBA ด้านล่างลงในไฟล์ รหัส หน้าต่าง. ดูภาพหน้าจอ:

รหัส VBA: ทำซ้ำหรือวนซ้ำมาโครทุกๆ X นาทีใน 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

หมายเหตุ: ในรหัสโปรดแทนที่บรรทัดนี้ 'ใส่รหัสของคุณที่นี่ ด้วยรหัสคุณจะเรียกใช้ทุก X นาที

3 กด F5 กุญแจสำคัญในการเรียกใช้รหัส ในการโผล่ขึ้นมา Kutools สำหรับ Excel โปรดป้อนช่วงเวลาที่คุณจะทำมาโครซ้ำตามจากนั้นคลิกที่ OK ปุ่ม. ดูภาพหน้าจอ:

จากนี้ไปมาโครบางตัวจะทำงานซ้ำ ๆ ทุกๆ 5 นาทีในสมุดงานของคุณ

หมายเหตุ: หากคุณต้องการหยุดการทำงานของมาโครและเปลี่ยนช่วงเวลาของรอบโปรดคัดลอกโค้ด VBA ด้านล่างลงในเดียวกัน โมดูล หน้าต่างแล้วกดปุ่ม F5 กุญแจสำคัญในการเรียกใช้รหัส จากนั้นมาโครจะหยุดทำงานโปรดรันโค้ดด้านบนอีกครั้งเพื่อระบุช่วงเวลาใหม่

รหัส VBA: หยุดการทำงานของแมโคร

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

แท็บ Office - การเรียกดูแบบแท็บการแก้ไขและการจัดการสมุดงานใน Excel:

Office Tab นำอินเทอร์เฟซแบบแท็บดังที่เห็นในเว็บเบราว์เซอร์เช่น Google Chrome, Internet Explorer เวอร์ชันใหม่และ Firefox ไปยัง Microsoft Excel มันจะเป็นเครื่องมือที่ช่วยประหยัดเวลาและไม่สามารถถูกแทนที่ได้ในงานของคุณ ดูการสาธิตด้านล่าง:

คลิกเพื่อทดลองใช้ Office Tab ฟรี!

แท็บ Office สำหรับ Excel


บทความที่เกี่ยวข้อง:

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
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