ฟังก์ชันวันที่ VBA | วิธีใช้ฟังก์ชันวันที่ของ Excel VBA

ฟังก์ชัน Excel VBA DATE

VBA Dateเป็นฟังก์ชัน Date and Time ซึ่งจะส่งกลับเฉพาะวันที่ปัจจุบันตามวันที่ของระบบที่คุณใช้สิ่งสำคัญที่ควรทราบก็คือฟังก์ชันนี้ไม่มีข้อโต้แย้งใด ๆ ปัจจัยสำคัญอีกประการหนึ่งที่ต้องจำไว้คือฟังก์ชันนี้ ส่งคืนวันที่ของระบบปัจจุบัน

ใน excel เราขาดไม่ได้หากไม่มีฟังก์ชันบางอย่างและ“ VBA Date” ก็เป็นหนึ่งในฟังก์ชันเหล่านั้น หากคุณเป็นผู้ใช้แผ่นงาน excel เป็นประจำคุณต้องทราบถึงฟังก์ชันที่เรียกว่า“ TODAY ()”ซึ่งจะส่งคืนวันที่ปัจจุบันตามวันที่ของระบบ

วันที่เป็นฟังก์ชันที่เรียบง่ายมากและจะส่งกลับเฉพาะวันที่ปัจจุบันตามวันที่ของระบบที่คุณใช้ การทำงานนี้คล้ายกับฟังก์ชันแผ่นงานของเรา“ TODAY” แต่ไม่มีการระเหยตามธรรมชาติ

ไวยากรณ์ของฟังก์ชัน excel DATE นั้นง่ายมากเพราะไม่มีอาร์กิวเมนต์ให้ใส่และมีเฉพาะวงเล็บว่างเท่านั้น

วันที่ ()

วงเล็บมีไว้เพื่ออธิบายฟังก์ชันเมื่อคุณใช้ฟังก์ชันโดยไม่จำเป็นต้องใส่วงเล็บ

วิธีใช้ฟังก์ชันวันที่ของ Excel VBA

คุณสามารถดาวน์โหลดเทมเพลต VBA Date Excel ได้ที่นี่ - เทมเพลต Excel วันที่ VBA

ตัวอย่าง # 1

สมมติว่าคุณต้องการแทรกวันที่ปัจจุบันในเซลล์ A1 จากนั้นทำตามขั้นตอนด้านล่างเพื่อเขียนโค้ดเพื่อแทรกวันที่ปัจจุบันในเซลล์ A1

ขั้นตอนที่ 1:สร้างชื่อมาโคร

รหัส:

 Sub Date_Example1 ()

ขั้นตอนที่ 2:เนื่องจากเราต้องเก็บวันที่ปัจจุบันในเซลล์ A1 รหัสของเราจะเป็น. ช่วง (“A1”)

รหัส:

 Sub Date_Example1 () ช่วง ("A1") ค่า End Sub 

ขั้นตอนที่ 3:ในเซลล์ A1 เราต้องการวันที่ปัจจุบันดังนั้นให้ใช้ฟังก์ชัน DATE

รหัส:

 Sub Date_Example1 () ช่วง ("A1") ค่า = Date End Sub 

ขั้นตอนที่ 4:โอเคเสร็จแล้ว มาเรียกใช้โค้ดนี้ทันทีโดยกดปุ่ม F5 หรือคุณสามารถเรียกใช้รหัสด้วยตนเองดังที่แสดงในภาพด้านล่าง เราจะได้วันที่ปัจจุบันในเซลล์ A1

ดังนั้นเมื่อฉันเขียนโค้ดนี้วันที่ปัจจุบันในระบบของฉันคือ“ 15 มีนาคม 2019”

หมายเหตุ: รูปแบบ  วันที่ของคุณขึ้นอยู่กับการตั้งค่า windows ของคุณ อย่างไรก็ตามคุณสามารถเปลี่ยนรูปแบบของวันที่ภายใต้เซลล์รูปแบบ

ตัวอย่าง # 2

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

สมมติว่าด้านล่างนี้คือรายชื่อลูกค้าที่คุณมีอยู่ในฐานข้อมูลของคุณ

ฉันได้เขียนโค้ดที่จะแจ้งให้คุณทราบทันทีที่คุณเปิดไฟล์ excel

รหัส:

 Sub Due_Notifier () Dim Duedate As Date Dim i As Long Duedate = Date i = 2 For i = 2 To Cells (Rows.Count, 1). End (xlUp) .Row If Duedate = DateSerial (Year (Date), Month ( Cells (i, 3) .Value), Day (Cells (i, 3) .Value)) จากนั้น MsgBox "Customer Name:" & Cells (i, 1) .Value & vbNewLine & "Premium Amount:" & Cells (i , 2). Value End If Next i End Sub 

คัดลอกโค้ดด้านบนและวางในโมดูล VBA

ตอนนี้ดับเบิลคลิกที่ตัวเลือก "สมุดงานนี้"

ตอนนี้เลือก"สมุดงาน"จากเมนูแบบเลื่อนลงด้านบน

ทันทีที่คุณเลือกตัวเลือก“ สมุดงาน” คุณจะเห็นมาโครส่วนตัวเปิดขึ้นโดยอัตโนมัติ

ที่นี่ชื่อมาโครว่า“ Workbook_Open ()”ซึ่งหมายความว่าเมื่อใดก็ตามที่สมุดงานนี้เปิดสิ่งที่คุณต้องทำ เมื่อใดก็ตามที่สมุดงานนี้เปิดขึ้นเราจำเป็นต้องเรียกใช้แมโครที่เราสร้างขึ้น

ดังนั้นที่นี่เราต้องเรียกมาโครของเราที่เราสร้างขึ้นตามชื่อ ในโค้ดด้านบนชื่อมาโครของเราคือ“ Due_Notifier”

รหัส:

 โทร Due_Notifier

บันทึกสมุดงานนี้แล้วปิด

หลังจากปิดแล้วให้เปิดสมุดงานและดูความมหัศจรรย์

ตอนนี้ฉันจะเปิด….

ว้าว!!! มันแสดงชื่อลูกค้าและจำนวนเงินที่ครบกำหนดสำหรับวันที่ปัจจุบัน

Customer Name is “Amar” and the due amount is “20883”. The reason why it is showing this customer name because the due date for Mr Amar is 15th March 2019 i.e. Today.

Now click on Ok, it will show other customer names if the due date is on today.

It is showing Mr Arvind name, his due date is also on 15th March 2019.

Now, you can easily identify the customer names as soon as you come to the office. One of the big headaches is gone.

Similarly, I have created one more excel macro which will send auto birthday emails from your outlook.

Example #3

Assume you are in an “Employee Engagement Team” and you are responsible to send birthday emails to your employees. Identify and sending the email to each and every one separately is a painful job.

Hello, my dear friend doesn’t worry I have created a macro for you to send the auto birthday emails to your employees.

I have created some data to test and below is the image of the same.

You just need to update the employee master according to the headings of the table. Below is the code to send the emails.

Copy the below code and paste in the module.

 Sub Birthday_Wishes() Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Dim Mydate As Date Dim i As Long Set OutlookApp = New Outlook.Application Mydate = Date i = 2 For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row Set OutlookMail = OutlookApp.CreateItem(olMailItem) If Mydate = DateSerial(Year(Date), Month(Cells(i, 5).Value), Day(Cells(i, 5).Value)) Then OutlookMail.To = Cells(i, 7).Value OutlookMail.CC = Cells(i, 8).Value OutlookMail.BCC = "" OutlookMail.Subject = "Happy Birthday - " & Cells(i, 2).Value OutlookMail.Body = "Dear " & Cells(i, 2).Value & "," & vbNewLine & vbNewLine & _ "We wish you a happy birhday on behalf of the management and we wish all the success in the coming future" & vbNewLine & _ vbNewLine & "Regards," & vbNewLine & "StrIDE Team" OutlookMail.Display OutlookMail.Send End If Next i End Sub 

As soon as you come to the office just open the file and run this code, it will automatically send birthday wishes to the respective email id’s.

Note: You should have Outlook configured in your system.