Copy and Paste Rows - Conditionally - to Last Row

meledward23

New Member
Joined
Aug 2, 2016
Messages
19
I am trying to copy and paste rows, based on a condition being meet to the last row of the sheet.


So basically what I am trying to do is:
Range("Assemselect") = cell k2, its a text value.
Column A - has matching values.
I want to copy the ENTIREROW of Column A when it matches.
Then paste it at the end. (Actually I think eventually I will want to insert before row 6, but I am undecided), that I believe I can handle.
Getting the copy paste to work correctly and run through the range is my issue.

this code just passes through one time and does nothing.

VBA Code:
Dim lrow As Long
Dim rng As Range
Dim rngrow As Range
lrow = Sheets("HS Template").Range("a" & Rows.Count).End(xlUp).Row 'find last row (ONLY VISBLE)
 
      
     On Error Resume Next
   For Each rngrow In Range("a1:a" & lrow).Rows 'sets rngrow to full visible range
       If rngrow.Cells(1).Value = Range("assemselect").Value Then 'finds where column A = $k$2
     With Sheets("HS TEMPLATE")
     .rngrow.EntireRow.Copy
    .Rows(lrow).Insert Shift:=x1down
        End With
                End If
        
          
   Next rngrow
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Managed to get it working with this.
A lot of cleaning up on it for proper range and such, but the core copy paste If does what I needed.



VBA Code:
For i = 2 To 200
If Worksheets("HS TEMPLATE").Cells(i, 1).Value = Range("ASSEMSELECT") Then
 Worksheets("HS TEMPLATE").Rows(i).copy
 Worksheets("TEST").Activate
 b = Worksheets("TEST").Cells(Rows.Count, 1).End(xlUp).Row
 Worksheets("TEST").Cells(b + 1, 1).Select
 ActiveSheet.Paste
 End If
 Next i
 
Upvote 0
You can shorten that to

VBA Code:
For i = 2 To 200
If Worksheets("HS TEMPLATE").Cells(i, 1).Value = Range("ASSEMSELECT") Then _
Worksheets("HS TEMPLATE").Rows(i).Copy Worksheets("TEST").Cells(Rows.Count, 1).End(xlUp)(2)
Next i

Although I would turn off ScreenUpdating before running it or better still look at doing it with the AutoFilter.
 
Upvote 0
Just in case you want to try it with the Autofilter, the code will be something like....

VBA Code:
    Application.ScreenUpdating = False
    
    With Worksheets("HS TEMPLATE").Range("A1:A200")
    
        .AutoFilter 1, Range("ASSEMSELECT").Value
        
        On Error Resume Next
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(12).Copy _
        Worksheets("TEST").Range("A" & Rows.Count).End(xlUp)(2)
        On Error GoTo 0
        
        .AutoFilter
    
    End With
    
    Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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