Solved

VBA Access to Excel 2010 work 2016 not work

Posted on 2016-11-01
2
25 Views
Last Modified: 2016-11-01
Windows 7 32 bit Originally MS Access 2010 on MS Access 2007 database. Upgraded(?) to 2016 and the part that copies Access tables to Excel gets an error Run-Time error 9 Subscript out of range. When the macro is run that calls the module, the file is created and opened (no data) and another generic Excel file is opened. Neither file can be closed normally and are killed with task manager. Entering debug mode does not get out of the error loop.  When I got in the code and said run, I was able to step over the error and it completed without further errors.

Error is occurs at XL.Visible = True.

Complete Code

Public Function ExpExcelll()
'Export Variance to Excel format

'COPY FRESH TEMPLATE WITH NO DATA
    Dim fso As Object
    Dim StrDate As String
    Dim StrSheetPath As String
    Dim dlastmon As Date
    dlastmon = DateAdd("m", -1, Now)
    StrDate = Format(dlastmon, "yyyymm")
    StrSheetPath = "R:\Month_End\TA_Variance\TA_VAR" & StrDate & ".xlsx"
    Set fso = VBA.CreateObject("Scripting.FileSystemObject")
    fso.CopyFile "R:\Month_End\TA_Variance\TA_Template.xlsx", StrSheetPath, True
 
'Creating the Recordset
 Dim cnn As ADODB.Connection
 Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String

'Variables to refer to Excel and Objects
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set cnn = CurrentProject.Connection
MyRecordset.ActiveConnection = cnn

'SQL statements to extract the data required for the report - From a query
MySQL = "SELECT Variance.* FROM Variance;"
MyRecordset.Open MySQL

'Open Excel and the workbook
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(StrSheetPath)

'Make sure excel is visible on the screen
Xl.Visible = True
' this is the problem with runtime error 9 subscript out of range
XlBook.Windows(1).Visible = True

'Define the sheet in the Workbook as XlSheet
Set XlSheet = XlBook.Worksheets("Variance")

'Insert the Recordset in the excel sheet starting at specified cell
XlSheet.Range("A2").CopyFromRecordset MyRecordset

'Clean up and end with worksheet visible on the screen
MyRecordset.Close

'SQL statements to extract the data required for the report - From a query into second tab in workbook
MySQL = "SELECT No_RuleID.* FROM No_RuleID;"
MyRecordset.Open MySQL

'Insert the Recordset in the excel sheet starting at specified cell
Set XlSheet = XlBook.Worksheets("No_RuleID")
XlSheet.Range("A2").CopyFromRecordset MyRecordset

'Clean up and end with worksheet visible on the screen
MyRecordset.Close

'SQL statements to extract the data required for the report - From a query into third tab in workbook
MySQL = "SELECT Zero_ExpPay.* FROM Zero_ExpPay;"
MyRecordset.Open MySQL

'Insert the Recordset in the excel sheet starting at specified cell
Set XlSheet = XlBook.Worksheets("Zero_ExpPay")
XlSheet.Range("A2").CopyFromRecordset MyRecordset

'Clean up and end with worksheet visible on the screen
MyRecordset.Close

'SQL statements to extract the data required for the report - From a query into fourth tab in workbook
MySQL = "SELECT Payor_99.* FROM Payor_99;"
MyRecordset.Open MySQL

'Insert the Recordset in the excel sheet starting at specified cell
Set XlSheet = XlBook.Worksheets("Payor_99")
XlSheet.Range("A2").CopyFromRecordset MyRecordset

'Clean up and end with worksheet visible on the screen
MyRecordset.Close

'SQL statements to extract the data required for the report - From a query into fifth tab in workbook
MySQL = "SELECT Zero_ExpPayGroup.* FROM Zero_ExpPayGroup;"
MyRecordset.Open MySQL

'Insert the Recordset in the excel sheet starting at specified cell
Set XlSheet = XlBook.Worksheets("Zero_ExpPayGroup")
XlSheet.Range("A2").CopyFromRecordset MyRecordset

'Clean up and end with worksheet visible on the screen
MyRecordset.Close


'SQL statements to extract the data required for the report - From a query into fifth tab in workbook
MySQL = "SELECT Totals.* FROM Totals;"
MyRecordset.Open MySQL

'Insert the Recordset in the excel sheet starting at specified cell
Set XlSheet = XlBook.Worksheets("Totals")
XlSheet.Range("A2").CopyFromRecordset MyRecordset

'Clean up and end with worksheet visible on the screen
'    fso.CopyFile "C:\TDown\TA.accdb", StrSheetPath, True

MyRecordset.Close


'Save and close
'XlBook.ActiveWorkbook.Save
'XlBook.ActiveWorkbook.Close
'Xl.Quit

    Set fso = Nothing


Set cnn = Nothing
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing

End Function



MACRO
Run Code function name ExpExcelll()
0
Comment
Question by:avgplusguy
2 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
What happens if you change this,
Set XlBook = GetObject(StrSheetPath)

Open in new window

to this.
Set XlBook = Xl.Workbooks.Open(StrSheetPath)

Open in new window

0
 

Author Closing Comment

by:avgplusguy
Comment Utility
Simple. Effective. Quick. The error was not the error. It occurs above.
THANK YOU
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now