Better Way to Copy Rows Out of a Loop?

beartooth91

Board Regular
Joined
Dec 15, 2024
Messages
76
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
 
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
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,226,795
Messages
6,193,032
Members
453,772
Latest member
aastupin

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