ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link สอน Python / Programming
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

วิธีทำให้ VBA Run คำสั่ง ณ เวลาที่ต้องการ

Categories 📂

Excel VBA

Tags 🏷️

task scheduler, vba

จากที่ผมสอนวิธีการส่ง line/email ด้วย excel ไป ทำให้มีหลายคนถามมาหลังไมค์ว่า ทำยังไงถึงจะให้ VBA มันแจ้งเตือน ณ เวลาใดเวลาหนึ่งที่ต้องการได้? เช่น บางคนอาจเอาไปส่งเตือนลูกค้า บางคนเอาไว้ส่งเรื่องหุ้น อะไรแบบนี้

การที่จะทำแบบนั้นได้ แปลว่ามันต้องมี Event/Trigger บางอย่างเกิดขึ้น ดังนั้นใบบทความนี้จะสอนว่าทำยังไงถึงจะรัน VBA ในเวลาที่กำหนดได้ครับ

สร้าง Action ขึ้นมาก่อน

สมมติว่าผมสร้าง sub เพื่อแสดง msgbox ธรรมดาๆ ไว้ใน Module 1 ดังนี้ (ให้แสดง msgbox นี่แหละ ง่ายสุดในสามโลกแล้ว)

Sub TestMacro1()

MsgBox ("Test Run Macro Number 1")

End Sub

Sub TestMacro2()

MsgBox ("Test Run Macro Number 2")

End Sub

ทั้งนี้ในชีวิตจริงคุณต้องเปลี่ยนจาก MsgBox ไป call sub ที่เตรียมไว้ เช่น Call LineNotify อะไรแบบนี้นะ

Application.Ontime คำสั่งที่ใช้กำหนดเรื่องรันตาม schedule เวลา

การกำหนด schedule เวลา จะใช้ Code แบบง่ายสุดในรูปแบบนี้

Application.OnTime เวลา, "ชื่อsub" 

ระวัง! อย่างไรก็ตาม เวลาที่ใส่ลงไปเป็นเวลาอย่างเร็วสุดที่จะรันคำสั่ง (จะไม่รันก่อนหน้านั้นแน่นอน) แต่มันอาจไม่รันตอนเวลานั้นเป๊ะ (ถ้า Excel ยังยุ่งกับเรื่องอื่นอยู่)

ดังนั้นผมสามารถสร้าง Sub ไว้ Test เรื่องเวลาดังนี้

เวลาอาจจะใช้เป็นตัวแปร เช่น MyTime แล้ว MyTime สามารถกำหนดได้หลายแบบเช่น

ถ้าใส่เวลาลงไปเลย เช่น

Sub TestTime1()
MyTime = TimeValue("19:43:00")   'รันตอน 19:43
Application.OnTime MyTime, "TestMacro2"
End Sub

ถ้าใส่แบบนับจากปัจจุบันด้วยวิธี
Now + TimeSerial(ชั่วโมง, นาที, วินาที) หรือ
Now + TimeValue(” ชั่วโมง : นาที : วินาที “) ก็ได้

Sub TestTime2()
MyTime = Now + TimeSerial(0, 0, 5)   'อีก 5 วินาทีจะรัน
MyTime2 = Now + TimeValue("00:00:05")  'อีก 5 วินาทีจะรัน แบบนี้ก็ได้
Application.OnTime MyTime, "TestMacro1"
End Sub

พอกด F5 ที่ Sub TestTime ก็จะพบว่าอีก 5 วินาทีถึงจะมี MsgBox เก้งขึ้นมาว่า Test Run Macro Number 1

มีคำสั่งอื่นแทรกก็ได้

แต่ถ้าใส่อีกคำสั่งต่อท้ายไปเลยแบบไม่หน่วงเวลา คำสั่ง Test แทรกที่ต่อท้ายมันจะถูกรันขึ้นมาก่อน (เพราะคำสั่งแรกมีการ schedule เอาไว้ว่าอีก 5 วินาทีจากตอนแรกถึงจะสั่ง TestMacro1)

Sub TestTime3()
MyTime = Now + TimeSerial(0, 0, 5)   'อีก 5 วินาทีจะรัน
Application.OnTime MyTime, "TestMacro1" 'อันนี้สั่งตาม schedule ล่วงหน้าไว้
MsgBox ("Test แทรก") 'นี่คือคำสั่งแทรก
End Sub

ถ้าจะยกเลิกการ Schedule สามารถทำได้ดังนี้

Application.OnTime เวลาเดิมที่สั่ง, "ชื่อsubเดิมที่สั่ง", , False
'False คือสั่ง cancel

เช่น

Sub TestTime4()
Mytime = Now + TimeSerial(0, 0, 5)   'อีก 5 วินาทีจะรัน
Application.OnTime Mytime, "TestMacro1"
MsgBox ("Test แทรก")
Application.OnTime Mytime, "TestMacro1", , False    'นี่จะ cancel การ schedule
End Sub

การสั่งรันหลายๆ ครั้งต่อเนื่อง ทุกๆ xx นาที

ถ้าเราสั่งให้ Sub 2 ตัวมันเรียกกันเอง มันก็จะรันต่อเนื่องไปเรื่อยๆ ได้ เช่น

Sub TestMacroRecur()

MsgBox ("Test Run Macro Number 2")

StartTimeRecur   'เรียกตัว schedule อีกรอบ

End Sub

Sub StartTimeRecur()
StartTime = Now + TimeSerial(0, 0, 5)   'อีก 5 วินาทีจะรัน
Application.OnTime StartTime, "TestMacroRecur", , True
End Sub

แต่ที่นี้มันจะรันไปเรื่อยๆ ไม่หยุดเลย เราก็ต้องสร้างปุ่มมาสั่งหยุดการเตือนอีกที เช่น สร้าง sub นี้แล้ว Assign Macro ให้กับปุ่มเอาไว้

Sub CancelTimeRecur()
' เอาอันนี้ไป assign กับปุ่มก็ได้
Application.OnTime StartTime, "TestMacroRecur", , False
End Sub

และนี่ก็คือ Code ให้รันคำสั่งที่ต้องการขณะที่เปิดไฟล์ Excel ไว้อยู่

แต่จะทำไงดี ถ้าไม่อยากเปิด Excel ทิ้งไว้ แต่อยากให้มันเปิดขึ้นมาเองเมื่อจะรัน??

วิธีแก้ไขก็คือ ให้ Windows เป็นคนเปิด Excel ขึ้นมา แล้วรัน Macro ที่เราต้องการ ซึ่งวิธีทำก็คือใช้เครื่องมือที่ชื่อว่า Windows Task Scheduler นั่นเอง (ดังนั้นเงื่อนไขเดียวคือ Computer ต้องเปิดอยู่ ตอนถึงเวลารัน)

วิธีการใช้ Task Scheduler

ซึ่งถ้าเราใช้คำสั่งนี้แล้ว เราสามารถรัน VBA ได้ในเวลาที่ต้องการ และสั่งให้มันปิดโปรแกรมไป แล้วก็รันใหม่ในอีกเวลาก็ได้ โดยไม่ต้องยุ่งกับคำสั่ง Application.OnTime ที่เราเรียนกันข้างบนด้วยซ้ำ 555

ผมไปเจอบทความภาษาอังกฤษที่เขียนไว้ครบถ้วนมาก คือ บทความนี้ ดังนั้นผมจะพยายามสรุปจากบทความเลยนะครับ

Step1 : เอา code นี้ไปใส่ Notepad

'Input Excel File's Full Path
'เปลี่ยน path เป็นไฟล์ของตัวเองที่จะรัน
  ExcelFilePath = "D:\ThepExcel\web\VBA on time\VBAonTime.xlsm"

'Input Module/Macro name within the Excel File
'ใส่ชื่อ macro และ module ให้ถูกต้อง
  MacroPath = "Module1.TestMacro1"

'Create an instance of Excel
  Set ExcelApp = CreateObject("Excel.Application")

'Do you want this Excel instance to be visible?
  ExcelApp.Visible = True  'or "False"

'Prevent any App Launch Alerts (ie Update External Links)
  ExcelApp.DisplayAlerts = False

'Open Excel File
  Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)

'Execute Macro Code
  ExcelApp.Run MacroPath

'Save Excel File (if applicable)
  wb.Save

'Reset Display Alerts Before Closing
  ExcelApp.DisplayAlerts = True

'Close Excel File
  wb.Close

'End instance of Excel
  ExcelApp.Quit

'Leaves an onscreen message!
  MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation

save as ไฟล์ เลือกแบบ all files แล้วตั้งชื่อให้เป็น MacroTask.vbs

Step 2 : ตั้งค่าในโปรแกรม Task Scheduler

หาโปรแกรม Task Scheduler แล้วเปิดขึ้นมา

  • ไปที่ Action -> Create Task
  • ใน Tab General : ตั้งชื่อ Taskอะไรก็ได้
  • ใน Tab Triggers : กดปุ่ม New แล้วตั้งเวลาได้ สามารถเลือกรันแบบ one time หรือจะ repeat ก็ได้ เช่น สามารถสั่งรัน ทุกๆ วัน ทุกอาทิตย์ ทุกเดือนได้
  • ใน Tab Action : กด New แล้วใส่ว่า
    • ใน Program/Script ให้ browse หรือใส่คำว่า C:\Windows\System32\cscript.exe
    • ใน Argument (ต้องมีเครื่องหมายคำพูด)
      • ใส่ path ของ MacroTask.vbs เช่น
        “D:\ThepExcel\MacroTask.vbs”
  • ใน Tab Condition : กำหนดได้ว่าให้รันก็ต่อเมื่ออะไร? ถ้าไม่อยากให้มีเงื่อนไขเยอะก็ติ๊กออก
  • ใน Setting : ให้เป็นเงื่อนไขเพิ่มเติมอื่นๆ ไม่ต้อง Set ก็ได้

พอ ok แล้วรอเวลา เจ้า Excel ก็จะถูกรันเองโดยอัตโนมัติเลย!!

ตัวอย่างการเอาไปใช้งาน

ผมอาจจะใช้สั่ง LoopTable ในไฟล์การแจ้งเตือน Line ที่ผมทำเอาไว้ก็ได้

มันก็จะเปิด Excel ขึ้นมาเอง ส่ง Line เอง ปิด Excel เอง จบเลย 555

แชร์ความรู้ให้เพื่อนๆ ของคุณ
849    
849    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • วิธีสั่ง Prompt และตั้งค่าใน Stable Diffusion ให้รูปสวยโดนใจ [Part3]
  • วิธีเรียกใช้งาน Model เจ๋งๆ ใน Stable Diffusion [ตอนที่2]
  • วิธีใช้งาน AI สร้างรูปสุดเจ๋งและฟรีด้วย Stable Diffusion ฉบับมือใหม่ [ตอนที่1]
  • 10 ไอเดีย เรียนรู้ Excel ผ่าน ChatGPT AI สุดเจ๋ง
  • การทำ Simulation ด้วย Excel
  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 2

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง
  • แยกข้อมูลที่อยู่สุดเน่า ด้วย Excel Power Query

Categories

Tags

ai collection concepts copy database Data Model data table data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro irr len link logic lookup match m code merge mid overview paste pivot power query row sort speed split substitute table text textjoin time tips trim vba vlookup

Archives

  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT