5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBAでシリアル値(日付、時間)を扱う時の注意点

Last updated at Posted at 2017-11-02

VBAでシリアル値を扱う場合、格納する変数の型によって出力が異なる。

 ・String型:日時がそのまま文字列として格納される
 ・Double型:シリアル値が小数以下まで格納される(日付+時間の値)
 ・Long型 :シリアル値が整数として格納される(日付値)

Integer型だとオーバーフローする。

※シリアル値:1日を1とした値。小数以下は時間を表す。

qiita1.jpg

vba

Sub test()

  Dim timeStr As String
  Dim timeDbl As Double
  Dim timeLng As Long

  timeStr = Range("B2").Value
  timeDbl = Range("B2").Value
  timeLng = Range("B2").Value

  Debug.Print "timeStr", timeStr
  Debug.Print "timeDbl", timeDbl
  Debug.Print "timeLng", timeLng

End Sub

出力結果

timeStr       2017/01/01 12:20:15
timeDbl        42736.5140625 
timeLng        42737 
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?