Advertisement
zilexa

Excel - copy rows x number of times based on number per row

Feb 23rd, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. EXCEL
  2. ====
  3. Copy every now x number of times based on number in a column
  4.  
  5. 1. open workbook
  6. 2. press ALT+F11
  7. 3. Insert>Add module
  8. 4. paste the code below
  9. 5. Run by clicking the 'play' button or press F5 or in Excel choose tools>macro>run
  10.  
  11. NOTE: make sure your table starts in column A and the number representing the number of times you want to copy that row should be modified in the code below (currently it is "B", change every instance of "B" to the column containing the numbers).
  12.  
  13. Sub CopyData()
  14. Dim lRow As Long
  15. Dim RepeatFactor As Variant
  16.  
  17. lRow = 1
  18. Do While (Cells(lRow, "A") <> "")
  19.  
  20. RepeatFactor = Cells(lRow, "B")
  21. If ((RepeatFactor > 1) And IsNumeric(RepeatFactor)) Then
  22.  
  23. Range(Cells(lRow, "A"), Cells(lRow, "B")).Copy
  24. Range(Cells(lRow + 1, "A"), Cells(lRow + RepeatFactor - 1, "B")).Select
  25. Selection.Insert Shift:=xlDown
  26.  
  27. lRow = lRow + RepeatFactor - 1
  28. End If
  29.  
  30. lRow = lRow + 1
  31. Loop
  32. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement