Copy Data

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Hello All

I know this is incorrect , Can someone explain why all my data which meets the criteria <> 4 is not copied to sheet 7. Is the rows(j) the issue


Code:
Sub CopyNot4Q()
    
        Dim lr1 As Long, p As Long
        Dim ws As Worksheet
        
        Set ws = Sheet6
        
        ws.Activate
        
        lr1 = ws.Range("B" & Rows.Count).End(xlUp).Row
        
        For p = 2 To lr1
        
        
        If Left(Cells(p, "B"), 1) <> 4 Then
        
               
        Rows(p).Copy Destination:=Sheets("Sheet7").Range("A1")
        
        End If
        Next p
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Make Sheet6 the active sheet and try this macro:
Code:
Sub CopyNot4Q()
    Application.ScreenUpdating = False
    Dim lr1 As Long, p As Long
    lr1 = Sheets("Sheet6").Range("B" & Rows.Count).End(xlUp).Row
    For p = 2 To lr1
        If Left(Cells(p, "B"), 1) <> 4 Then
            Rows(p).EntireRow.Copy Sheets("Sheet7").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If
    Next p
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
You are very welcome. :) This line of code:
Code:
Set ws = Sheet6
should have read:
Code:
 ws = Sheets("Sheet6")
Therefore lr1 had no value. Also, this line:
Code:
 Rows(p).Copy Destination:=Sheets("Sheet7").Range("A1")
would have copied each row on top of each other at cell A1 whereas this line:
Code:
Rows(p).EntireRow.Copy Sheets("Sheet7").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
copies the row below the last row which has data. I hope that makes sense.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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