Copy entire row from another worksheet

Jazzer

Board Regular
Joined
Jun 14, 2011
Messages
71
Hello,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I am looking for some code which will enable me to copy the entire row from another worksheet based on specific criteria<o:p></o:p>
<o:p></o:p>
In worksheet REC I want to copy any row where a cell in column G contains the text WRONG<o:p></o:p>
Also I want to copy any row where column H contains a value greater or less than zero<o:p></o:p>
<o:p></o:p>
The rows need to be pasted into worksheet exceptions starting in row 7<o:p></o:p>
<o:p></o:p>
Many Thanks<o:p></o:p>
James<o:p></o:p>
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try

Code:
Sub CopyRows()
Dim LR1 As Long, LR2 As Long, i As Long
LR1 = WorksheetFunction.Max(7, Sheets("exceptions").Range("A" & Rows.Count).End(xlUp).Row + 1)
With Sheets("Rec")
    LR2 = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR2
        If .Range("G" & i).Value Like "*WRONG*" Or .Range("H" & i).Value <> 0 Then
            .Rows(i).Copy Destination:=Sheets("Exceptions").Range("A" & LR1)
            LR1 = LR1 + 1
        End If
    Next i
End With
End Sub
 
Upvote 0
A filter may be the way to go. It can be automated but, given that you can filter everything in one hit, a manual copy/paste may do. Autofilter can't do the job in one go but the Advanced Filter can.

1. Create 4 blank rows at the top of your worksheet (REC).
2. Copy the headings from Row 5 to Row 1.
3. In G2 enter WRONG; in H3 enter <>0
4. Place your cursor somewhere in the main body of the table (say, A5) and start the Advanced Filter. In 2003 and below, Data > Filter > Advanced Filter. In 2007 and higher, Data > Filter group > Advanced.
The filter should highlight the man table as the filter list.
5. Click the rangefinder next to Criteria, and drag over A1:H3. You can extend to the right to include any additional headings but DO NOT add blank rows.
6. Click OK and your data should be filtered. Now it's a simple copy/paste.

Denis
 
Upvote 0
Try

Code:
Sub CopyRows()
Dim LR1 As Long, LR2 As Long, i As Long
LR1 = WorksheetFunction.Max(7, Sheets("exceptions").Range("A" & Rows.Count).End(xlUp).Row + 1)
With Sheets("Rec")
    LR2 = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR2
        If .Range("G" & i).Value Like "*WRONG*" Or .Range("H" & i).Value <> 0 Then
            .Rows(i).Copy Destination:=Sheets("Exceptions").Range("A" & LR1)
            LR1 = LR1 + 1
        End If
    Next i
End With
End Sub

Thanks for that - I am having trouble getting it to work - at the moment nothing seems to happen when I run this macro - is there anything I should be aware of ?

"Cant execute code in break mode"

thanks
 
Last edited:
Upvote 0
Click the Reset button .

If column A is empty try

Rich (BB code):
Sub CopyRows()
Dim LR1 As Long, LR2 As Long, i As Long
LR1 = WorksheetFunction.Max(7, Sheets("exceptions").Range("A" & Rows.Count).End(xlUp).Row + 1)
With Sheets("Rec")
    LR2 = .Range("G" & Rows.Count).End(xlUp).Row
    For i = 1 To LR2
        If .Range("G" & i).Value Like "*WRONG*" Or .Range("H" & i).Value <> 0 Then
            .Rows(i).Copy Destination:=Sheets("Exceptions").Range("A" & LR1)
            LR1 = LR1 + 1
        End If
    Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,480
Members
452,915
Latest member
hannnahheileen

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