苦苦挣扎以获得正确的输出:基于日期和时间的条件

时间:2017-12-21 16:52:14

标签: excel-vba vba excel

我正在创建一个显示前一个1班次数据的程序: 共有3个班次: 班次1:上午6点到下午2点 班次2:下午2点到晚上10点 班次3:晚上10点至早上6点 因此,根据当前时间,我应该能够复制前一班的所有数据。

过去一周我一直在努力破坏我的大脑,直到现在还没有运气,程序运行正常,但我觉得逻辑不对。 我也得到其他数据,这不属于前一班次。

代码的前半部分是根据当前时间显示正确的时间

    Sub previous() 
    Worksheets("Previousshiftdata").Activate
    Dim x As String

    Range("A1").Value = Now

    'To display the shift time for correspondng data i.e. previous shift. this is done in cells E and F

        'To display correct date and time of shift 3
    If (TimeValue(Time) > TimeValue("6:00:00") And TimeValue(Time) <= 
TimeValue("14:00:00")) Then
        x = "Shift 3"
        Range("E1").Value = TimeValue("22:00:00")
        Range("F1").Value = TimeValue("6:00:00")
        Range("B1").Value = Date

         'To display correct date and time of shift 1
    ElseIf (TimeValue(Time) > TimeValue("14:00:00")) And (TimeValue(Time) <= TimeValue("22:00:00")) Then
        x = "Shift 1"
        Range("E1").Value = TimeValue("6:00:00")
        Range("F1").Value = TimeValue("14:00:00")
        Range("B1").Value = Date

        'To display correct date and time of shift 2
    ElseIf (TimeValue(Time) > TimeValue("22:00:00") And TimeValue(Time) <= TimeValue("23:59:59")) Or (TimeValue(Time) > TimeValue("00:00:00") And TimeValue(Time) <= TimeValue("6:00:00")) Then
        x = "Shift 2"
        Range("E1").Value = TimeValue("14:00:00")
        Range("F1").Value = TimeValue("22:00:00")
        If TimeValue(Time) > TimeValue("22:00:00") And TimeValue(Time) < TimeValue("23:59:59") Then
            Range("B1").Value = Date
        ElseIf TimeValue(Time) > TimeValue("00:00:00") And TimeValue(Time) <= TimeValue("6:00:00") Then
            Range("B1").Value = Date - 1
        End If
    End If



    Range("D1").Value = x

    Call previous_ClearCells
    Call previous_transfer

    End Sub

'to clear cells
    Public Sub previous_ClearCells()
    Sheets("Previousshiftdata").Activate
    Range("A3:J300").ClearContents

    End Sub

我正在努力解决以下部分代码,如果我使用正确的逻辑,请告诉我:
我正在尝试根据时间和日期复制行。

    'to copy rows based on condition
    Public Sub previous_transfer()

    Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long

    Dim mydate As Date
    Dim mytime As Date
    Dim mystatus As String

    'to find the last filled row in summary
    lastrow1 = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row

    Sheets("Summary").Activate


    'assign date, time and status of row item in Summary for comparison with previous shift time
    'to start populating data in reverse order to get the latest data first
    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 个答案:

没有答案