Link to home
Start Free TrialLog in
Avatar of AndresHernando
AndresHernando

asked on

Excel VBA - Copy / Paste without using "selection"

I want to copy values of one row onto another row without "selecting"

Here's my attempt:

Range(vSourceRow).Copy Destination:=Range("2:2").PasteSpecial (xlPasteValues)
SOLUTION
Avatar of Cluskitt
Cluskitt
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of imaki06
imaki06

Have you tried to make the macro with recoder?

It would give you something like this:
Range("A1:B2").Select
    Selection.Copy
    Range("A4").Select
    ActiveSheet.Paste
Have you tried reading the question? He specifically said he wanted to copy/paste without selecting.
Avatar of Rory Archibald
You could also simply assign the .Value property from one range to the other.
Avatar of AndresHernando

ASKER

rorya,
what's the script for your suggestion?  What I wrote below doesn't work.
    Range("SourceRow").Value = Range("DestinationRow").Value

Thanks, --Andres
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If Source and Destination are variables, you need to:

Range(DestinationRow).Value = Range(SourceRow).Value
Cluskitt:
I tried your suggestion (see below), but it bugs out.  What am I doing wrong?

Sub CopyRows()
    Dim vSourceRow As Range
    Dim vRowNum As Long
   
    vRowNum = 9
    Set vSourceRow = Range(vRowNum & ":" & vRowNum)
    Range(vSourceRow).Copy '..............................................Bugs out here
    Range("2:2").PasteSpecial (xlPasteValues)

End Sub


Tnx, --Andres
vSourceRow is a Range, not an address, so:
vSourceRow.Copy
    Range("2:2").PasteSpecial xlPasteValues

Open in new window

Yes, it all depends on the the variables you declare. If you declare it as a range, you have to use it as a range. For example, if you declared vSourceRow as String, then assigned it to: "9:9", you could then use Range(vSourceRow). If it's a range, you have to use it as rorya said. If you use it as integer and assign it the value 9, you have to use: Range(vSourceRow & ":" & vSourceRow)
Thanks for the info.  Both worked.
To be fair, I split, giving the extra 5pts to Cluskitt for answering first.

I'm not sure if this is the correct protocol... I just recently started using this site.

Please tell me if I am in breach of proper etiquette by splitting pts like I did.
To my mind, you closed this properly - thank you! :)
I agree. Both answered the question using different approaches. Not that points are that important, but it's only fair to split things :)
Great.  Thanks again guys.  --Andres