473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VBA Copy and PasteSpecial puts nothing in destination range but appears to select it

This is virtually identical to the actual code.

Sub Test()

Worksheets("Sheet1").Activate
Range(Cells(1, 1), Cells(10, 13)).Copy Destination:=Worksheets("Sheet2").Range("A2")
' and I've tried the alternative:
' Range(Cells(1, 1), Cells(10, 13)).Copy
' Sheets("Sheet2").Range("A2").PasteSpecial xlPasteValuesAndNumberFormats

End Sub

I've checked all the real basic stuff that I know to check: source is actually populated with data, pasted values are not in any way hidden, worksheet references checked and double-checked.

After successful execution, the destination range has no data in it, but it is selected in the sense it has a box around it and is gray. In the original code, source range is actually Range(Cells(1,1), Cells(iRowNum, 13)), where iRowNum is dimensioned LONG and has a valid value assigned. In that case, however, the destination range is not boxed with a solid line and the cells gray, but rather the range has your moving dashed-line border and the cells have no fill. But still no data.

I've tried quite a number of different approaches; if it runs at all, I get this unsatisfying result. I've tried activating the sheets in turn, etc., it makes no difference (nor would I expect it to).

My level of VBA experience is pre-beginner.
Aug 5 '10 #1
1 3755
Stewart Ross
2,545 Expert Mod 2GB
Hi. The copy sub works fine for me from within an Excel workbook with test data.

It is not an optimal way to do the copy as there is actually no need to select a worksheet before you copy a range, but regardless it does work just as it should.

Are you running it directly within Excel? This is important to know, as what works in Excel will not work when using Excel as an automation server without full qualification of the Excel instance involved.

The following does the copy without selecting the sheet first. It does not use the cells method, which is not suitable for use in the form listed without qualification as it is not referring to the same range:

Expand|Select|Wrap|Line Numbers
  1. Worksheets("Sheet1").Range("A1:M10").Copy Destination:=Worksheets("Sheet2").Range("A2")
If you need to use the Cells method you can ensure that the correct range is being referred to by using With and using the dot qualifier to specify that the cells method is within the Sheet1 object range:

Expand|Select|Wrap|Line Numbers
  1. With Worksheets("Sheet1")
  2.  .Range(.Cells(1, 1), .Cells(10, 13)).Copy Destination:=Worksheets("Sheet2").Range("A2")
  3. End With
All three of these - your original and the two listed above - work fine for me.

-Stewart
Aug 6 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Paolo Invernizzi | last post by:
Spambayes CVS, Python 2.2.3, FreeBSD 5.1 On my machine I'm lunching the pop3proxy with cron at startup, and all is fine. But... sometimes during startup, or after a one or two days, the...
14
by: dover | last post by:
/*Copy the line a token at a time into the output*/ copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(oss, " ")); What's the meaning of this statement?...
5
by: sam | last post by:
Hi, The following code produced a core-dump: PropertyParser::PropertyParser(list<HashMap> &p_list) { l_conf_data.clear(); cout << "beginning copy.. size: " << p_list.size() << endl;...
3
by: Alex | last post by:
I stumbled upon this while developing a custom XPathNavigator. It appears that copy action for attributes is broken in the .net framework XSLT processor. The intent was to just copy the entities...
4
by: Bill R via AccessMonster.com | last post by:
I get this Run-Time Error 1004 whenevery the following code runs: On Error GoTo XLSheet2 Set objXL = CreateObject("Excel.Application") With objXL Set objWkb = .Workbooks.Open(strPath) With...
1
by: adrian | last post by:
hello! i have a problem in pasting the content of a cell into another. i have tried it with the following code: excel = createobject ("excel.application") with excel .range...
0
by: shantanu | last post by:
I am trying to convert a macro code to c# that will copy the values of a column and paste to anather through paste special. Everything is working fine but the transpose meathod to paste the column...
0
by: iain654 | last post by:
Application.Run "Violations_VBA.xls!RefreshTable" Sheets("Total").Select Sheet1.Range("M5:P5").Copy Destination:=Range("A65536").End(xlUp).Offset(1, 0) I have the above VBA code to paste...
2
by: Lambda | last post by:
I'd like to change all the character in a array to lower case. I find transform() is convenient. Because the array is large, I don't want to define another array. I tried: transform(buffer,...
2
by: andre0170 | last post by:
Hi... well I'm stuck. I'm hoping that someone can help me identify where I'm going wrong with the macro code I'm using...I'm working on a spreadsheet that tracks purchases. When the word "Data" is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.