Error formatting Excel spreadsheet from VBA

Posted on 2009-07-15
Last Modified: 2012-05-07
Hi!  I have a query in MSAccess that I am using TransferSpreadsheet to export to Excel.  I have a module to format the Excel file once it is exported.  Sometimes the file is exported and formatted correctly and other times I receive an error:  Subscript Out of Range.  
I attached the code from the module but it seems that the error is coming from Excel.  Is it possible that the data being exported is causing the problem?  It seems to happen more often when replacing an existing file with the same name rather than generating a new file.  
Could this be related to an existing RESUME.xlw file when trying to export another file?  

Sub ModifyExcelFile(vRptName As String, vHeader As String, vWorksheet As String, vTabName As String)

On Error GoTo Err_ModifyExcelFile

    Dim excelapp As Object

    Dim excelwb As Workbook

    Dim excelws As Worksheet

    'Open the XLS file with the Workbook from Application, not directly from Application.

    Set excelapp = New Excel.Application

    Set excelwb = excelapp.Workbooks.Open(vRptName)

    Set excelws = excelwb.Worksheets(vWorksheet)



            excelws.Application.Selection.Font.Bold = True



            excelws.PageSetup.Orientation = xlLandscape

            excelws.PageSetup.CenterHeader = vHeader

            excelws.PageSetup.LeftHeader = "Generated on: " & Now()

            excelws.PageSetup.CenterFooter = "Page &P"

            excelws.Name = vTabName


            excelws.Application.Selection.Font.Name = "Tahoma"

            excelws.Application.Selection.Font.Size = 10



    'Save the file so the prompt asking to save the file will not pop up



    'Need to quit the application or the file will be kept opened in memory



    'Freeing the objects

    Set excelws = Nothing

    Set excelwb = Nothing

    Set excelapp = Nothing


   MsgBox "Report exported to " & vRptName



    Exit Sub


    MsgBox Err.Description


    'Need to quit the application or the file will be kept opened in memory



    'Freeing the objects

    Set excelws = Nothing

    Set excelwb = Nothing

    Set excelapp = Nothing

    Resume Exit_ModifyExcelFile


End Sub

Open in new window

Question by:jkoneil
  • 3
LVL 13

Accepted Solution

Brian Withun earned 500 total points
ID: 24862385
My theory is that this line is causing it:

Set excelws = excelwb.Worksheets(vWorksheet)

There are very few statements which involve a "subscript".
It could be that vWorksheet has a value which does not correspond to an actual sheet name in Excel.
Step through this procedure in the debugger to confirm.
LVL 119

Expert Comment

by:Rey Obrero
ID: 24862529
first open the the excel file you used for the variable (vRptName)
then, look for the name of the sheet that you use for the variable (vWorksheet) if it exist in the Excel file.

Author Comment

ID: 24862803
I stepped through the procedure in MS Access and the file was exported successfully 2 times and one time it issued the error and it was on the Set excelws = excelwb.Worksheets(vWorksheet) line.    When I open the export that had the error, everything looks correct.


Author Comment

ID: 24896454
I tried replacing Set excelws = excelwb.Worksheets(vWorksheet) with Set excelws = excelwb.Worksheets(1) and that seems to have fixed the problem.

Author Closing Comment

ID: 31603884
Thanks for pointing me in the right direction.  I couldn't figure out what was causing the error.  I thought it might be the data that was exported.

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

759 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

21 Experts available now in Live!

Get 1:1 Help Now