2017-12-21 7 views
0

이전 시프트 데이터를 표시하는 프로그램을 만듭니다. 시프트 1 : 오전 6시에서 오후 2 시까 지 3 시프트가 있습니다. 시프트 2 : 오후 2시에서 오후 10 시까 지 Shift 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 
+0

'TimeValue ("6:00:00") JohnyL

+0

'IF'블록 내부에는 차별화가 없습니다. 1) 모든 결과가 똑같은 경우 'IF'블록을 갖는 점은 무엇입니까? 2) 같은 코드를 3 번 ​​쓰는 이유는 무엇입니까? 프로 시저가 필요할 때 실행하고 호출하기 위해 호출 할 수있는 루틴을 작성하십시오. –

답변

2
TimeValue("6:00:00") < mytime < TimeValue("14:00:00") 

VBA는이 문제를 처리하지 않습니다. 당신이 그런 일을하고 싶었다 경우에, 당신은해야 할 것 :

TimeValue("6:00:00") < mytime AND mytime < TimeValue("14:00:00") 

당신에게 나의 추천은 주어진 시간은 두 배 사이 인 경우 확인 약간의 기능을하게하는 것입니다 :

을 그래서 같은
Private Function isTimeBetween(timeToCheck As Date, shiftStartTime As String, shiftEndTime As String) 
    Dim shiftStartTime2 As Date 
    Dim shiftEndTime2 As Date 

    shiftStartTime2 = TimeValue(shiftStartTime) 
    shiftEndTime2 = TimeValue(shiftEndTime) 

    If shiftStartTime2 < timeToCheck And timeToCheck < shiftEndTime2 Then 
     isTimeBetween = True 
    Else 
     isTimeBetween = False 
    End If 
End Function 

사용 :

Dim mytime As Date 
mytime = TimeValue("10:00:00") 
isTimeBetween(mytime, "6:00:00", "14:00:00") 

... True를 반환합니다. 작은 함수로 분해하기 위해 코드를 많이 정리합니다.

+0

VBA가 그런 식으로 처리하지 않는다고 생각했습니다. 고맙습니다. 내가 전역 변수를 사용했기 때문에 코드가 원하는대로 실행되지 않았지만 그렇게 선언하지 않았습니다. 이제 작동합니다! –