Better Way to Copy Rows Out of a Loop?

beartooth91

New Member
Joined
Dec 15, 2024
Messages
46
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
So.....the code below works....as long as the copy from sheet is selected. (If the copy to sheet is selected, while running the procedure; nothing is copied.) This is a common problem I've had when copying between sheets and/or workbooks using With Statements. I need to paste values as some of the data contains formulas or lookups. As I said; the code works, but lots of forum readings say you're not supposed to use 'Activate' and 'Select'.

VBA Code:
Sub Copy_Master()
'
'Copies point info from Master IO List Workbook to the Imported sheet in the Master Database Workbook
'
Application.ScreenUpdating = False
Call Clear_Imported
Call Open_Master_IO
'
'Count rows of data in NIC Master IO List worksheet
 Dim a As Long, b As Long, entry As Range
 a = Workbooks("NIC Master IO List.xlsm").Worksheets("NIC Master IO List").Range("B" & Rows.Count).End(xlUp).Row
'
'Determine start row to paste in Imported worksheet of Master Database Workbook
 '
 'Workbooks("NIC Master IO List.xlsm").Worksheets("NIC Master IO List").Range("B11:BP" & a).Copy
 'Workbooks("NIC Master Database.xlsm").Worksheets("Imported").Range("B11:BP" & b).PasteSpecial Paste:=xlPasteValues 'xlPasteAll
 Workbooks("NIC Master IO List.xlsm").Worksheets("NIC Master IO List").Activate
 With Workbooks("NIC Master IO List.xlsm").Worksheets("NIC Master IO List")
   For Each entry In Range("BP11:BP" & a)
     If entry.Value = "Valid" Then
        b = Workbooks("NIC Master Database.xlsm").Worksheets("Imported").Range("B" & Rows.Count).End(xlUp).Row + 1
        entry.EntireRow.Copy 'Destination:=Workbooks("NIC Master Database.xlsm").Worksheets("Imported").Range("A" & b)
        Workbooks("NIC Master Database.xlsm").Worksheets("Imported").Activate
        Workbooks("NIC Master Database.xlsm").Worksheets("Imported").Range("A" & b).PasteSpecial Paste:=xlPasteValues
        Workbooks("NIC Master IO List.xlsm").Worksheets("NIC Master IO List").Activate
     End If
    Next entry
End With
 Application.CutCopyMode = False
 '
End Sub
 
The VBA Union method in Excel is designed to combine ranges into a single range. You use Union to combine multiple ranges based on a common criterion as in your case, cell values that match criteria “Valid”
However, Union method cannot combine ranges if one of the ranges does not exist therefore, until object variable rngValid is defined the first time, you cannot include it in Union statement.

To do this, you include a if statement to test each cell in the range

Rich (BB code):
If rngValid Is Nothing Then

And use the set statement to add it to the range

Rich (BB code):
Set rngValid = Cell

Now after the range is defined the first time, you add to the existing range(s) with the Union command to build a single range.

Rich (BB code):
Set rngValid = Union(Cell, rngValid)

You can read more in VBA help file if needed.

Union method is sufficient in many cases but there are, as @Peter_SSs has shown, still much quicker methods to produce required result if needed.

Hope Helpful

Dave
Where is the row coming from to copy? **If I understand your code**, the array - in memory - is only one column BP11:BP & LastRow that have valid entries, not the entire set of rows.....
So I assume it goes back to the copy-from worksheet to copy the rows.....?
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Where is the row coming from to copy?

At this point in the code, Union has combined ALL the ranges that match the criteria
Copying the entirerow of the range & pasting to destination sheet is then undertaken in one single action without any need for further interaction with the worksheet which is why code is faster.

VBA Code:
If Not rngValid Is Nothing Then
        With wsImported
            LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
            rngValid.EntireRow.Copy
            .Cells(LastRow, 1).PasteSpecial Paste:=xlPasteValues
        End With
    End If

Dave
 
Upvote 0

Forum statistics

Threads
1,225,322
Messages
6,184,269
Members
453,224
Latest member
Prasanna arachchi

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