Loop Question

Sundae

New Member
Joined
Jul 25, 2011
Messages
44
Hi, I have the following code:

Sub Copy_Rows()
Dim nextrow As Long, i As Long

Application.ScreenUpdating = False

With Worksheets("Baby")


For i = 1 To WorksheetFunction.COUNTIF(Workbooks("Data.xlsx").Worksheets("Sheet1").Columns("A"), Range("BALL")) - 1

Range("RUTH").Copy

Range("Total").Insert Shift:=xlDown

nextrow = .Cells(Rows.Count, "A").End(xlUp).Row

Range("A" & nextrow + 1).PasteSpecial

Next i
End With

Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub


****

The code works great except for the loop. I am new to VBA so I hope someone can guide me in the right direction.

Essentially, the code is copying the Named Range Baby the number of times the Named Range BALL is in Column A of the Data.xlsx file (less one).

So, in the Named Range BALL is the number 4 - Baby pastes nicely 4x before the Named Range Total, but then the Baby pastes again 3x after the Named Rnage Total. I do not want it to paste the 3 extra times.

Any advice?

Thank you!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
So, in the Named Range BALL is the number 4 - Baby pastes nicely 4x before the Named Range Total, but then the Baby pastes again 3x after the Named Rnage Total. I do not want it to paste the 3 extra times.

Hi Sundae,

The problem is that you are copying the Range("RUTH") twice on each iteration of the loop.

The first is here...the copied cells are Insert-Pasted above Range("Total")
Code:
Range("RUTH").Copy
Range("Total").Insert Shift:=xlDown

The second time is here...the copied cells are Pasted
below the last row of data [probably below Range("Total")]
Code:
nextrow = .Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & nextrow + 1).PasteSpecial

You can drop this second set of statements to eliminate the duplication.

Since you are looping n-1 times, when you say "Baby pastes nicely 4x above" you are probably counting the pre-existing Range("RUTH") as one paste. That's fine as long as Baby always starts directly above Total. Otherwise, you'll want to remove the -1.
 
Upvote 0
Glad to help. :)

BTW, if you are going to insert a large number of rows,
it will be much faster to Insert X Rows in one step, then
copy down your Ruth Range, instead of doing multiple Inserts.

Good luck!
 
Upvote 0
Hi there, I am now running this VBA code in another workbook and I am getting this error message - Insert Method of Range class failed - I tried searching on the net and found something about CopyOrigin, but that did not work.

Any advice would be appreciated.

Thanks,
 
Upvote 0
Try using this Sub instead. It uses the approach I suggested in the earlier post of inserting all the rows first then copying the range instead of looping.

Code:
Sub Copy_Rows2()
    Dim lCopies As Long
    Application.ScreenUpdating = False
    
    lCopies = WorksheetFunction.CountIf(Workbooks("Data.xlsx") _
        .Worksheets("Sheet1").Columns("A"), Range("BALL")) - 1
    
    With Worksheets("Baby").Range("Total").Resize(lCopies)
        .EntireRow.Insert Shift:=xlDown
        Range("RUTH").Copy Destination:= _
            .Resize(, Range("Ruth").Columns.Count) _
            .Offset(-1 * lCopies, 0)
    End With

    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Jerry, but I am still getting the same error message at the .EntireRow.Insert Shift:=xlDown

Any suggestions?

Thanks,
 
Upvote 0
Is there anything else different about this workbook? such as ...
Protected Sheet
PivotTables on Rows you are trying to insert
Tables (ListObject) on Rows you are trying to insert
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top