Insert Varying # Rows

R

ryguy7272

I got this macro (below) on this DG a while back and used it several times.
I just tried to modify it to insert variable rows in column B and it doesn’t
work. I get an error at:

For I = 1 To Cells(myRow, 1)

I have not idea why this causes an error. Does anyone have a clue? Thanks
in advance!!


Sub InsertVarRows()
Dim myRow As Long

lastcell = Cells(Rows.Count, "B").End(xlUp).Row

myRow = 1
Do Until myRow = lastcell
For I = 1 To Cells(myRow, 1)

If Cells(myRow, 1) <> "" Then
Cells(myRow + 1, 1).Select
Selection.Insert shift:=xlDown
End If

Next
lastcell = Cells(Rows.Count, "B").End(xlUp).Row
myRow = myRow + 1
Loop
End Sub
 
J

JW

Does column A contain the number of cells to insert? Because you are
saying For I = 1 To Cells(myRow, 1) . Cells(myRow,1) is going to
return the value from whatever is in column A and whatever row myRow
is set to. So, if Cells(myRow, 1) = "Bob", you can see why the For
Next wouldn't work. That's about all I can tell you without seeing
your data.
 
R

ryguy7272

JW, you are absolutely right. I changed the code to: For I = 1 To
Cells(myRow, 2)
Now, it works! I can't understand why I didn't see that. Oh well...
Thanks for your time!!

Cordially,
Ryan---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top