Solved

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

Posted on 2011-02-14
3
2,884 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

911 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

23 Experts available now in Live!

Get 1:1 Help Now