Hey everyone -
As always, thanks in advance for any help or insight tek-tips and its community can provide me.
First the problem, then some background. When running the code below, I will get a _Global reference error every other time.
Background: One of the functions of my Access database is to export the results of a query to an Excel spreadsheet, and then format the Excel spreadsheet properly. I am using the DoCmd.TransferSpreadsheet command to accomplish this - no problems there.
The difficulty lies in (I believe):
1) Formatting the spreadsheet: I originally recorded macros in excel, and then dropped them into Access' VBA. As I said, the entire script runs perfectly fine the first time, but the second time I run it I receive an error at the very first "Range(...)" line.
2)Somehow having multiple copies of Excel open: despite using Excel.Application.Quit, Excel remains open after the script is run. Repeated running of the script results in repeated Excel instances being open.
* Code at end of post
I'd really appreciate any help or insight that is offered - I've spent the better part of two days trying to fix this problem, and am really just banging my head against the wall at this point.
Thanks in advance,
sbelyea
As always, thanks in advance for any help or insight tek-tips and its community can provide me.
First the problem, then some background. When running the code below, I will get a _Global reference error every other time.
Background: One of the functions of my Access database is to export the results of a query to an Excel spreadsheet, and then format the Excel spreadsheet properly. I am using the DoCmd.TransferSpreadsheet command to accomplish this - no problems there.
The difficulty lies in (I believe):
1) Formatting the spreadsheet: I originally recorded macros in excel, and then dropped them into Access' VBA. As I said, the entire script runs perfectly fine the first time, but the second time I run it I receive an error at the very first "Range(...)" line.
2)Somehow having multiple copies of Excel open: despite using Excel.Application.Quit, Excel remains open after the script is run. Repeated running of the script results in repeated Excel instances being open.
* Code at end of post
I'd really appreciate any help or insight that is offered - I've spent the better part of two days trying to fix this problem, and am really just banging my head against the wall at this point.
Thanks in advance,
sbelyea
Code:
Public Function Create23()
Dim objExcel As Excel.Application
Dim excelPath
Dim currentWorkSheet
Dim objSheet As Object
Dim objRange As Object
Dim stDocName As String
stDocName = "MyQuery"
DoCmd.OpenQuery stDocName, acNormal, acEdit
DoCmd.Close acQuery, "MyQuery", acSaveYes
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "MyQuery", "C:\MyQuery.xls", True
excelPath = "C:\MyQuery.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = 0
objExcel.Workbooks.Open excelPath, False, False
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
'objSheet.Name = "MyQuery"
objSheet.Cells(1, 3).Value = "MyQuery #"
objSheet.Rows(1).Font.Bold = True
' Moves data down a row
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("A2").Select
ActiveSheet.Paste
' Selects "######" column and paints it yellow
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Interior.ColorIndex = 6
' Selects "#######" column and paints it grey
Range("B2").Select
Selection.Interior.ColorIndex = 48
' Selects remaining column heads and paints them blue
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Interior.ColorIndex = 42
' Adds a cell called "Notes"
Range("K2").Select
ActiveCell.FormulaR1C1 = "Notes"
With ActiveCell.Characters(Start:=1, Length:=5).Font
.Name = "MS Sans Serif"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
' Removes the background color for cell L5 (Notes)
Range("K2").Select
Selection.Interior.ColorIndex = xlNone
' Puts a title above the data, adds current date
Range("A1").Select
ActiveCell.FormulaR1C1 = "MyQuery List"
Range("E1").Select
With Selection
.HorizontalAlignment = xlLeft
End With
ActiveCell.Value = Format(Now, "mmm dd, yyyy")
Selection.NumberFormat = "mmm dd, yyyy"
' Changes all the column widths
Columns("A:A").ColumnWidth = 4
Columns("B:B").ColumnWidth = 4
Columns("C:C").ColumnWidth = 6.5
Columns("D:D").ColumnWidth = 20
Columns("E:E").ColumnWidth = 13
Columns("F:F").ColumnWidth = 9
Columns("G:G").ColumnWidth = 8
Columns("H:H").ColumnWidth = 8
Columns("I:I").ColumnWidth = 10
Columns("J:J").ColumnWidth = 9.14
Columns("K:K").ColumnWidth = 23
' Sets all cells to Word Wrap
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Changes all of the header columns to be center aligned
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Changes data in H6:K9 to be center aligned
Range("G3:J3").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Makes the font Times New Roman
Cells.Select
With Selection.Font
.Name = "Times New Roman"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
' Creates borders around and within the data
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
' Change Print Layout to Landscape
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P"
.RightFooter = ""
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.PrintErrors = xlPrintErrorsDisplayed
End With
'Set objSheet = Nothing
'Set objRange = Nothing
'Set objExcel = Nothing
Excel.Application.ActiveWorkbook.SaveAs excelPath
End Function