2017-02-20 228 views
0

特定單元格的值插入整行我有這種格式的問題使用使用VBA

from hrs to hrs quantum rate 
12:00:00 6:00:00 100  1.8 
12:00:00 5:00:00 125  1.6 

我想這個數據在給定的格式一樣

from hrs to hrs  quantum rate 
12:00:00 6:00:00  -50  1800.00 
12:00:00 6:00:00  -50  1800.00 
12:00:00 5:00:00  -50  1600.00 
12:00:00 5:00:00  -50  1600.00 
12:00:00 5:00:00  -25  1600.00 

,我使用下面的代碼:

Option Explicit 

Sub main() 
    Dim data As Variant 
    Dim iData As Long, datum As Long, iRow As Long 
With Range("A1", Cells(Rows.Count, 1).End(xlUp)) 
    data = .Resize(, 4).Value 
    iData = LBound(data) 
    Do 
     datum = data(iData, UBound(data, 2) - 1) 
     Do While datum > 0 
      iRow = iRow + 1 
      .Cells(iRow).Resize(, 4) = Application.Index(data, iData, 0) 
      .Cells(iRow, UBound(data, 2) - 1).Value = WorksheetFunction.Min(50, datum) 
      datum = datum - 50 
     Loop 
     iData = iData + 1 
    Loop While iData <= UBound(data) 
    .Resize(1).Copy 
    .Resize(iRow).PasteSpecial xlPasteFormats 
    Application.CutCopyMode = False 
End With 
End Sub 
+0

@ user3598756 recoginze this code? –

+0

@ShaiRado,是的,我這樣做:-) – user3598756

+0

你還沒有問過一個具體的問題。 – Squashman

回答

1

那就是:

Option Explicit 

Sub main() 
    Dim data As Variant 
    Dim iData As Long, datum As Long, iRow As Long 
    With Range("A1", Cells(Rows.Count, 1).End(xlUp)) 
     data = .Resize(, 4).Value 
     iData = LBound(data) 
     Do 
      datum = data(iData, UBound(data, 2) - 1) 
      Do While datum > 0 
       iRow = iRow + 1 
       .Cells(iRow).Resize(, 4) = Application.Index(data, iData, 0) 
       .Cells(iRow).Offset(, 3).Value = .Cells(iRow).Offset(, 3).Value * 1000 
       .Cells(iRow, UBound(data, 2) - 1).Value = -WorksheetFunction.Min(50, datum) 
       datum = datum - 50 
      Loop 
      iData = iData + 1 
     Loop While iData <= UBound(data) 
     .Resize(1, 4).Copy 
     .Resize(iRow, 4).PasteSpecial xlPasteFormats 
     Application.CutCopyMode = False 
    End With 
End Sub 
+0

@satyendrasharma,嘗試此代碼後有看看[這裏](http://stackoverflow.com/help/someone-answers) – user3598756

+0

先生,當我運行宏它顯示類型不匹配錯誤,因爲我使用列標題也請幫助 –

+0

不準確,但你可以嘗試改變'Range(「A1」,Cells(...')'Range(「A2」,Cells(...')讓我知道 – user3598756