友人、VBAの時間を比較
タイムスタンプが過去2分間+ 7時間以内にある場合は、列をスキャンしてコピー/ペーストしようとしています。タイムスタンプの日付部分は整列していないので、時刻を変更せずに同じ日付に変換する必要があります。ここで
は私のコードです:(0.704461794(一般的な形式)または1/0/00 16:54:
Sub Timecompare()
Dim i As Integer
Dim lRow As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Sheets("Volm")
Set ws2 = Sheets("Sheet2")
'goal:
'scan all rows in dataset
'if cell time > current time - 2 minutes
'copy pasta
With ws1
'find the last row
lRow = .Cells(.Rows.Count, "E").End(xlUp).Row
'loop through all the rows
For i = 10 To lRow
'if the cell value is greater than time + 7 hours - 2 minutes then copy/paste the row to new sheet
If .Cells(i, 18).Value > Now + TimeSerial(7, 0, 0) - TimeSerial(0, 2, 0) Then
''' just spitting out the values in the comparator above so I can see the results and why they aren't comparing properly '''
ws2.Cells(i, 1).Value = .Cells(i, 18).Value
ws2.Cells(i, 2).Value = Now + TimeSerial(7, 0, 0) - TimeSerial(0, 2, 0)
Exit For
End If
Next i
End With
End Sub
".Cells(I、18).Valueの" 出力は次のようになります"Now + TimeSerial(7、0、0) - TimeSerial(0、2、0)"の出力は次のようになります:42467.75336(一般形式)または4/7/16 6:04 PM(日付形式)
日付形式)。
私は日付を気にしません。私が気にするのは、時間だけです。だから、 ".Cells(i、18).Value"を同じ時間に今日に持っていく、またはNow()+ 7時間 - 2分の日付を1/0/00に戻す方法がありますか?繰り返しますと、私はリンゴにリンゴを手に入れようとしているので、時間を比較することができます。
使用時間の分と秒を –
今日の日付は 'Now'関数の整数部分です - あなたは' INT(NOW) 'を引くことができます()は 'ws2.Cells(i、2).Value'に貼り付ける値を形成しますか? – asongtoruin