Results 1 to 4 of 4
  1. #1
    jadown is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    11

    Using Access to Count Worksheets in Workbook

    Hello,



    I'm stuck once again. I would like Access to open an Excel file count the worksheets and then give each tab the name of the column heading in cell A1 of each worksheet. I had no problem with the code in Excel but when I tried to add it to Access I am getting an error on the WS_Count = obj.ActiveWorkbook.Worksheets.Count line. The error message is "Object variable or With block variable not set" I'm not understanding what I did wrong.


    Here is the code:

    Code:
    'This block of code will cycle through each tab and name them
             Dim WS_Count As Integer
             Dim I As Integer
             
             Dim obj As Object
             
             Set obj = CreateObject("Excel.Application")
              
              obj.Visible = True
             ' Set WS_Count equal to the number of worksheets in the active
             ' workbook.
              WS_Count = obj.ActiveWorkbook.Worksheets.Count
            
           ' Begin the loop.
             For I = 1 To WS_Count
               
                ' The following line shows how to reference a sheet within
                ' the loop by displaying the worksheet name in a dialog box.
                obj.ActiveWorkbook.Worksheets(I).Name = obj.ActiveWorkbook.Worksheets(I).Range("A1").Value
                         
             Next I

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I don't think you can use 'activeworkbook' when you're working in access, you would have to actually set up the workbook and tell it which file to open

    activeworkbook assumes you are trying to run the code in an excel file you already have open, something you're not doing with your code.


    You'd need something more like this:
    http://www.experts-exchange.com/Data..._28142208.html

  3. #3
    robbeh is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    55
    rpeare is right, you will need to open the workbook and reference that object.

    Code:
    Dim wb As Object
    Dim obj As Object
    
    Set obj = CreateObject("Excel.Application")
    Set wb = obj.Workbooks.Open("workbook path")
    WS_Count = wb.Worksheets.Count
    Or something along those lines. You might need to reference the Microsoft Excel Object Library in Tools -> References.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Ken Snell has examples of importing and exporting Excel workbooks. I know you are not importing or exporting, but it shows examples of Excel automation from Access. Worth a look....

    http://www.accessmvp.com/kdsnell/EXCEL_MainPage.htm

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

Similar Threads

  1. Replies: 1
    Last Post: 08-08-2013, 03:54 PM
  2. New workbook/worksheets using Transfer Spreadsheet
    By nyneave in forum Import/Export Data
    Replies: 3
    Last Post: 09-05-2012, 06:44 PM
  3. Replies: 1
    Last Post: 03-12-2012, 02:21 PM
  4. Deleting Worksheets on a Excel workbook
    By BED in forum Programming
    Replies: 0
    Last Post: 07-27-2010, 01:20 PM
  5. Import Excel Worksheets into Access 2003
    By KramerJ in forum Programming
    Replies: 0
    Last Post: 03-18-2009, 04:11 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