Solved

VBA Access to Excel 2010 work 2016 not work

Posted on 2016-11-01
2
41 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
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

777 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