VBA Excel Do While Statement

时间:2016-01-13 07:27:25

标签: excel vba if-statement for-loop do-while

我正在尝试确定以下列形式输出的班次中设备的运行时间;

    27/01/2016 18:00:00         4
    28/01/2016 6:00:00         12
    28/01/2016 18:00:00         4
    29/01/2016 6:00:00          0

宏旨在循环列并记录班次的开始时间和结束时间,即

    28/01/2016 02:00 to 28/01/2016 22:00:00. 

如果块由连续小时组成后为零。

然而,如果时间间隔如下,则第二个4小时成为个别区块:

    27/01/2016 18:00:00         4
    28/01/2016 6:00:00         12
    28/01/2016 18:00:00         0
    29/01/2016 6:00:00          4

我当前的代码如下所示

  Sub EX01()

Dim ColCount As Long
Dim startrow, endrow As Long
Dim i, j As Long
Dim nextRow As Long
Dim Cumm As Double


Set wb = ThisWorkbook
Set ws = wb.Sheets("XACT RE")
Set ws3 = wb.Sheets("RE_EX 01")

ws.Select




startrow = Cells(1, 2).Value
endrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row - 1
Cells(1, 28) = startrow
Cells(1, 29) = endrow

nextRow = 2 'this is the row to put info on RE sheet

For j = 3 To 6
For i = startrow To endrow

    Cumm = 0
    If Cells(i, j) <> 0 Then

        'this is the inital pickup row
            ws3.Cells(nextRow, 1) = ws.Cells(i + 1, 2) - (ws.Cells(i, j).Value / 24) 'get next row time and subtract the hours in this cell
            ws3.Cells(nextRow, 3) = ws.Cells(3, j)
            Cumm = Cumm + ws.Cells(i, j).Value

            'now check how long the run goes for and add the delay data to Cumm
        Do While Cells(i + 1, j).Value >= 12
                        i = i + 1
            Cumm = Cumm + ws.Cells(i, j).Value


        Loop

        'this exits loop if less than 10
        ws3.Cells(nextRow, 2) = ws.Cells(i, 2).Value + ((ws.Cells(i, j).Value) * (0.5 / 12))
        ws3.Cells(nextRow, 8) = Cumm
        nextRow = nextRow + 1


    End If



  Next i
 Next j




End Sub

所以我的问题是如何解释不到12小时的单班制,以及如何解释12小时轮班后发生的小班次(少于12班)....

1 个答案:

答案 0 :(得分:0)

嗨,我在这方面不太好,所以我会立即调整我看到的东西。但首先:哪一列是开始日期/时间,其中是结束日期/时间?这一切都在同一列吗?

Sub EX01()

Dim ColCount As Long    
Dim startrow As Long
Dim endrow As Long
Dim i As Long
Dim j As Long
Dim nextRow As Long
Dim Cumm As Double

我认为仅在1行声明变量是一种古老的做法。我认为这不是一个好习惯。如果我是正确的,这只是跟踪所有使用的变量的更好方法。

Set wb = ThisWorkbook
Set ws = wb.Sheets("XACT RE")
Set ws3 = wb.Sheets("RE_EX 01")

ws.Activate

我最近了解到.select绝不是一个好主意,请尝试解决方法!

'startrow = Cells(1, 2).Value
'endrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row - 1
'Cells(1, 28) = startrow

是否正确,您想要将Cell(B1)的值输入Cell(AB1)?如果是,请尝试这样做:

ws.Cells(1,28).Value = ws.Cells(1,2).Value
'Cells(1, 29) = endrow

startrow = ws.Rows(1)
endrow = ws.Cells(ws.Rows.count, "a").End(xlUp).Row  'this will give you the last entry on this worksheet

'nextRow = 2

这将始终保持2我相信你想要的是这个:

nextRow = (ws3.Cells(ws3.Rows.Count, "a").End(xlUp).Row) + 1

For j = 3 To 6
    For i = startrow To endrow
        ws3.Cells(nextRow, 1) = ws.Cells(i + 1, 2) - (ws.Cells(i, j).Value / 24) 'get next row time and subtract the hours in this cell
        ws3.Cells(nextRow, 3) = ws.Cells(3, j)
        Cumm = Cumm + ws.Cells(i, j).Value

你想在某处打印Cumm吗?否则这只会贯穿...... 此外,您已经在代码中执行了这一行!

        Do While ws.Cells(i + 1, j).Value >= 12
            i = i + 1
            Cumm = Cumm + ws.Cells(i, j).Value
        Loop

循环将退出,只要在搜索到的单元格的下一行中有一个值为&#34; 12&#34;

        ws3.Cells(nextRow, 2) = ws.Cells(i, 2).Value + ((ws.Cells(i, j).Value) * (0.5 / 12))
        ws3.Cells(nextRow, 8) = Cumm
        nextRow = nextRow + 1

    Next i
Next j

End Sub

同样非常具体,您可以从哪个工作表中解析单元格。有了更多信息,我可能会帮助你更好一点:)