Solved

Excel - Copy and paste without selecting

Posted on 2015-01-10
8
131 Views
Last Modified: 2015-01-10
Hi,

The following piece of code works fine:
.Range(.Cells(firstRow,1) .Cells(lastRow,1)) = Sheets("Temp").Range("FB" & firstRow & ":FB" & lastRow).Value

When I try to make the columns dynamic (using R1C1 style) in the source sheet where aCol = 157

.Range(.Cells(firstRow,1) .Cells(lastRow,1)) = Sheets("Temp").Range(firstRow, aCol), cells(lastRow, aCol)).Value

I get an error "Application-defined or object defined error"?
0
Comment
Question by:adb11a
  • 5
  • 3
8 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
Comment Utility
Both statements give a syntax error

Try these instead

.Range(.Cells(firstRow, 1), .Cells(lastRow, 1)) = Sheets("Temp").Range("FB" & firstRow & ":FB" & lastRow).Value


.Range(.Cells(firstRow, 1), .Cells(lastRow, 1)) = Sheets("Temp").Range(Cells(firstRow, aCol), Cells(lastRow, aCol)).Value
0
 

Author Comment

by:adb11a
Comment Utility
Oops - I already had that - I accidently posted my question without the 'Cells'.
0
 

Author Comment

by:adb11a
Comment Utility
Oops - I already had that - I accidently posted my question without the 'Cells'.  

Any other ideas?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
Comment Utility
just before this line check the values of acol, firstrow, lastrow and make sure they are good.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:adb11a
Comment Utility
Yes, they are.

If I do the following it works...

Sheets("Temp").Select
Range(Cells(firstRow, aCol), Cells(lastRow, aCol)).Select

So the range is ok but the method doesn't work.  It does if the columns are alpha (eg FB)
0
 

Author Comment

by:adb11a
Comment Utility
It also works if I do the following:
aCol = "FB"
.Range(.Cells(firstRow, 1), .Cells(lastRow, 1)) = Sheets("Temp").Range(aCol & firstRow & ":" & aCol & lastRow).Value

So my get around will be to convert the column number (eg 157) to 'FB' and use this method unless you can assist....

Thanks for your help to date
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
Comment Utility
Try

.Range(.Cells(firstrow, 1), .Cells(lastrow, 1)).Value = Sheets("Temp").Range(Sheets("Temp").Cells(firstrow, acol), Sheets("Temp").Cells(lastrow, acol)).Value
0
 

Author Closing Comment

by:adb11a
Comment Utility
Thanks Saquib.

Quick, clear and concise expert solution provided.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

As freelancing is becoming more and more common in the tech industry, certain obstacles are proving to be a challenge to those who are used to more traditional, structured employment. This article is meant to help identify such obstacles and offer a…
Companies keep a much closer eye on costs today, so changing to new Technology – Microsoft Office 365 is the smartest move to take.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now