Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
WHERE [autonumberfieldname] \ 65356 = 0
WHERE [autonumberfieldname] \ 65356 = 1
Public Function ExportToExcel(QueryName As String, Optional ByVal WorkbookName As String = "", Optional RowsPerSheet As Long = 65356, Optional FirstRowHeadings As Boolean = True) As Boolean
[COLOR=green]' Export the results of a query to Excel, creating multiple worksheets
' if the number of rows exceeds the maximum number of rows per sheet (an optional variable).[/color]
Dim db As Database
Dim rst As Recordset
Dim appExcel As Excel.Application
Dim wkbExcel As Excel.Workbook
Dim wksExcel As Excel.Worksheet
Dim intFields As Integer
Dim intWorksheet As Integer
Dim intCol As Integer
Dim lngRow As Long
[COLOR=green]' Set error handling[/color]
ExportToExcel = False
On Error GoTo ExportToExcel_Err
[COLOR=green]' If no workbook name given, use the query name and put it in the current directory[/color]
If WorkbookName = "" Then WorkbookName = QueryName & ".xls"
[COLOR=green]' Create the Excel Application and Workbook[/color]
Set appExcel = Excel.Application
Set wkbExcel = appExcel.Workbooks.Add
[COLOR=green]' Initialize the worksheet to 1[/color]
intWorksheet = 1
[COLOR=green]' Open the query[/color]
Set db = CurrentDb()
Set rst = db.OpenRecordset(QueryName)
intFields = rst.Fields.Count
[COLOR=green]' Walk the query's output to build the workbook.
' Start by creating and formatting a new worksheet.
' Fill in worksheet rows from the query until you reach
' the end of the query or you reach the maximum number
' of rows in the worksheet.
' If you reach the maximum number of rows in the worksheet,
' create and format another worksheet and repeat the process.[/color]
While Not rst.EOF
On Error Resume Next
Set wksExcel = appExcel.Worksheets("Sheet" & intWorksheet)
If Err.Number = 9 Then
appExcel.Worksheets.Add.Move After:=appExcel.Worksheets(appExcel.Worksheets.Count)
Set wksExcel = appExcel.Worksheets("Sheet" & intWorksheet)
wksExcel.Visible = xlSheetVisible
End If
appExcel.Visible = True
[COLOR=green]' Initialize the worksheet font[/color]
With wksExcel
Cells.Font.Name = "Arial"
Cells.Font.Size = 10
Cells.Font.StrikeThrough = False
Cells.Font.Superscript = False
Cells.Font.Subscript = False
Cells.Font.OutlineFont = False
Cells.Font.Shadow = False
Cells.Font.Underline = xlUnderlineStyleNone
Cells.Font.ColorIndex = xlAutomatic
[COLOR=green]' If the first row will be the header, make the first row bold and freeze it[/color]
If FirstRowHeadings = True Then
.Rows("1:1").Font.Bold = True
.Rows("1:1").Interior.ColorIndex = 15
.Rows("1:1").Interior.Pattern = xlSolid
.Rows("1:1").Interior.PatternColorIndex = xlAutomatic
.Rows("2:2").Select
appExcel.ActiveWindow.FreezePanes = True
End If
[COLOR=green]' Also make the first row print on every page if the sheet is printed[/color]
If FirstRowHeadings = True Then
ActiveSheet.PageSetup.PrintTitleRows = "$1:$1"
ActiveSheet.PageSetup.PrintTitleColumns = ""
End If
[COLOR=green]' Also set up the header and footer to identify the data[/color]
.PageSetup.LeftHeader = QueryName [COLOR=green]' Name of query[/color]
.PageSetup.CenterHeader = ""
.PageSetup.RightHeader = ""
.PageSetup.LeftFooter = "&F" [COLOR=green]' File name[/color]
.PageSetup.CenterFooter = "&D" [COLOR=green]' Today's date[/color]
.PageSetup.RightFooter = "&P of &N" [COLOR=green]' Page x of y[/color]
.PageSetup.PrintHeadings = False
.PageSetup.PrintGridlines = False
.PageSetup.PrintComments = xlPrintNoComments
.PageSetup.PrintQuality = 600
.PageSetup.CenterHorizontally = False
.PageSetup.CenterVertically = False
.PageSetup.Orientation = xlPortrait
.PageSetup.Draft = False
.PageSetup.FirstPageNumber = xlAutomatic
.PageSetup.Order = xlDownThenOver
.PageSetup.BlackAndWhite = False
[COLOR=green]' Create column headers for each field in the query (if first row is to contain headings[/color]
lngRow = 1
If FirstRowHeadings = True Then
For intCol = 0 To intFields - 1
.Cells(1, intCol + 1).Value = rst.Fields(intCol).Name
Next intCol
lngRow = 2
End If
[COLOR=green]' Loop through the recordset adding rows to the worksheet[/color]
Do Until rst.EOF Or lngRow > RowsPerSheet
For intCol = 0 To intFields - 1
.Cells(lngRow, intCol + 1).Value = rst.Fields(intCol).Value
Next
lngRow = lngRow + 1
rst.MoveNext
Loop
[COLOR=green]' Resize the columns to fit the data[/color]
.Cells.Select
.Cells.EntireColumn.AutoFit
[COLOR=green]' Set the focus back at the first data cell[/color]
If FirstRowHeadings = True Then
.Range("A2").Select
Else
.Range("A1").Select
End If
[COLOR=green]' Make sure the worksheet is visible[/color]
wksExcel.Visible = xlSheetHidden
wksExcel.Visible = xlSheetVisible
End With
[COLOR=green]' Increment the worksheet number[/color]
Set wksExcel = Nothing
intWorksheet = intWorksheet + 1
Wend
[COLOR=green]' Save the file using the name provided[/color]
appExcel.ActiveWorkbook.SaveAs WorkbookName
[COLOR=green]' Cleanup Excel objects[/color]
Set wkbExcel = Nothing
Set appExcel = Nothing
[COLOR=green]' Close the recordset[/color]
rst.Close
Set db = Nothing
ExportToExcel = True
ExportToExcel_Exit:
Exit Function
ExportToExcel_Err:
MsgBox Err.Description
Resume ExportToExcel_Exit
End Function