Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA Access to Excel 2010 work 2016 not work

Posted on 2016-11-01
2
Medium Priority
?
133 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 35

Accepted Solution

by:
Norie earned 2000 total points
ID: 41869140
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
ID: 41869161
Simple. Effective. Quick. The error was not the error. It occurs above.
THANK YOU
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Suggested Courses

972 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