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
Solved

VBA Access to Excel 2010 work 2016 not work

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
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.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

791 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