Funcția dată VBA | Cum se utilizează funcția Excel VBA Date?

Funcția Excel VBA DATE

Data VBA este o funcție de dată și oră, returnează doar data curentă conform datei de sistem pe care o utilizați, de asemenea, este important să rețineți că această funcție nu are niciun argument, un alt factor important de reținut este că această funcție returnează data curentă a sistemului.

În Excel nu putem trăi fără unele dintre funcții și „Data VBA” este una dintre aceste funcții. Dacă sunteți un utilizator frecvent al foii de lucru Excel, atunci trebuie să fiți la curent cu o funcție numită „TODAY ()” care va returna data curentă conform datei sistemului.

Data este o funcție foarte simplă și returnează doar data curentă conform datei de sistem pe care o utilizați. Acest lucru funcționează foarte asemănător cu funcția noastră de foaie de lucru „AZI”, dar nu are o natură volatilă.

Sintaxa funcției Excel DATE este foarte simplă, deoarece nu are niciun argument de furnizat și include doar paranteze goale.

Data ()

Paranteze există doar pentru a explica funcția atunci când utilizați funcția nu este nevoie să introduceți paranteză.

Cum se utilizează funcția Excel VBA Date?

Puteți descărca acest șablon Excel VBA Date aici - Șablon Excel VBA Date

Exemplul nr. 1

Să presupunem că doriți să inserați data curentă în celula A1, apoi urmați pașii de mai jos pentru a scrie codul pentru a insera data curentă în celula A1.

Pasul 1: Creați un nume de macro.

Cod:

 Subdată_Exemplu1 ()

Pasul 2: Deoarece trebuie să stocăm data curentă în celula A1, codul nostru va fi Range („A1”). Valoare .

Cod:

 Sub Date_Example1 () Range ("A1"). Valoare End Sub 

Pasul 3: În celula A1 avem nevoie de data curentă, deci utilizați funcția DATE.

Cod:

 Sub Date_Example1 () Range ("A1"). Valoare = Data End Sub 

Pasul 4: Ok, am terminat. Să rulăm acest cod acum apăsând tasta F5 sau puteți rula codul manual așa cum se arată în captura de ecran de mai jos. Vom obține data curentă în celula A1.

Deci, când scriu acest cod, data curentă în sistemul meu este „15 martie 2019”.

Notă: Formatul  datei dvs. depinde de setările Windows. Oricum, puteți modifica formatul datei în formatul de celule.

Exemplul nr. 2

Să presupunem că sunteți agent LIC și că aveți mai mulți clienți cu care să vă ocupați. Unul dintre obiectele cheie este să știți a cărui plată este datorată astăzi, astfel încât să le puteți apela și să încasați plata imediat.

Să presupunem că mai jos este lista clienților pe care îi aveți în baza de date.

Am scris deja un cod care vă va notifica imediat ce deschideți fișierul Excel.

Cod:

 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)) Apoi MsgBox "Nume client:" & Cells (i, 1) .Value & vbNewLine & "Suma Premium:" & Cells (i , 2) .Value End If Next i End Sub 

Copiați codul de mai sus și lipiți-l în modulul VBA.

Acum faceți dublu clic pe opțiunea „Acest registru de lucru”.

Now select “Workbook” from the above dropdown.

As soon as you select the option “Workbook” you can see a private macro automatically opens.

Here macro name says “Workbook_Open ()” this means whenever this workbook opens what you have to do. Whenever this workbook opens we need to run the macro we have created.

So, here we need to call our macro we have created by its name. In the above code, our macro name is “Due_Notifier”.

Code:

 Call Due_Notifier

Now save this workbook and close it.

After closing it, open the workbook and see the magic.

Now I will open….

Wow!!! It shows me the customer name and their due amount for the current date.

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.