構文
構文は以下の通りです。
DateValue(日付文字列)
解説
DateValue関数は、文字列日付を日付に変換します。
引数 | 指定 | 内容 |
日付文字列 | 必須 | 日付を表す文字列、セル参照、変数などを指定します。 |
使用例
DateValue("2013/6/9")
DateValue("2013年6月9日")
DateValue("平成25/6/9")
使い方
具体的な使い方を紹介します。
Sub ConvertDate()
Dim dateText As String
Dim convertedDate As Date
' 日付を表すテキストを指定
dateText = "2023/06/04"
' DateValue関数を使って日付を変換
convertedDate = DateValue(dateText)
' 変換した日付をメッセージボックスで表示
MsgBox "変換後の日付は " & convertedDate & " です。"
End Sub
Dim dateText As String
Dim convertedDate As Date
' 日付を表すテキストを指定
dateText = "2023/06/04"
' DateValue関数を使って日付を変換
convertedDate = DateValue(dateText)
' 変換した日付をメッセージボックスで表示
MsgBox "変換後の日付は " & convertedDate & " です。"
End Sub
上記のコードでは、「変換後の日付は 2023/06/04 です。」というメッセージボックスが表示されます。
プログラミング例
以下に、プログラミング例を紹介します。
Sub CalculateDays()
Dim startDateText As String
Dim endDateText As String
Dim startDate As Date
Dim endDate As Date
Dim days As Long
' 開始日と終了日をテキストで指定
startDateText = "2023/05/01"
endDateText = "2023/06/01"
' 開始日と終了日を日付に変換
startDate = DateValue(startDateText)
endDate = DateValue(endDateText)
' 日数を計算
days = endDate - startDate
' 結果を表示
MsgBox "開始日から終了日までの日数は " & days & " 日です。"
End Sub
Dim startDateText As String
Dim endDateText As String
Dim startDate As Date
Dim endDate As Date
Dim days As Long
' 開始日と終了日をテキストで指定
startDateText = "2023/05/01"
endDateText = "2023/06/01"
' 開始日と終了日を日付に変換
startDate = DateValue(startDateText)
endDate = DateValue(endDateText)
' 日数を計算
days = endDate - startDate
' 結果を表示
MsgBox "開始日から終了日までの日数は " & days & " 日です。"
End Sub
上記のコードでは、指定した開始日と終了日の間の日数を計算して表示する例です。
まとめ
DateValue関数は、テキスト形式の日付を日付値に変換するために使用されます。是非活用してください。