該宏根據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
例子:
的輸出畫面應該如何看實例:
.cells(lRow_O,1 ).value = .cells(lRow_O,1).value + 1類似於副本之後的東西 –
感謝您的回覆,不幸的是我不認爲這對我有用,因爲並非所有單元格都是數值,現在更新帖子。 – Phlosef
單元格(lRow_O-1,1).AutoFill目標:=範圍(單元格(lRow_O-1,1),單元格(lRow_O,1)),Type:= xlFillDefault類似於那些類型 –