2016-12-07 104 views
-4

我有一個excel表格中的數據按照下面的圖片:重複多行中多次Excel VBA中,與計算

Input

,我需要把它載列如下:

Output

有人可以幫助我爲這個請求提供一個excel宏,因爲有很多這樣的行,我對excel宏不太熟練嗎?

+1

您好,歡迎來到Stackoverflow。本網站不是「爲我開發此代碼」網站,而是一個互相幫助的網站,查看我們的代碼無法正常工作。你需要的是可以實現的。使用從列表底部開始的循環,然後根據int列中的值插入行數,然後將時間差除以該數並填充時間。嘗試是併發布你在這裏得到的,我們可以看看 –

+0

你可以在第一秒和第三列的VBA中應用shorting。 – MarmiK

回答

0

在我的測試中,這完全是你要求的。

您需要重新命名錶格,具體取決於原始數據表名稱的表名和輸出/結果表名稱。

Option Explicit 

Sub splittinghours() 

     Dim DataSheet As Worksheet 
     Dim ResultSheet As Worksheet 
     Set DataSheet = ThisWorkbook.Sheets("Sheet1") 
     Set ResultSheet = ThisWorkbook.Sheets("Sheet2") 

     Dim DataSheetLastRow As Long 
     With DataSheet 
      DataSheetLastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row 
     End With 

     Dim ActualWarehouse As String 
     Dim ActualDate As String 
     Dim InTime As Date 
     Dim OutTime As Date 
     Dim Duration As Long 
     Dim CurrentRow As Long 
     Dim DurationCounter As Long 
     Dim SegmentedDuration As Date 
     Dim ResultSheetNextFreeLine As Long 

     ResultSheet.Range(Cells(2, "A"), Cells(ResultSheet.Rows.Count, ResultSheet.Columns.Count)).Delete 

     ResultSheetNextFreeLine = 0 

     For CurrentRow = 2 To DataSheetLastRow 

      ActualWarehouse = DataSheet.Cells(CurrentRow, "A").Value 
      ActualDate = DataSheet.Cells(CurrentRow, "B").Value 
      InTime = DataSheet.Cells(CurrentRow, "C").Value 
      OutTime = DataSheet.Cells(CurrentRow, "D").Value 
      Duration = DataSheet.Cells(CurrentRow, "E").Value 
      SegmentedDuration = (OutTime - InTime)/Duration 

      ResultSheetNextFreeLine = ResultSheet.Cells(ResultSheet.Rows.Count, "A").End(xlUp).Row 

      For DurationCounter = 1 To Duration 

       With ResultSheet 

        .Cells(ResultSheetNextFreeLine + DurationCounter, "A").Value = ActualWarehouse 
        .Cells(ResultSheetNextFreeLine + DurationCounter, "B").Value = ActualDate 
        .Cells(ResultSheetNextFreeLine + DurationCounter, "C").Value = InTime 
        .Cells(ResultSheetNextFreeLine + DurationCounter, "D").Value = InTime + SegmentedDuration 
        InTime = InTime + SegmentedDuration 

       End With 

      Next DurationCounter 

     Next CurrentRow 

End Sub 
+0

非常感謝Jean!但是,下面的代碼導致了一個錯誤,我評論了它。我相信它是在填充之前清除結果表單。代碼工作得很好。非常感謝您的及時幫助 ResultSheet.Range(Cells(2,「A」),Cells(ResultSheet.Rows.Count,ResultSheet.Columns.Count))。刪除 –

+0

@SrivathsanV好東西,享受stackoverflow。請記住標記正確,並提出您選擇的問題答案。 –