Copy Data to Row 5 VBA

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good afternoon

I am trying to copy data from worksheet "Project" column G "Over" to inserted sheet "Over" to row 5 of the new sheet. Code I am using is copying the header row (row 4) and breaking at that point.

Not sure what is going wrong.


Sub Project()


Dim i As Long
Dim lr As Long
Dim lr2 As Long
Dim result As String

Sheets.Add after:=Worksheets("Project")
ActiveSheet.Name = "Over"
Sheets("Project").Activate



lr = Sheets("Project").Cells(Rows.Count, "G").Row


For i = 5 To lr

If Sheets("Project").Cells(i, "G").Value = "Over" Then
result = Cells(i, "G").Value


Sheets("Project").Rows(4).Copy Destination:=Sheets("Over").Rows(4)


lr2 = Sheets(result).Cells(Rows.Count, "G").End(xlUp).Row + 5

Rows(i).Copy desination:=Sheets(result).Rows(lr2)



End If

Next i


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Untested but try
Code:
Sub Project()


    Dim i As Long
    Dim lr As Long
    Dim lr2 As Long
    Dim result As String
    
    Sheets.Add(after:=Worksheets("Project")).Name = "Over"
    With Sheets("Project")
        .Rows(4).Copy Destination:=Sheets("Over").Rows(4)
        lr = .Cells(Rows.Count, "G").End(xlUp).Row
        For i = 5 To lr
            If .Cells(i, "G").Value = "Over" Then
                result = Cells(i, "G").Value
                lr2 = Sheets("Over").Cells(Rows.Count, "G").End(xlUp).Row + 5
                .Rows(i).Copy destination:=Sheets("Over").Rows(lr2)
            End If
        Next i
    End With


End Sub
Cheers Mark
 
Last edited:
Upvote 0
Hi Fluff missing a "t" in Destination...


Code:
.Rows(i).Copy [COLOR="#FF0000"]desination[/COLOR]:=Sheets("Over").Rows(lr2)

Not sure if you have enough time to edit.
 
Upvote 0
Thanks Fluff

Works perfectly.
Glad to help & thanks for the feedback

Not sure what i was doing wrong with my code.
The only actual problem was the missing t from destination.

However, with this line you were setting lr to 1048576
Code:
lr = Sheets("Project").Cells(Rows.Count, "G").Row

And then here you were also copying your header row every time Col G said Over
Code:
If Sheets("Project").Cells(i, "G").Value = "Over" Then
result = Cells(i, "G").Value


Sheets("Project").Rows(4).Copy Destination:=Sheets("Over").Rows(4)
 
Upvote 0
I didn't realize the missing "t" was directed for my code.

Thanks again both of you

It was infact aimed at me & I just managed to edit my code before time ran out.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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