Solved

VBA Access to Excel 2010 work 2016 not work

Posted on 2016-11-01
2
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
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…

749 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