Solved

Excel VBA - Copy / Paste without using "selection"

Posted on 2011-02-21
14
1,157 Views
Last Modified: 2012-05-11
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)
0
Comment
Question by:AndresHernando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 18

Assisted Solution

by:Cluskitt
Cluskitt earned 65 total points
ID: 34942035
Assuming vSourceRow is a full row:

Range(vSourceRow).Copy
Range("2:2").PasteSpecial (xlPasteValues)
0
 
LVL 3

Expert Comment

by:imaki06
ID: 34942037
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
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34942055
Have you tried reading the question? He specifically said he wanted to copy/paste without selecting.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34942130
You could also simply assign the .Value property from one range to the other.
0
 

Author Comment

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

Thanks, --Andres
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 60 total points
ID: 34942206
Other way round:
Range("DestinationRow").Value = Range("SourceRow").Value

Open in new window

as you are assigning the source values to the destination. Note that the ranges must be the same size.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34942227
If Source and Destination are variables, you need to:

Range(DestinationRow).Value = Range(SourceRow).Value
0
 

Author Comment

by:AndresHernando
ID: 34942272
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
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34942282
vSourceRow is a Range, not an address, so:
vSourceRow.Copy
    Range("2:2").PasteSpecial xlPasteValues

Open in new window

0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34942305
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)
0
 

Author Closing Comment

by:AndresHernando
ID: 34942337
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.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34942357
To my mind, you closed this properly - thank you! :)
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34942521
I agree. Both answered the question using different approaches. Not that points are that important, but it's only fair to split things :)
0
 

Author Comment

by:AndresHernando
ID: 34942706
Great.  Thanks again guys.  --Andres
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question