VBA DateValue | Excel VBAでDateValue関数を使用する方法は?

VBA DateValue関数とは何ですか?

DateValue関数は、ExcelVBAの日付/時刻関数のカテゴリに組み込まれている関数です。これは、vbaではVBA関数とワークシート関数の両方として機能します。この関数は、Date文字列によって提供される時刻の情報を無視して、文字列表現形式で提供されるDateのシリアル番号または値を返します。Excelでは2つの異なる方法で使用されます。この関数は、ワークシートセルに入力するワークシート数式として使用されます。これは、MicrosoftExcelに関連付けられたVisualBasicEditorを介して入力するVBAアプリケーションのマクロコードとして使用されます。

この記事では、VBA DATEVALUEの例と、その使用方法を明確に説明して学習します。

VBA日付値関数の説明

VBAでは、DATEVALUEは次の構文を使用します。

この関数は、単一の引数またはパラメーターのみを使用します

  • 日付:文字列形式で表される日付です
  • 戻り値:この関数は、VBA関数として使用された日付の値を返します。ワークシート関数として使用される場合、日付値を返します

VBA DateValue関数は、有効なExcel形式で記述されているテキスト形式で表されたデータを解釈できます。文字列に平日のテキスト表現が含まれている場合、日付値を返すことはできません。

VBA DateValue関数の利点は、文字列から日付値を抽出し、時刻付きの日付を唯一の日付に変換することです。日付が時間とともに指定されている場合、この関数は時間値を避けて日付値のみを機能させることが簡単にできます。

Excel VBA DATEVALUEの使用方法は?

ExcelでDateValue関数を使用するには、最初にVBAエディターを開く必要があります。

コマンドボタンをExcelワークシートに配置して、VBAプログラムの行を追加する必要があります。プログラムの行を実装するには、ユーザーはExcelシートのコマンドボタンをクリックする必要があります。プログラムからの有効な出力を得るために、有効な入力は引数を介して与えられます。たとえば、次のコードは、DateValue関数を実行してVBAのテキストから日付値を抽出するマクロの作成に役立ちます。

VBAプログラム: 

 Sub Datebutton()Dim myDate As Date myDate = DateValue(“ August 15、1991”)MsgBox Date(myDate)End Sub 

このコードにより、指定された入力から日付が15になります。

Excel VBADATEVALUEの例

以下は、ExcelVBAのDateValueの例です。

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

例1-日付から日、月、年を取得する

VBAでプログラムを作成して実行するには、いくつかの手順に従います。それらが含まれます

ステップ1:[開発者]タブに移動し、Excelシートのセルにカーソルを置き、[挿入]オプションをクリックして、図に示すようにActiveXコントロールの下の[コマンドボタン]を選択します。

ボタンを目的の場所にドラッグし、プロパティウィンドウから[日付]ボタンとしてキャプションを付けます。

ステップ2: VBAプロジェクトにリダイレクトするボタンをダブルクリックし、PrivateSubコマンドボタンとendsubの間にコードを記述します。

日付、月、年を取得するには、コードを次のように開発する必要があります。

コード:

 Private Sub Datebutton1_Click()Dim Exampledate As Date Exampledate = DateValue( "April 19,2019")MsgBox Date MsgBox Year(Exampledate)MsgBox Month(Exampledate)End Sub 

このコードでは、Datebutton1_Click()は名前であり、exampledateはDateデータ型と出力を表示するMsgboxで変数です。

ステップ3:コードの開発中に、vbaタイプの不一致エラーが発生し、それらを処理する必要があります。

ステップ4:このステップでは、実行オプションをクリックしてプログラムを実行します。

または、[デバッグ]メニューの[ステップイン]オプションを選択して、プログラムを段階的に確認またはデバッグすることもできます。コードにエラーがない場合は、出力が表示されます。

ステップ5:プログラムが実行されると、最初にテキスト入力で指定された日付を含むメッセージボックスが表示されます。次に、[OK]をクリックして年の値を確認し、メッセージボックスの[OK]をクリックして月の値を確認します。

注:正確な結果を得るには、これらの手順を明確に実行する必要があります。

Example #2 – Using the DatePart to Get the Different Parts of Date

Step 1: Go to Excel Developer Tab, place cursor on a cell in the Excel sheet and click on ‘Insert’ option and choose ‘Command Button’ under ActiveX Control as shown in the figure.

Step 2: Drag the button and give the caption as DatePart under properties.

Double click on this button, it directs to the Visual Basic Editor sheet and displays as follows.

Step 3: Develop the code using the DatePart with DateValue as shown in the figure.

Code:

 Private Sub Datepart1_Click() Dim partdate As Variant partdate = DateValue("8/15/1991") MsgBox partdate MsgBox Datepart("yyyy", partdate) MsgBox Datepart("dd", partdate) MsgBox Datepart("mm", partdate) MsgBox Datepart("q", partdate) End Sub 

In this program, DatePart1 is the macro name and partDate is argument name with data type ‘variant’. For displaying year, date, month, and a quarter, the format is applied as “yyyy”, “d”, “m”, and “q”. If we do any mistake in the format it displays the following error.

Step 4: After successful debugging of program, run the program by clicking on the run button use excel shortcut key F5.

The code first displays the full date then after clicking every OK from the msgbox, it shows the year value after that Date value, Month Value, Quater Value respectively.

Things to Remember About the VBA DATEVALUE

The following things must be remembered while using the DateValue function in Excel VBA

  • Run time error 13 with message Type Mismatch is displayed when the date provided to the DateValue function is not able to convert into a valid date. We need date is proper text format
  • When we try to get the only date from the string argument with code ‘msgbox date (argument name)’, it displays the type mismatch error.
  • We can see the output of the DateValue function without opening the VBA editor. It is done by clicking the command button and select the macro created for the respective program
  • When DatePart is used to get the values, the appropriate format should be followed. Otherwise, it leads ‘run time error 5’ with message invalid procedure call or argument.