1つ前のシフトデータを表示するプログラムを作成しています:シフトは3つあります。シフト1:午前6時から午後2時シフト2:午後2時から午後10時シフト午後3時10分から午前6時まで。現在の時間に応じて、前の1シフトからすべてのデータをコピーできるはずです。VBAのロジックを悩ます:日付と時刻を含む
私は今週私の脳を壊してしまいましたが、今までは運がなかったので、プログラムは正常に動作しましたが、ロジックは正しくはありません。私は以前のシフトに属していない他のデータも取得しています。
ここにロジックのみを含む抜粋があります: これに助けていただければ幸いです!あなたはそれを期待しているよう
For i = lastrow1 To (lastrow1 - 150) Step -1
mydate = Sheets("Summary").Cells(i, "A").Value
mytime = Sheets("Summary").Cells(i, "B").Value
mystatus = Sheets("Summary").Cells(i, "J").Value
Sheets("Previousshiftdata").Activate
lastrow2 = Sheets("Previousshiftdata").Range("A" & Rows.Count).End(xlUp).Row
'j indicates destination row no i.e. row no. in previoushift
j = lastrow2 + 1
'to get shift 3 data
If (x = "Shift 3" And (mydate = Date - 1 And mytime > TimeValue("22:00:00"))) Or (mydate = Date And mytime > TimeValue("00:00:00") And mytime < TimeValue("6:00:00")) Then
Sheets("Summary").Activate
Sheets("Summary").Range(Cells(i, "A"), Cells(i, "J")).Copy
Sheets("Previousshiftdata").Activate
Sheets("Previousshiftdata").Range(Cells(j, "A"), Cells(j, "J")).Select
Selection.PasteSpecial Paste:=xlPasteValues
'to get shift 2 data
ElseIf ((mydate = Date) And (mytime > TimeValue("14:00:00")) And (mytime < TimeValue("22:00:00"))) Or ((mydate = Date - 1) And (mytime > TimeValue("14:00:00")) And (mytime < TimeValue("22:00:00"))) Then
Sheets("Summary").Activate
Sheets("Summary").Range(Cells(i, "A"), Cells(i, "J")).Copy
Sheets("Previousshiftdata").Activate
Sheets("Previousshiftdata").Range(Cells(j, "A"), Cells(j, "J")).Select
Selection.PasteSpecial Paste:=xlPasteValues
'to get shift 1 data
ElseIf (TimeValue("6:00:00") < mytime < TimeValue("14:00:00")) And (mydate = Date) Then
Sheets("Summary").Activate
Sheets("Summary").Range(Cells(i, "A"), Cells(i, "J")).Copy
Sheets("Previousshiftdata").Activate
Sheets("Previousshiftdata").Range(Cells(j, "A"), Cells(j, "J")).Select
Selection.PasteSpecial Paste:=xlPasteValues
End If
'to clear clipboard
Application.CutCopyMode = False
Next i
Sheets("Previousstatus").Activate
End Sub
'TimeValue(" 6:00:00 ")
JohnyL
' IF'ブロック内には区別がありません。 1)すべての結果が同じ場合、IFブロックを持つ点は何ですか? 2)同じコードを3回書くのはなぜですか?プロシージャが必要なときに実行して呼び出すために呼び出すことができるルーチンを作成します。 –