時間大小判定応用: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
比較元の日付と時間、比較先の日付と時間をセットして、時間が比較元より小さいか大きいかを判定する
<分類:判定>
<使用例:比較元の日付(2018/11/10)と時間(23:59:59)、比較先の日付(2018/11/11)と時間(00:00:00)をセットして、時間が比較元より大きいので大と判定する>
<例外:比較元の日付と時間、比較先に日付と時間が同じだった場合は、小にしている
<プログラム例>
Sub 使用例() Dim fdate As Date Dim ftime As Date Dim tdate As Date Dim ttime As Date Dim rtn1 As String fdate = "2018/11/10" ftime = "23:59:59" tdate = "2018/11/11" ttime = "00:00:00" rtn1 = 時間大小判定(fdate, ftime, tdate, ttime) MsgBox (fdate & " " & ftime & " と " & tdate & " " & ttime & " を比較した結果は" & rtn1 & "です") 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
小数点を含む値を整数部と小数部に分ける
<分類:値抜き取り>
<使用例:123.456という小数点を含む値を整数部(123)と小数部(456)に分ける>
<プログラム例>
Sub 使用例() Dim s As String Dim rtn1 As String Dim rtn2 As String '123.456 s = "123.456" Call 部品_小数分離(s, rtn1, rtn2) MsgBox (s & "の整数部は" & rtn1 & " 小数部は" & rtn2 & "です") End Sub Sub 部品_小数分離(p1, r1, r2) 'p1:値1 'r1:整数部 'r2:小数部 Dim ne1 As String Dim ne2 As String Dim nelens As Long nelens = Len(p1) If InStr(p1, ".") > 0 Then '整数部の取り出し ne1 = Mid(p1, 1, InStr(p1, ".") - 1) '小数部の取り出し ne2 = Mid(p1, InStr(p1, ".") + 1, nelens) Else ne1 = p1 ne2 = 0 End If r1 = ne1 r2 = ne2 End Sub