Solved

What is the correct VBA PasteSpecial Format -  copy from Access to Excel?

Posted on 2011-02-14
3
2,862 Views
Last Modified: 2012-05-11
I have an database that was developed in Access 2003.

I export query results to a temp Excel file and copy the contents of the temp file to a pre-formatted Excel workbooks using VBA.

This code worked perfectly for several years using Access 2003.
...
xlObj.ActiveWorkbook.sheets("On Hold").select
xlObj.activesheet.range("A1").select
xlObj.activesheet.PasteSpecial Format:=2

I then upgraded to Office 2010 - however the PasteSpecial stopped working properly and did a straight paste and overwrote my formatting.
I then edited the PasteSpecial lines to:

xlObj.activesheet.PasteSpecial Paste:=xlPasteValues   (which i took from a recorded Excel macro)

However, the code now fails with a Run time 1004 error





0
Comment
Question by:radiantripack
3 Comments
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 34887428
try defining
Const xlPasteValues = -4163


or add to your references the Microsoft excel 14 object library
0
 

Author Comment

by:radiantripack
ID: 34969018
Hi Capricorn1

I add the references and now have a line as follows:
xlObj.activesheet.PasteSpecial Paste:=xlPasteValues

I'm still getting the Run Time error 1004
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 250 total points
ID: 35496003
In a global sense, most developers do not use copy/paste to transfer data between Access and Excel.

This is typically done by using variables and specific Access/Excel automation.

Roughly like this from Access:

Dim YourVariable as SomeDatatype
YourVariable =Forms!YourForm!YourContol
'Code to instansiate Excel
xlObj.ActiveWorkbook.sheets("On Hold").select
xlObj.activesheet.range("A1").value=YourVariable

Then simply apply the formatting via Excel automation.

Also note that some formatting in Access 2010 is not compatible with Excel, so this may be part of your issue.

JeffCoachman
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

706 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

19 Experts available now in Live!

Get 1:1 Help Now