Export to Excel

  • Thread starter Thread starter learning_codes
  • Start date Start date
L

learning_codes

Hi,

I tried to use the template that has 20 worksheets with own differnent
heading. I have 17 Queries that were set by order 1, 2, 3, 4, etc.
When I click a button and i want each queries slot into each
worksheet#1, #2, #3 with no change on heading.

Query#1 = Worksheet #1
Query#2 = Worksheet #2
.....
....
Query#17 - Worksheet #17

I want to export the data to A5 (cell). A1 to A4 is for heading
only. The template has all worksheets with heading (A1-A4).

Your help would be much appreciated.
Thanks
 
I assume your code creates a copy of the template and exports each query to
the specified worksheet?
Do you have code that does the above?
If you post the relevant part of the code, we may be able to suggest how to
modify it.

Jeanette Cunningham
 
I assume your code creates a copy of the template and exports each query to
the specified worksheet?
Do you have code that does the above?
If you post the relevant part of the code, we may be able to suggest how to
modify it.

Jeanette Cunningham










- Show quoted text -

Using this, 17 Worksheets Template in one excel spreadsheet plus 17
query Data = 34 Tabs.

I want to get the data into 17 worksheets that are matched from query
name #1=#17 to worksheet sheet #1-#17.

Your help would be much appreciated.

*************************************
On Error GoTo HandleError

Dim objXLApp As Object
Set objXLApp = CreateObject("Excel.Application")
Dim objXLBook As Excel.Workbook

Dim db As DAO.Database

Set db = CurrentDb

Path = GetPath(db.Name)

Kill conPath & "Report_" & Me.Text4.Value & "_" & Me.Text2.Value &
"_" & Me.Text6.Value & ".xls"

Set objXLApp = New Excel.Application
Set objXLBook = objXLApp.Workbooks.Open(conPath & "Report
Template.xlt")
'objXLApp.Visible = True

objXLBook.SaveAs (Path & "Report_" & Me.Text4.Value & "_" &
Me.Text2.Value & "_" & Me.Text6.Value & ".xls")
objXLBook.Close

'Worksheet#1
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"Wrksheet#1", Path & "Report_" & Me.Text4.Value & "_" & Me.Text2.Value
& "_" & Me.Text6.Value & ".xls", True, ""

'Worksheet#2
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "
Wrksheet#2", Path & "Report_" & Me.Text4.Value & "_" & Me.Text2.Value
& "_" & Me.Text6.Value & ".xls", True, ""

'Worksheet#3
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "
Wrksheet#3", Path & "Report_" & Me.Text4.Value & "_" & Me.Text2.Value
& "_" & Me.Text6.Value & ".xls", True, ""

'Worksheet#4
'Worksheet#5
'.....
'.....
'Worksheet#17

ProcDone:
On Error Resume Next

Set qdf = Nothing
Set db = Nothing
Set rs = Nothing
Set objResultsSheet = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing

****************************************
 
This project needs to use that open each worksheet and tells excel where to
post the data.
You can do this using an excel method called CopyFromRecordset

The code looks long and complicated, but when you understand it, its not too
bad.
 
Sorry, clicked send accidentally before I had finished the previous post.

Here is an example using excel's copy from recordset method.
This is an example for one worksheet.

When I get time I will have a look at extending the code to multiple
spreadsheets in the same workbook.

Dim objXLApp As Object
Dim objXLws As Object
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strDocPath 'full path and name of template
Dim strPath As String

Set db = DBEngine(0)(0)
' Open a recordset on the query for the data to export
Set rst = db.OpenRecordset("your query")
' If there are no records, return an error and exit function
If rst.EOF Then
msgbox "error, no data"
Exit Function
End If


'replace with names and cell references that suit your template

' Populate the excel object
Set objXLApp = CreateObject("Excel.Application")
' Open the template workbook
objXLApp.Workbooks.Open (strDocPath)
' Save the template as the file specified by the user
objXLApp.ActiveWorkbook.SaveAs (strPath)
' Select the appropriate worksheet
Set objXLws = objXLApp.ActiveWorkbook.Worksheets("TheWsName")
' Activate the selected worksheet
objXLws.Activate
' Ask Excel to copy the data from the recordset starting with cell A5
objXLws.Range("A5").CopyFromRecordset rst
' Select the main worksheet
objXLApp.Worksheets("TheWsName").Activate
' Activate the selected worksheet
Set objXLws = objXLApp.ActiveWorkbook.Worksheets("TheWsName")


' Hide warnings on the spreadsheet
objXLApp.DisplayAlerts = False
' Save the workbook
objXLApp.ActiveWorkbook.Save
' Turn spreadsheet warnings back on
objXLApp.DisplayAlerts = True
' Make it visible
objXLApp.Visible = True

'**error handling, in the function exit - make sure you set the object
'references to nothing as shown below.

FunctionExit:

Set objXLws = Nothing
Set objXLApp = Nothing
' Destroy the recordset and database objects
rst.Close
Set rst = Nothing
Set db = Nothing

Exit Function

Jeanette Cunningham
 
Sorry, clicked send accidentally before I had finished the previous post.

Here is an example using excel's copy from recordset method.
This is an example for one worksheet.

When I get time I will have a look at extending the code to multiple
spreadsheets in the same workbook.

Dim objXLApp As Object
Dim objXLws As Object
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strDocPath  'full path and name of template
Dim strPath As String

    Set db = DBEngine(0)(0)
    ' Open a recordset on the query for the data to export
    Set rst = db.OpenRecordset("your query")
    ' If there are no records, return an error and exit function
    If rst.EOF Then
        msgbox "error, no data"
         Exit Function
    End If

'replace with names and cell references that suit your template

    ' Populate the excel object
    Set objXLApp = CreateObject("Excel.Application")
    ' Open the template workbook
    objXLApp.Workbooks.Open (strDocPath)
    ' Save the template as the file specified by the user
    objXLApp.ActiveWorkbook.SaveAs (strPath)
    ' Select the appropriate worksheet
    Set objXLws = objXLApp.ActiveWorkbook.Worksheets("TheWsName")
    ' Activate the selected worksheet
    objXLws.Activate
    ' Ask Excel to copy the data from the recordset starting with cellA5
    objXLws.Range("A5").CopyFromRecordset rst
    ' Select the main worksheet
    objXLApp.Worksheets("TheWsName").Activate
    ' Activate the selected worksheet
    Set objXLws = objXLApp.ActiveWorkbook.Worksheets("TheWsName")

    ' Hide warnings on the spreadsheet
    objXLApp.DisplayAlerts = False
    ' Save the workbook
    objXLApp.ActiveWorkbook.Save
    ' Turn spreadsheet warnings back on
    objXLApp.DisplayAlerts = True
    ' Make it visible
    objXLApp.Visible = True

'**error handling, in the function exit  - make sure you set the object
'references to nothing as shown below.

FunctionExit:

    Set objXLws = Nothing
    Set objXLApp = Nothing
    ' Destroy the recordset and database objects
    rst.Close
    Set rst = Nothing
    Set db = Nothing

    Exit Function

Jeanette Cunningham

Hi Jeanette,

Thank you in million times. It works as beautiful.

I try to learn how to add 2nd worksheet related "My Query #2" but I
couldn't. Your help would be much appreciated if you provide extra
sample of code to add 2nd worksheet related "My Query #2"
 
Try this
Make a table (tblExport) to store the names of the queries to export with
the worksheet names
Create a table with 2 fields
field 1 -- QueryN -- text 'for name of each query to export
field 2 -- WorksheetN -- text 'for the corresponding worksheet name
save the table as tblExport
Add your query names and worksheet names to tblExport

I haven't tested this code extensively, but it worked for me.

-----------------------------------
Private Function MyExportMulti()
On Error GoTo FunctionErr
Dim objXLApp As Object
Dim objXLws As Object
Dim db As DAO.Database
Dim rstCopy As DAO.Recordset
Dim rstMain As DAO.Recordset
Dim strDocPath 'full path and name of template
Dim strSavePath As String 'full path and name to save file as
Dim strWsName As String 'name of worksheet
Dim strQueryName As String

strDocPath = "your template file name and path here"
strPath = "your save file name and path here"

'replace with names and cell references that suit your setup

'Populate the excel object
Set objXLApp = CreateObject("Excel.Application")
'Open the template workbook
objXLApp.Workbooks.Open (strDocPath)
'Save the template as the file specified by the user
objXLApp.activeworkbook.SaveAs (strSavePath)

Set db = DBEngine(0)(0)
'Open a recordset on the table with query and worksheet names
Set rstMain = db.OpenRecordset("tblExport")
'make sure at start of table
rstMain.MoveFirst
'Use the recordset as a base
With rstMain
' Process until end of file
Do While Not .EOF

'get the name of the query
strQueryName = rstMain("QueryN")
'get the name of the worksheet
strWsName = rstMain("WorksheetN")
' Open a recordset on the query for the data to export
Set rstCopy = db.OpenRecordset(strQueryName)
' If there are no records, return an error
If rstCopy.EOF Then
MsgBox "error, no data"
Else
' Select the appropriate worksheet
Set objXLws = objXLApp.activeworkbook.Worksheets(strWsName)
' Activate the selected worksheet
objXLws.Activate
' Ask Excel to copy the data from the recordset starting
with cell A5
objXLws.Range("A5").CopyFromRecordset rstCopy
' Select the main worksheet
objXLApp.Worksheets(strWsName).Activate
' Activate the selected worksheet
Set objXLws = objXLApp.activeworkbook.Worksheets(strWsName)
'close the 1st recordset
rstCopy.Close
Set rstCopy = Nothing
End If

rstMain.MoveNext
Loop
End With


'**error handling, in the function exit - make sure you set the object
'references to nothing as shown below.

FunctionExit:
'Hide warnings on the spreadsheet
objXLApp.DisplayAlerts = False
'Save the workbook
objXLApp.activeworkbook.Save
'Turn spreadsheet warnings back on
objXLApp.DisplayAlerts = True
'Make it visible
objXLApp.Visible = True

Set objXLws = Nothing
Set objXLApp = Nothing
'Destroy the recordset and database objects
rstMain.Close
If Not rstCopy Is Nothing Then
Set rstCopy = Nothing
End If
If Not rstMain Is Nothing Then
Set rstMain = Nothing
End If
If Not db Is Nothing Then
Set db = Nothing
End If

Exit Function

FunctionErr:
MsgBox Err.Description & " " & Err.Number
Resume FunctionExit
End Function
---------------------------


Jeanette Cunningham

Sorry, clicked send accidentally before I had finished the previous post.

Here is an example using excel's copy from recordset method.
This is an example for one worksheet.

When I get time I will have a look at extending the code to multiple
spreadsheets in the same workbook.

Dim objXLApp As Object
Dim objXLws As Object
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strDocPath 'full path and name of template
Dim strPath As String

Set db = DBEngine(0)(0)
' Open a recordset on the query for the data to export
Set rst = db.OpenRecordset("your query")
' If there are no records, return an error and exit function
If rst.EOF Then
msgbox "error, no data"
Exit Function
End If

'replace with names and cell references that suit your template

' Populate the excel object
Set objXLApp = CreateObject("Excel.Application")
' Open the template workbook
objXLApp.Workbooks.Open (strDocPath)
' Save the template as the file specified by the user
objXLApp.ActiveWorkbook.SaveAs (strPath)
' Select the appropriate worksheet
Set objXLws = objXLApp.ActiveWorkbook.Worksheets("TheWsName")
' Activate the selected worksheet
objXLws.Activate
' Ask Excel to copy the data from the recordset starting with cell A5
objXLws.Range("A5").CopyFromRecordset rst
' Select the main worksheet
objXLApp.Worksheets("TheWsName").Activate
' Activate the selected worksheet
Set objXLws = objXLApp.ActiveWorkbook.Worksheets("TheWsName")

' Hide warnings on the spreadsheet
objXLApp.DisplayAlerts = False
' Save the workbook
objXLApp.ActiveWorkbook.Save
' Turn spreadsheet warnings back on
objXLApp.DisplayAlerts = True
' Make it visible
objXLApp.Visible = True

'**error handling, in the function exit - make sure you set the object
'references to nothing as shown below.

FunctionExit:

Set objXLws = Nothing
Set objXLApp = Nothing
' Destroy the recordset and database objects
rst.Close
Set rst = Nothing
Set db = Nothing

Exit Function

Jeanette Cunningham

Hi Jeanette,

Thank you in million times. It works as beautiful.

I try to learn how to add 2nd worksheet related "My Query #2" but I
couldn't. Your help would be much appreciated if you provide extra
sample of code to add 2nd worksheet related "My Query #2"
 
Try this
Make a table (tblExport) to store the names of the queries to export with
the worksheet names
Create a table with 2 fields
field 1 -- QueryN -- text  'for name of each query to export
field 2 --  WorksheetN -- text  'for the corresponding worksheet name
save the table as tblExport
Add your query names and worksheet names to tblExport

I haven't tested this code extensively, but it worked for me.

-----------------------------------
Private Function MyExportMulti()
On Error GoTo FunctionErr
Dim objXLApp As Object
Dim objXLws As Object
Dim db As DAO.Database
Dim rstCopy As DAO.Recordset
Dim rstMain As DAO.Recordset
Dim strDocPath  'full path and name of template
Dim strSavePath As String  'full path and name to save file as
Dim strWsName As String  'name of worksheet
Dim strQueryName As String

strDocPath = "your template file name and path here"
strPath = "your save file name and path here"

'replace with names and cell references that suit your setup

    'Populate the excel object
    Set objXLApp = CreateObject("Excel.Application")
    'Open the template workbook
    objXLApp.Workbooks.Open (strDocPath)
    'Save the template as the file specified by the user
    objXLApp.activeworkbook.SaveAs (strSavePath)

    Set db = DBEngine(0)(0)
    'Open a recordset on the table with query and worksheet names
    Set rstMain = db.OpenRecordset("tblExport")
    'make sure at start of table
    rstMain.MoveFirst
    'Use the recordset as a base
    With rstMain
        ' Process until end of file
        Do While Not .EOF

            'get the name of the query
            strQueryName = rstMain("QueryN")
            'get the name of the worksheet
            strWsName = rstMain("WorksheetN")
            ' Open a recordset on the query for the data to export
            Set rstCopy = db.OpenRecordset(strQueryName)
            ' If there are no records, return an error
            If rstCopy.EOF Then
                MsgBox "error, no data"
            Else
                ' Select the appropriate worksheet
                Set objXLws = objXLApp.activeworkbook.Worksheets(strWsName)
                ' Activate the selected worksheet
                objXLws.Activate
                ' Ask Excel to copy the data from the recordset starting
with cell A5
                objXLws.Range("A5").CopyFromRecordset rstCopy
                ' Select the main worksheet
                objXLApp.Worksheets(strWsName).Activate
                ' Activate the selected worksheet
                Set objXLws = objXLApp.activeworkbook.Worksheets(strWsName)
                'close the 1st recordset
                rstCopy.Close
                Set rstCopy = Nothing
            End If

          rstMain.MoveNext
        Loop
    End With

'**error handling, in the function exit  - make sure you set the object
'references to nothing as shown below.

FunctionExit:
    'Hide warnings on the spreadsheet
    objXLApp.DisplayAlerts = False
    'Save the workbook
    objXLApp.activeworkbook.Save
    'Turn spreadsheet warnings back on
    objXLApp.DisplayAlerts = True
    'Make it visible
    objXLApp.Visible = True

    Set objXLws = Nothing
    Set objXLApp = Nothing
    'Destroy the recordset and database objects
    rstMain.Close
    If Not rstCopy Is Nothing Then
        Set rstCopy = Nothing
    End If
    If Not rstMain Is Nothing Then
        Set rstMain = Nothing
    End If
    If Not db Is Nothing Then
        Set db = Nothing
    End If

    Exit Function

FunctionErr:
    MsgBox Err.Description & " " & Err.Number
    Resume FunctionExit
End Function
---------------------------

Jeanette Cunningham














Hi Jeanette,

Thank you in million times.   It works as beautiful.

I try to learn how to add 2nd worksheet related "My Query #2" but I
couldn't.   Your help would be much appreciated if you provide extra
sample of code to add 2nd worksheet related "My Query #2"- Hide quoted text -

- Show quoted text -

Thank you so much and it works beautiful . I am really appreciated
your help.

I have two things to ask:
1) Will it be possible to put A5 as "No report" instead of MsgBox
2) Is there a way to make Ariel Narrow 9 on the excel spreadsheet from
Access.

Thanks again in billion times.
 
Thank you so much and it works beautiful . I am really appreciated
your help.

I have two things to ask:
1) Will it be possible to put A5 as "No report" instead of MsgBox
2) Is there a way to make Ariel Narrow 9 on the excel spreadsheet from
Access.

Thanks again in billion times.


To put 'No report' in cell A5
Open your template workbook and in cell A5 in each workbook type the words
No report
The words 'No report' will be overwritten with data for each worksheet that
has data.
For any worksheet without data, the text 'No report' will show.

I have amended the code to format the cells as you asked and a few extra
format things you may find useful.


Private Function MyExportMulti()
On Error GoTo FunctionErr
Dim objXLApp As Object
Dim objXLws As Object
Dim db As DAO.Database
Dim rstCopy As DAO.Recordset
Dim rstMain As DAO.Recordset
Dim strDocPath 'full path and name of template
Dim strPath As String 'full path and name to save file as
Dim strWsName As String 'name of worksheet
Dim strQueryName As String
Dim strFirstCell As String
Dim strRange As String

Const xlCellTypeLastCell = 11
Const xlContinuous = 1
Const xlAutomatic = -4105


strDocPath = "c:\documents and
settings\jc.ECJ-02.000\desktop\MyPersonxpt.xls"
strPath = "c:\documents and
settings\jc.ECJ-02.000\desktop\MyNewPersonxpt.xls"
strFirstCell = "A5"

'replace with names and cell references that suit your template

' Populate the excel object
Set objXLApp = CreateObject("Excel.Application")
' Open the template workbook
objXLApp.Workbooks.Open (strDocPath)
' Save the template as the file specified by the user
objXLApp.activeworkbook.SaveAs (strPath)

Set db = DBEngine(0)(0)
'Open a recordset on the table with query and worksheet names
Set rstMain = db.OpenRecordset("tblExport")
'make sure at start of table
rstMain.MoveFirst
' Use the recordset as a base
With rstMain
' Process until end of file
Do While Not .EOF

'get the name of the query
strQueryName = rstMain("QueryN")
'get the name of the worksheet
strWsName = rstMain("WorksheetN")
' Open a recordset on the query for the data to export
Set rstCopy = db.OpenRecordset(strQueryName)
' If there are no records, return an error and exit function
If rstCopy.EOF Then
'handle error here
Else
' Select the appropriate worksheet
Set objXLws = objXLApp.activeworkbook.Worksheets(strWsName)
' Activate the selected worksheet
objXLws.Activate
' Ask Excel to copy the data from the recordset starting
with cell A5
objXLws.Range("A5").CopyFromRecordset rstCopy
'close the 1st recordset
rstCopy.Close
Set rstCopy = Nothing

' Select the main worksheet
objXLApp.Worksheets(strWsName).Activate
' Activate the selected worksheet
Set objXLws = objXLApp.activeworkbook.Worksheets(strWsName)
'format cells
With objXLws.Cells
.Range(.Cells(1, 1), .Cells(1,
1).SpecialCells(xlCellTypeLastCell)).Borders.LineStyle = xlContinuous
.Range(.Cells(1, 1), .Cells(1,
1).SpecialCells(xlCellTypeLastCell)).Borders.ColorIndex = xlAutomatic
.Font.Size = 9
.Font.Name = "Arial Narrow"
.WrapText = True

End With

End If

rstMain.MoveNext
Loop
End With


'**error handling, in the function exit - make sure you set the object
'references to nothing as shown below.

FunctionExit:
' Hide warnings on the spreadsheet
objXLApp.DisplayAlerts = False
' Save the workbook
objXLApp.activeworkbook.Save
' Turn spreadsheet warnings back on
objXLApp.DisplayAlerts = True
' Make it visible
objXLApp.Visible = True

Set objXLws = Nothing
Set objXLApp = Nothing
' Destroy the recordset and database objects
rstMain.Close
If Not rstCopy Is Nothing Then
Set rstCopy = Nothing
End If
If Not rstMain Is Nothing Then
Set rstMain = Nothing
End If
If Not db Is Nothing Then
Set db = Nothing
End If

Exit Function

FunctionErr:
MsgBox Err.Description & " " & Err.Number
Resume FunctionExit
End Function


Jeanette Cunningham
 
Back
Top