時間の範囲判定をする→開始(FROM)~終了(TO)の日付と時間、比較対象の日付と時間をセットして範囲に入っているかどうかを判定する
<分類:判定>
<使用例:対象範囲(開始)の日付と時間、対象日付と時間、対象範囲(終了)の日付と時間(13:00:00)をセットして、範囲内の場合は内、範囲外の場合は外と判定する>
<具体例:対象範囲(開始)の日付(2019/8/16)時間(02:00:00)≦対象の日付(2019/8/16)時間(01:59:59)≦対象範囲(終了)の日付(2019/8/16)時間(13:00:00)は成立しないので範囲外と判定>
<具体例:対象範囲(開始)の日付(2019/8/16)時間(02:00:00)≦対象の日付(2019/8/16)時間(02:00:00)≦対象範囲(終了)の日付(2019/8/16)時間(13:00:00)が成立するので範囲内と判定>
<具体例:対象範囲(開始)の日付(2019/8/16)時間(02:00:00)≦対象の日付(2019/8/16)時間(13:00:00)≦対象範囲(終了)の日付(2019/8/16)時間(13:00:00)が成立するので範囲内と判定>
<具体例:対象範囲(開始)の日付(2019/8/16)時間(02:00:00)≦対象の日付(2019/8/16)時間(13:00:01)≦対象範囲(終了)の日付(2019/8/16)時間(13:00:00)は成立しないので範囲外と判定>
<プログラム例>
Sub 使用例() Dim fdate As Date Dim ftime As Date Dim gdate As Date Dim gtime As Date Dim tdate As Date Dim ttime As Date Dim rtn1 As String fdate = "2019/8/16" ftime = "02:00:00" gdate = "2019/8/16" gtime = "01:59:59" tdate = "2019/8/16" ttime = "13:00:00" rtn1 = 時間範囲判定(fdate, ftime, gdate, gtime, tdate, ttime) MsgBox (gdate & " (" & gtime & ") は、" & Chr(13) & fdate & " (" & ftime & ") ~ " & tdate & " (" & ttime & ") の範囲" & rtn1 & "です") fdate = "2019/8/16" ftime = "02:00:00" gdate = "2019/8/16" gtime = "02:00:00" tdate = "2019/8/16" ttime = "13:00:00" rtn1 = 時間範囲判定(fdate, ftime, gdate, gtime, tdate, ttime) MsgBox (gdate & " (" & gtime & ") は、" & Chr(13) & fdate & " (" & ftime & ") ~ " & tdate & " (" & ttime & ") の範囲" & rtn1 & "です") fdate = "2019/8/16" ftime = "02:00:00" gdate = "2019/8/16" gtime = "13:00:00" tdate = "2019/8/16" ttime = "13:00:00" rtn1 = 時間範囲判定(fdate, ftime, gdate, gtime, tdate, ttime) MsgBox (gdate & " (" & gtime & ") は、" & Chr(13) & fdate & " (" & ftime & ") ~ " & tdate & " (" & ttime & ") の範囲" & rtn1 & "です") fdate = "2019/8/16" ftime = "02:00:00" gdate = "2019/8/16" gtime = "13:00:01" tdate = "2019/8/16" ttime = "13:00:00" rtn1 = 時間範囲判定(fdate, ftime, gdate, gtime, tdate, ttime) MsgBox (gdate & " (" & gtime & ") は、" & Chr(13) & fdate & " (" & ftime & ") ~ " & tdate & " (" & ttime & ") の範囲" & rtn1 & "です") End Sub Function 時間範囲判定(fdate As Date, ftime As Date, gdate As Date, gtime As Date, tdate As Date, ttime As Date) Dim fr As String Dim tr As String Dim ftr As String If DateDiff("d", fdate, gdate) > 0 Then fr = "大" End If If DateDiff("d", fdate, gdate) = 0 Then If DateDiff("s", ftime, gtime) >= 0 Then fr = "大" Else fr = "小" End If End If If DateDiff("d", fdate, gdate) < 0 Then fr = "小" End If If DateDiff("d", gdate, tdate) > 0 Then tr = "大" End If If DateDiff("d", gdate, tdate) = 0 Then If DateDiff("s", gtime, ttime) >= 0 Then tr = "大" Else tr = "小" End If End If If DateDiff("d", gdate, tdate) < 0 Then tr = "小" End If ftr = fr & tr Select Case ftr Case "大大" 時間範囲判定 = "内" Case Else 時間範囲判定 = "外" End Select End Function