Solved How can I copy rows based on cell values in Excel?

April 20, 2013 at 14:41:02
Specs: Windows XP
Here's an example of similar data:

Name Count Column3 Column4
A 2 50 51
B 1 40 41
C 0 1 2
D 3 52 55

What macro could I create that would produce this data?

Name Count Column3 Column4
A 2 50 51
A 2 50 51
B 1 40 41
D 3 52 55
D 3 52 55
D 3 52 55

Put into words, what macro could I create that would repeat each row the number of times contained in Column B?


See More: How can I copy rows based on cell values in Excel?

Report •

#1
April 20, 2013 at 18:51:15
✔ Best Answer
Sub CopyByNumber()
'Determine last Row with data in Column A
 lastRw = Range("A" & Rows.Count).End(xlUp).Row
'Loop throught rows in reverse order
  For srcRw = lastRw To 2 Step -1
'Determine how many rows to copy and insert
   copyNum = Cells(srcRw, 2) - 1
'If Column B was 0, delete the Row
    If copyNum = -1 Then
      Rows(srcRw).EntireRow.Delete
    End If
   'If Column B was not 0, Copy and insert
   For copyRw = 1 To copyNum
      Rows(srcRw).Copy
      Rows(srcRw).Insert
   Next
  Next
End Sub

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#2
April 26, 2013 at 16:59:02
Excellent... thank you very much!

Report •
Related Solutions


Ask Question