Copy-Paste a Row via a cell reference

G

Guest

Hi,
I’m trying to do something that is more advanced of my elementary VBA skills.
I would like to create a procedure (macro) that would copy Row 2 and paste
it according to the value in cell B2. (B2 being the row number to paste to).

IE: If B2 says 11. The macro would copy Row 2 , then paste to Row 11.
Row 2 will always be the Row that is copied. But the value in B2 can change
to another row reference.

I’m just learning VBA for Excel and have figured out the Row(2) , Select,
Copy, Paste part. But not sure how to word the code as reference to B2. My
attempts have not worked.

Thank you for your time on this,
Amy
 
N

Nigel

This will do it ----
Rows(2).Copy Destination:=Rows(Range("B2"))

but it would be worth putting a check that the value in B2 is numeric and in
range by using.....

With Range("B2")
If .Value > 0 And .Value <= 65536 Then
Rows(2).Copy Destination:=Rows(.Value)
Else
MsgBox "Value of B2 is out of range"
End If
End With
 
G

Guest

Thank you Tom and Nigel for taking time to answer my question.
It works great!

If you come back here. I have a another question. The "Destination" part of
the code... is that what is called a method or maybe a property?

I'm just beginning to learn.
Thanks again if you come back to this post.
Amy
 
R

Rowan Drummond

Hi Amy

Destination as it is used here is an Argument of the Copy Method.

Hope that clears it up <g>
Rowan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top