Results 1 to 6 of 6
  1. #1
    greggue is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    12

    Exporting to excel Adding new Tab

    Hell all,

    I appreciate the help in advance.

    I'm trying to add a new worksheet to excel and then import the data from a query to this new worksheet. I am able to import the data to the first 3 worksheets but when I get to the 4th one which is not created I get a Subscript out of range error at the line XlBook.Worksheets("Sheet4").Add

    Here's part of the code



    Dim Xl As Excel.Application
    Dim XlBook As Excel.Workbook
    Dim XlSheet As Excel.Worksheet
    Dim wrkshtcnt As Integer ' worksheet count

    wrkshtcnt = 1
    Stop
    'Open an instance of Excel, open the workbook.
    Set Xl = CreateObject("Excel.Application")
    Set XlBook = GetObject(MySheetPath)
    'Make sure everything is visible on the screen.
    Xl.Visible = True
    XlBook.Windows(wrkshtcnt).Visible = True
    XlBook.Windows(wrkshtcnt).Activate

    'Define the topmost sheet in the Workbook as XLSheet,


    wrkshtcnt = 1

    ' LOOP

    Do While wrkshtcnt < 5

    If wrkshtcnt >= 4 Then
    XlBook.Worksheets("Sheet4").Add
    XlBook.Windows(wrkshtcnt).Activate
    XlBook.Windows(wrkshtcnt).Visible = True
    End If

    Set XlSheet = XlBook.Worksheets(wrkshtcnt)
    Select Case wrkshtcnt
    Case 1
    MySQL = qryDesig
    XlSheet.Name = "Designations"
    Case 2

    MySQL = qryRegMeth
    XlSheet.Name = "Registrations Methods"
    Case 3
    MySQL = qryPracArea
    XlSheet.Name = "Practice Areas"
    Case 4
    MySQL = qryProv
    XlSheet.Name = "Province Count"
    End Select

    'Copy the recordset to worksheet starting at cell B3.
    MyRecordset.Open MySQL
    XlSheet.Range("B3").CopyFromRecordset MyRecordset
    wrkshtcnt = wrkshtcnt + 1
    MyRecordset.Close
    ' END LOOP
    Loop



    'Clean up and end with worksheet visible on the screen.
    Set cnn = Nothing
    Set Xl = Nothing
    Set XlBook = Nothing
    Set XlSheet = Nothing
    EventID = 0

    End Sub

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    The problem is that your worksheet name does not exist and you are asking it to put the new worksheet before that worksheet.

    So, what you can do is to instead just use the worksheet number. Are you wanting the sheets to go before each other or just after the last one?

  3. #3
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529

  4. #4
    greggue is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    12
    HI Thanks for your help.

    I want to add it after.

  5. #5
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by greggue View Post
    HI Thanks for your help.

    I want to add it after.
    So this code:
    Code:
    ' LOOP
    
    Do While wrkshtcnt < 5
    
    If wrkshtcnt >= 4 Then
    XlBook.Worksheets("Sheet4").Add
    Should be this:
    Code:
    ' LOOP
    
    Do While wrkshtcnt < 5
    
    If XlBook.Worksheets.Count < 4 Then
        XlBook.Worksheets.AddNew
    End If

  6. #6
    greggue is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    12
    Thank you Bob,

    This issue is resolved.

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

Similar Threads

  1. exporting to Excel
    By johnririe in forum Import/Export Data
    Replies: 6
    Last Post: 08-23-2011, 10:16 AM
  2. Exporting to Excel Help
    By Coffee in forum Import/Export Data
    Replies: 4
    Last Post: 07-18-2011, 07:37 AM
  3. Exporting to Excel
    By TheDeceived in forum Programming
    Replies: 0
    Last Post: 11-15-2010, 08:56 AM
  4. Exporting to Excel
    By DreamOn in forum Import/Export Data
    Replies: 1
    Last Post: 05-23-2010, 10:27 PM
  5. Really Need Help With Exporting to Excel
    By graviz in forum Import/Export Data
    Replies: 0
    Last Post: 09-24-2009, 08:29 AM

Tags for this Thread

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