PC Review


Reply
Thread Tools Rate Thread

Copy query, paste into Excel as data with VBA..

 
 
=?Utf-8?B?RGF2ZSBSYW1hZ2U=?=
Guest
Posts: n/a
 
      1st Aug 2007
Hi...

If I copy a query manually (by selecting it in the Database Window, then
Ctrl + C), I can then paste the results into Excel by simply pasting. Is
there a way of doing this from VBA- e.g.

Sub Test
Dim xlApp as Excel.Application
Dim wbTarget as Excel.Workbook

Set xlApp = New Excel.Application
Set wbTarget = xlApp.Workbooks.Add

CurrentDB.QueryDefs("qryTest").Copy 'no such method!
wbTarget.Sheets(1).Range("A1").Paste
End Sub

I could export the query as an Excel file, open it, then copy the worksheet
across...but a simple copy and paste would be easier if possible.

Thanks,
Dave

 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      1st Aug 2007
I believe the following should work:

Sub Test
Dim xlApp as Excel.Application
Dim wbTarget as Excel.Workbook
Dim rsCurr As DAO.Recordset

Set xlApp = New Excel.Application
Set wbTarget = xlApp.Workbooks.Add

Set rsCurr = CurrentDB.QueryDefs("qryTest").OpenRecordset
wbTarget.Sheets(1).Range("A1").CopyFromRecordset rsCurr

End Sub


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Dave Ramage" <(E-Mail Removed)> wrote in message
news:C8F0466C-F0B4-4039-85A7-(E-Mail Removed)...
> Hi...
>
> If I copy a query manually (by selecting it in the Database Window, then
> Ctrl + C), I can then paste the results into Excel by simply pasting. Is
> there a way of doing this from VBA- e.g.
>
> Sub Test
> Dim xlApp as Excel.Application
> Dim wbTarget as Excel.Workbook
>
> Set xlApp = New Excel.Application
> Set wbTarget = xlApp.Workbooks.Add
>
> CurrentDB.QueryDefs("qryTest").Copy 'no such method!
> wbTarget.Sheets(1).Range("A1").Paste
> End Sub
>
> I could export the query as an Excel file, open it, then copy the
> worksheet
> across...but a simple copy and paste would be easier if possible.
>
> Thanks,
> Dave
>



 
Reply With Quote
 
=?Utf-8?B?RGF2ZSBSYW1hZ2U=?=
Guest
Posts: n/a
 
      2nd Aug 2007
Thanks Douglas- works fine- even accepts an ADO recordset, which I happened
to have handy...

"Douglas J. Steele" wrote:

> I believe the following should work:
>
> Sub Test
> Dim xlApp as Excel.Application
> Dim wbTarget as Excel.Workbook
> Dim rsCurr As DAO.Recordset
>
> Set xlApp = New Excel.Application
> Set wbTarget = xlApp.Workbooks.Add
>
> Set rsCurr = CurrentDB.QueryDefs("qryTest").OpenRecordset
> wbTarget.Sheets(1).Range("A1").CopyFromRecordset rsCurr
>
> End Sub
>
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no e-mails, please!)
>
>
> "Dave Ramage" <(E-Mail Removed)> wrote in message
> news:C8F0466C-F0B4-4039-85A7-(E-Mail Removed)...
> > Hi...
> >
> > If I copy a query manually (by selecting it in the Database Window, then
> > Ctrl + C), I can then paste the results into Excel by simply pasting. Is
> > there a way of doing this from VBA- e.g.
> >
> > Sub Test
> > Dim xlApp as Excel.Application
> > Dim wbTarget as Excel.Workbook
> >
> > Set xlApp = New Excel.Application
> > Set wbTarget = xlApp.Workbooks.Add
> >
> > CurrentDB.QueryDefs("qryTest").Copy 'no such method!
> > wbTarget.Sheets(1).Range("A1").Paste
> > End Sub
> >
> > I could export the query as an Excel file, open it, then copy the
> > worksheet
> > across...but a simple copy and paste would be easier if possible.
> >
> > Thanks,
> > Dave
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How copy none excel data & paste in 2007 without overwriting data Wakefootin Microsoft Excel Misc 2 8th Oct 2009 01:15 AM
Delay copy & paste of data until web query refreshes? CM4@FL Microsoft Excel Programming 2 16th Apr 2009 07:00 PM
Excel cut/Paste Problem: Year changes after data is copy and paste =?Utf-8?B?QXNpZg==?= Microsoft Excel Misc 3 9th Dec 2005 06:16 PM
Copy & Paste Data from Web into Excel Kelvin Pakaree Microsoft Excel Misc 1 6th Aug 2004 03:07 PM
Copy & Paste Data from Web into Excel Kelvin Pakaree Microsoft Excel Misc 1 6th Aug 2004 02:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:34 PM.