Results 1 to 2 of 2
  1. #1
    msche398 is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2011
    Posts
    1

    Subscript out of range (9)

    I have an Access 2007 database that, when a certain button is clicked, opens excel, and inserts worksheets and data. It works for the most part except for one function. When I run this function, I get the error "Subscript out of range (9)" and it stops. Below is the code.


    Function ExportExcel_lot()
    Dim xlworkbook As Excel.Workbook
    Dim currpath As String
    DoCmd.SetWarnings False
    Set xlapp = CreateObject("Excel.Application")
    xlapp.Visible = True
    Set xlworkbook = xlapp.Workbooks.Add

    Dim r1 As ADODB.Recordset
    Set r1 = New ADODB.Recordset
    r1.CursorLocation = adUseClient
    xlapp.Sheets.Add
    With xlworkbook
    .Worksheets(1).Name = "Doff"
    .Worksheets(2).Name = "Creel"
    .Worksheets(3).Name = "Sizing"


    .Worksheets(4).Name = "Packing"
    End With
    DoCmd.SetWarnings True
    End Function

    The point where it stops is the .Worksheets(4).Name = "Packing". If I take out this line, the entire function works fine and I have an Excel 2003 format workbook (but using Excel 2007), with 3 worksheets, complete with data and formatting. But if I try to create a 4th worksheet, I get the error again and I only have 3 worksheets.

    Does anyone have any ideas why it is doing this? I don't understand why I could have 3 worksheets but not 4. I really appreciate the help.

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    You can't assume that there will be enough worksheets in the Excel workbook when you add one. It goes off of the USER'S option settings so if you have it set to only have two worksheets then it will only add two worksheets. You can modify your code to be like this:

    Code:
     
    Function ExportExcel_lot()
        Dim xlworkbook As Excel.Workbook
        Dim currpath As String
        DoCmd.SetWarnings False
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Visible = True
        Set xlworkbook = xlApp.Workbooks.Add
        Dim r1     As ADODB.Recordset
        Set r1 = New ADODB.Recordset
        r1.CursorLocation = adUseClient
        With xlworkbook
           Do Until .Worksheets.Count >= 4
               .Worksheets.Add
           Loop
            .Worksheets(1).Name = "Doff"
            .Worksheets(2).Name = "Creel"
            .Worksheets(3).Name = "Sizing"
            .Worksheets(4).Name = "Packing"
        End With
        DoCmd.SetWarnings True
    End Function

    And you incorrectly had the code
    xlapp.Sheets.Add
    which is not right. It is the workbook object which should be adding them.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 02-13-2013, 02:35 PM
  2. "Subscript out of range" Error
    By yes sir in forum Access
    Replies: 21
    Last Post: 08-16-2012, 08:02 PM
  3. Replies: 2
    Last Post: 11-25-2010, 11:01 AM
  4. Database Split - Error Subscript out of range
    By Huddle in forum Database Design
    Replies: 7
    Last Post: 07-16-2010, 01:52 PM
  5. need help with summing a range
    By sundance0000 in forum Access
    Replies: 3
    Last Post: 01-12-2010, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums