Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2012, 09:28 AM
Registered User
 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exporting queries to multiple sheets in one workbook

Hello

I'm fairly new to VBA but doing my best to learn - I'm building a reporting system using access 2003 and have got a bit stuck when exporting some queries to excel.

I have four queries I want to export to four separate tabs within one excel workbook, then bold the headers on each tab.

I found a solution for this that works on only one query to one sheet but I don't know how to modify it to work on the other queries.

I've tried finding a solution on the web but can't find anything suitable, any help will be greatly appricited.
 
Old January 5th, 2012, 09:38 AM
JP JP is offline
Authorized User
 
Join Date: Apr 2008
Posts: 57
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Quote:
I found a solution for this that works on only one query to one sheet but I don't know how to modify it to work on the other queries.
Can you post it? Maybe someone can help you modify it.
__________________
Regards,
JP
JP SoftTech
 
Old January 5th, 2012, 09:47 AM
Registered User
 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
Option Compare Database
Option Explicit
 
Sub ExportReport24Changes()
     '   workbook's name
    Const szWorkbook As String = "\Report 24 2.xls"
        
     '   Access query where the info comes from
    Const szQueryName As String = "report 24 changes"
     
     '   Workbook is created in this path
    Dim szFullPath As String
    szFullPath = "S:\HR\Performance Team\HR Programme Office\MI\1. Weekly Reports\Report 24\Temporary Files" & szWorkbook
     
    On Error GoTo ErrHandle
    With Application
        .Echo False
         
         '       Output the query to our Excel book
        DoCmd.OutputTo acOutputQuery, szQueryName, acFormatXLS, szFullPath, False
         
         '       Late binding to avoid reference:
        Dim xlApp As Object 'Excel.Application
        Dim xlwb As Object 'Workbook
        Dim xlws As Object 'Worksheet
         
         '       Create the instance of Excel that we will use to open the workbook
        Set xlApp = CreateObject("Excel.Application")
        Set xlwb = xlApp.Workbooks.Open(szFullPath)
        Set xlws = xlwb.Worksheets(szQueryName)
         

         '       Format our report
        xlws.Range("A1:AH1").Font.Bold = True
        xlws.Range("A1:AH1").Font.ColorIndex = 1
        xlws.Range("A1:AH1").Interior.ColorIndex = 2

        '       AutoFit the columns
        xlws.Range("A:AH").Columns.AutoFit

        
         '       Select to make sure the sheet has focus
        xlws.Range("A1").Select
         
         '       Close the file we created
        xlwb.Close True
         
ErrorExit:
         '       Explicitly Clear Memory
        Set xlws = Nothing
        Set xlwb = Nothing
        Set xlApp = Nothing
         
        .Echo True
    End With
    Exit Sub
     
ErrHandle:
    MsgBox Err.Description
    GoTo ErrorExit
    
End Sub
 
Old January 5th, 2012, 09:50 AM
Registered User
 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found that code from the following site, which i changed slightly to fit my project

http://www.vbaexpress.com/kb/getarticle.php?kb_id=662

Thanks
 
Old January 8th, 2012, 09:13 AM
Registered User
 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone have any ideas?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching Multiple Sheets for Data? Bill_L Excel VBA 2 June 20th, 2007 09:14 AM
Exporting Table to Excel Sheets ayazhoda Access VBA 11 April 5th, 2007 07:25 AM
Exporting to Excel - one sheet for many queries dearnne Access VBA 1 July 19th, 2004 09:39 AM
Loading from multiple sheets on another workbook luxcs Excel VBA 0 March 25th, 2004 03:00 PM
exporting a workbook from excel to access zisko3 Access 1 February 3rd, 2004 12:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.