2016-02-25 64 views
1

該宏根據M2中的單元格值複製並粘貼行X次的值。它反覆粘貼確切的數字。有沒有辦法改變它,以便數字在複製下來時會上升?基於單元格值複製行X次數

E.g.如果A2包含「hello 3」,則在運行宏A3後將包含「hello 4」,A4將包含「hello 5」。輸入屏幕,輸出屏幕應該是什麼樣子

Sub Sample() 
Dim wsI As Worksheet, wsO As Worksheet 
Dim lRow_I As Long, lRow_O As Long, i As Long, j As Long 

'~~> Set your input and output sheets 
Set wsI = ThisWorkbook.Sheets("Sheet1") 
Set wsO = ThisWorkbook.Sheets("Sheet1") 

'~~> Output row 
lRow_O = wsO.Range("A" & wsO.Rows.Count).End(xlUp).Row + 1 

With wsI 
    '~~> Get last row of input sheet 
    lRow_I = .Range("A" & .Rows.Count).End(xlUp).Row 

    '~~> Loop through the rows 
    For i = 2 To lRow_I 
     '~~> This will loop the number of time required 
     '~~> i.e the number present in cell M 
     For j = 1 To Val(Trim(.Range("M" & i).Value)) 
      '~~> This copies 
      .Rows(i).Copy wsO.Rows(lRow_O) 
      '~~> Get the next output row 
      lRow_O = wsO.Range("A" & wsO.Rows.Count).End(xlUp).Row + 1 
     Next j 
    Next i 
End With 
End Sub 

例子:

Input

的輸出畫面應該如何看實例:

Output

+0

.cells(lRow_O,1 ).value = .cells(lRow_O,1).value + 1類似於副本之後的東西 –

+0

感謝您的回覆,不幸的是我不認爲這對我有用,因爲並非所有單元格都是數值,現在更新帖子。 – Phlosef

+0

單元格(lRow_O-1,1).AutoFill目標:=範圍(單元格(lRow_O-1,1),單元格(lRow_O,1)),Type:= xlFillDefault類似於那些類型 –

回答

0

其實沒有必要j循環如果你使用resize方法。

Sub Sample() 
Dim wsI As Worksheet, wsO As Worksheet, lCounter As Long 
Dim lRow_I As Long, lRow_O As Long, i As Long 

Set wsI = ThisWorkbook.Sheets("Sheet1") 
Set wsO = ThisWorkbook.Sheets("Sheet2") 

With wsI 
    lCounter = Val(Trim(.Range("M" & i).Value)) 
    lRow_I = .Range("A" & .Rows.Count).End(xlUp).Row 

    For i = 2 To lRow_I 
     lRow_O = wsO.Range("A" & wsO.Rows.Count).End(xlUp).Row + 1 
     .Rows(i).Copy wsO.Rows(lRow_O).Resize(lCounter) 
    Next i 

End With 
+0

我不認爲方式會增加計數器。 –

+0

我真的很感謝你的幫助,但Nathan_Sav是正確的,這不會增加計數器 – Phlosef

+0

是的,它肯定不會增加文本值的計數器。我只是發佈它來避免j循環來處理代碼的擴展。 – Sixthsense

0

我升級我的解決方案有「反」增加

Sub Sample() 
Dim wsI As Worksheet, wsO As Worksheet 
Dim lRow_I As Long, lRow_O As Long, i As Long, nRowsToPaste As Long 
Dim rngToCopy As Range, rngToPaste As Range 

'~~> Set your input and output sheets 
Set wsI = ThisWorkbook.Sheets("SheetI") 
Set wsO = ThisWorkbook.Sheets("SheetO") '<=== I made it different that wsI 

'~~> Output row 
lRow_O = wsO.Range("A" & wsO.Rows.Count).End(xlUp).row + 1 
With wsI 
    '~~> Get last row of input sheet 
    lRow_I = .Range("A" & .Rows.Count).End(xlUp).row 

    '~~> Loop through the rows 
    For i = 2 To lRow_I 
     nRowsToPaste = val(Trim(.Range("M" & i).Value)) '<== set number of rows to be pasted 

     Set rngToCopy = .Range(.Cells(i, 1), .Cells(i, wsI.Columns.Count).End(xlToLeft)) '<== set range to be copied 
     Set rngToPaste = wsO.Rows(lRow_O).Resize(1, rngToCopy.Columns.Count)    '<== set 1st row of the range to be pasted 

     rngToCopy.Copy rngToPaste  '<== copy&paste the 1st row in wsO sheet              '<== copy and paste the 1st row 
     Call Prefix(rngToPaste) '<== differentiate each single cell of pasted range by means of adding a different prefix. this will subsequently have autofill method work on cells with originally the same value as well 

     With rngToPaste 
      .AutoFill .Resize(nRowsToPaste + 1) ' <== fill all rows exploiting AutoFill method, which will work on every column being their 1st row different from each other 
      .Resize(nRowsToPaste + 1).Replace What:="%%*%%", Replacement:="", LookAt:=xlPart '<== remove prefix 
     End With 

     lRow_O = lRow_O + nRowsToPaste + 1 '<== GET the next output row 

    Next i 

End With 
End Sub 

Sub Prefix(rng As Range) 
Dim j As Long 
With rng 
    For j = 1 To .Columns.Count 
     .Cells(1, j).Value = "%%" & j & "%%" & .Cells(1, j).Value 
    Next j 
End With 
End Sub 

它消除了內部j-循環的需要,簡單地升級lRow_O

+0

Sixsthsense發佈幾乎相同的解決方案,而我寫我的。但他將「lCounter」變量放置在「i循環」之外,以便粘貼的行數始終相同。我認爲這不是Phlosef想要的 – user3598756

+0

嗨,我真的很感謝你對此的幫助,但它似乎有同樣的問題,但不增加計數器。 – Phlosef

+0

「lRow_O = lRow_O + nRowsToPaste」是增量語句。只要「nRowsToPaste」> 0,它就會增加。可能由於「val(Trim(.Range(」M「&i).Value))」「即出現什麼內容」而導致其結果爲「0」。「Range(」M「&i).Value 」。 – user3598756

相關問題