【问题标题】:VBA Excel Do While StatementVBA Excel Do While 语句
【发布时间】:2016-04-18 01:48:10
【问题描述】:

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

    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 小时)......

【问题讨论】:

  • 你有什么问题?
  • @Gareth 我已经编辑过了

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


【解决方案1】:

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

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

循环将退出,只要在搜索单元格的下一行中存在值“12”

        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

还总是非常具体地从哪个工作表中对单元格进行寻址。再多一点信息,我可能会更好地帮助你:)

【讨论】:

    猜你喜欢
    • 2016-01-10
    • 2020-05-07
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    相关资源
    最近更新 更多