VBA日付関数| Excel VBA日付関数の使い方は?

Excel VBADATE関数

VBA Dateは日付と時刻の関数であり、使用しているシステム日付に従って現在の日付のみを返します。また、この関数には引数がまったくないことに注意してください。覚えておくべきもう1つの重要な要素は、この関数です。現在のシステム日付を返します。

エクセルでは、いくつかの機能なしでは生きていけません。「VBA日付」はその機能の1つです。Excelワークシートを頻繁に使用する場合は、システム日付に従って現在の日付を返す「TODAY()」という関数に注意する必要があります。

日付は非常に単純な関数であり、使用しているシステム日付に従って現在の日付のみを返します。これは、ワークシート関数「TODAY」と非常によく似ていますが、本質的に揮発性ではありません。

Excel DATE関数の構文は、指定する引数がなく、空の括弧のみが含まれているため、非常に単純です。

日付()

括弧は、関数を使用するときに関数を説明するためだけにあり、括弧を入力する必要はありません。

Excel VBA日付関数の使い方は?

このVBA日付Excelテンプレートはここからダウンロードできます–VBA日付Excelテンプレート

例1

セルA1に現在の日付を挿入するとし、以下の手順に従って、セルA1に現在の日付を挿入するコードを記述します。

手順1:マクロ名を作成します。

コード:

 Sub Date_Example1()

ステップ2:現在の日付をセルA1に格納する必要があるため、コードはRange(“ A1”)。Valueになります。

コード:

 Sub Date_Example1()Range( "A1")。Value End Sub 

ステップ3:セルA1に現在の日付が必要なので、DATE関数を使用します。

コード:

 Sub Date_Example1()Range( "A1")。Value = Date End Sub 

ステップ4:わかりました。これで完了です。F5キーを押してこのコードを実行するか、以下のスクリーンショットに示すように手動でコードを実行することもできます。セルA1に現在の日付を取得します。

したがって、このコードを記述しているとき、システムの現在の日付は「2019年3月15日」です。

注: 日付の形式は、Windowsの設定によって異なります。とにかく、フォーマットセルの下で日付のフォーマットを変更することができます。

例2

あなたがLICエージェントであり、対処する顧客が複数いると仮定します。重要な目的の1つは、誰が今日支払期日を迎えているかを知ることです。そうすれば、彼らに電話してすぐに支払いを受け取ることができます。

以下がデータベースにある顧客のリストであると仮定します。

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))Then 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.