時間大小判定応用:A列に日付、B列に時間、C列に値が設定されている履歴データの指定時間を超えた直後のC列の値を取得する
<分類:値取得>
<使用例:指定時間(15:04:00)を超えた次の行のC列の値を取得する>
<注意:サンプルデータを作成するプログラムになっていますので、Sheet1は何もデータがないシートをご用意ください>
<プログラム例>
A | B | C | |
1 | 2018/11/18 | 15:00:00 | 11 |
2 | 2018/11/18 | 15:01:00 | 12 |
3 | 2018/11/18 | 15:02:00 | 13 |
4 | 2018/11/18 | 15:03:00 | 14 |
5 | 2018/11/18 | 15:04:00 | 15 |
6 | 2018/11/18 | 15:05:00 | 16 |
7 | 2018/11/18 | 15:06:00 | 17 |
Sub 使用例() Dim fdate As Date Dim ftime As Date Dim tdate As Date Dim ttime As Date Dim i As long Worksheets("Sheet1").Select 'サンプルデータ作成 Cells(1, 1) = "2018/11/18" Cells(1, 2) = "15:00:00" Cells(1, 3) = "11" Cells(2, 1) = "2018/11/18" Cells(2, 2) = "15:01:00" Cells(2, 3) = "12" Cells(3, 1) = "2018/11/18" Cells(3, 2) = "15:02:00" Cells(3, 3) = "13" Cells(4, 1) = "2018/11/18" Cells(4, 2) = "15:03:00" Cells(4, 3) = "14" Cells(5, 1) = "2018/11/18" Cells(5, 2) = "15:04:00" Cells(5, 3) = "15" Cells(6, 1) = "2018/11/18" Cells(6, 2) = "15:05:00" Cells(6, 3) = "16" Cells(7, 1) = "2018/11/18" Cells(7, 2) = "15:06:00" Cells(7, 3) = "17" fdate = "2018/11/18" ftime = "15:04:00" i = 1 Do While Cells(i, 1) <> "" tdate = Cells(i, 1) ttime = Cells(i, 2) If 時間大小判定(fdate, ftime, tdate, ttime) = "大" Then MsgBox (fdate & " " & ftime & "を超えた次の行のC列の値は" & Cells(i, 3) & "です") Exit Do End If i = i + 1 Loop End Sub Function 時間大小判定(fdate As Date, ftime As Date, tdate As Date, ttime As Date) If DateDiff("d", fdate, tdate) > 0 Then 時間大小判定 = "大" End If If DateDiff("d", fdate, tdate) = 0 Then If DateDiff("s", ftime, ttime) > 0 Then 時間大小判定 = "大" Else 時間大小判定 = "小" End If End If If DateDiff("d", fdate, tdate) < 0 Then 時間大小判定 = "小" End If End Function