2016-01-13 80 views
0

我試圖確定在以下形式輸出的移位中設備的運行時間;VBA Excel 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)......

+0

你的問題是什麼? – Gareth

+0

@Gareth我編輯了它 – Maverick27

回答

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?否則這隻會貫穿... 你也已經在代碼中做了這1行!

 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 

而且總是非常具體從哪個工作您adressing細胞。多一點信息我可能會幫助你更好一點:)