Home » excel » excel – Copy row from sheet 1 to sheet 2 based on row number

excel – Copy row from sheet 1 to sheet 2 based on row number

Posted by: admin May 14, 2020 Leave a comment

Questions:

Hi i want to be able to enter a row number in “PrintSheet” cell G15 and copy the specified numbered row from “Log” to row 40 on “PrintSheet”, I have tried this and lots of other code but none does what Ii need, this one keeps stopping at:

Sheets(“Log”).Rows(15).EntireRow.Copy

but not sure why

My Code

Sub GetOrder()
    Dim val As Integer

    val = Sheets("PrintSheet").Range("G15")

    Sheets(“Log”).Rows(15).EntireRow.Copy

    Sheets("PrintSheet").Range("A40").Select
    Sheets(“PrintSheet”).Paste

End Sub
How to&Answers:

First, you are using and not ".

Second, Copy >> Paste is a 1 line command, and there’s no need to Select the range before you paste in it.

Third, and an answer why you are getting the error you asked in your post, to copy an entire row you can either use:

Sheets("Log").Rows(15).Copy

or

Sheets("Log").Range("A15").EntireRow.Copy

And last, you need to use the row value from cell “G15” val inside your copy, so it should be Sheets("Log").Rows(val).Copy.

Code

Option Explicit

Sub GetOrder()

    Dim val As Long

    val = Sheets("PrintSheet").Range("G15").Value

    ' copy >> paste is 1-line command
    Sheets("Log").Rows(val).Copy Destination:=Sheets("PrintSheet").Range("A40")

End Sub