[英]Copy and paste rows x times based if x=“cell value”
copy rows x-times and paste in new sheet x-amount of Rows. 将行复制x次并粘贴到新工作表中x行数。 Then change the cell value of Col "S" to "1 of x", "2 of x", "3 of x"...."x of x".
然后将Col“S”的单元格值更改为“x中的1”,“x中的2”,“x中的3”......“x的x”。
"Sheet1" Col "S" holds the x-numerical value. “Sheet1”Col“S”保存x数值。 Copy col "B" through "AJ" in each row with value and paste in "sheet3".
使用值将每行中的col“B”复制到“AJ”并粘贴到“sheet3”中。
I have used some code found in stack overflow at " Copy Row X amount of times based on cell value ", but need it to change value of "S" in each added row. 我已经在“ 基于单元格值复制行X次数 ”的堆栈溢出中找到了一些代码,但需要在每个添加的行中更改“S”的值。
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("Sheet3")
'~~> Output row
lRow_O = wsO.Range("B" & wsO.Rows.Count).End(xlUp).Row + 1
With wsI
'~~> Get last row of input sheet
lRow_I = .Range("B" & .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 S
For j = 1 To Val(Trim(.Range("S" & i).Value))
'~~> This copies
.Rows(i).Copy wsO.Rows(lRow_O)
'~~> Get the next output row
lRow_O = wsO.Range("B" & wsO.Rows.Count).End(xlUp).Row + 1
Next j
Next i
End With
End Sub
I expect each row on sheet 1 to be on sheet 3 the amount of times cell "S" ("S"= numerical digit between 1-15) in that row's value is. 我希望第1页上的每一行都在第3页上,该行的值为单元格“S”(“S”= 1-15之间的数字)的次数。 Then each new line added will have "S" value "1 of S", "2 of S", and so on to "S of S".
然后,每个添加的新行将具有“S”值“1的S”,“2的S”,依此类推到“S的S”。
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Dim lRow_I As Long, lRow_O As Long, i As Long, j As Long
Dim rw As Range, n As Long
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet3")
lRow_O = wsO.Range("B" & wsO.Rows.Count).End(xlUp).Row + 1
lRow_I = wsI.Range("B" & wsI.Rows.Count).End(xlUp).Row
For i = 2 To lRow_I
Set rw = wsI.Rows(i)
n = Val(Trim(rw.Range("S1").Value))
rw.Copy wsO.Rows(lRow_O).Resize(n) 'makes n copies of the row
'add the "x of n" values
wsO.Cells(lRow_O, "S").Resize(n, 1).Value = Evaluate("=ROW(1:" & n & ") & "" of " & n & """")
'or alternatively / less complex...
for j = 1 to n
wsO.Cells(lRow_O, "S").Offset(j-1, 0).Value = j & " of " & n
next j
lRow_O = lRow_O + n
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.