Results 1 to 2 of 2
  1. #1
    thestappa is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2008
    Posts
    16

    RunSavedImportExport on excel worksheet that might or might not exist.

    I have macro that steps through a bunch of saved ImportsExports. My problem is that sometimes the spreadsheets will have more than one worksheet. I added a new line to my macro to begin importing the second worksheet, however, it doesn't always exist. When the second sheet doesn't exist I get an error stating that "WORKSHEET_2$" is not a valid name.

    Is there anyway I can just skip that? My only option is to "Stop All Macros".

    I have SetWarnings = NO already but that isn't helping.



    I'm hoping I don't have to redo my macro all in VBA.

    But if I do have to go vba, is there an easy way to check if a worksheet exists?

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I'm hoping I don't have to redo my macro all in VBA.

    But if I do have to go vba, is there an easy way to check if a worksheet exists?
    Well, I think it is going to require VBA, but the good news is that it really isn't as hard as you think.

    Access has a great feature that converts your macros to VBA code for you. So this gives you a great start. Just highlight your Macro and select the "Convert Macros to Visual Basic" in the Visual Basic menu/ribbon. Then view your code.

    I think you may be able to bypass those errors with some error handling/trapping. Just before the step that checks for sheet 2, try adding the line:
    Code:
    On Error Resume Next
    and then after the part of the code that tries to import sheet 2, add this line to reset it:
    Code:
    On Error GoTo 0
    and see if that works.

    Trying to determine the number of sheets in each Excel file first is probably a "cleaner" option, but I think it will be a lot more complex, and probably mean having to use some Excel objects in your code. If my suggestion works, it will probably be easier for your to maintain/understand, expecially if you are already leery of VBA code.

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

Similar Threads

  1. Replies: 6
    Last Post: 11-05-2011, 09:01 AM
  2. Import excel worksheet by range with non identical headers
    By snoopy2003 in forum Import/Export Data
    Replies: 1
    Last Post: 03-14-2011, 01:04 AM
  3. VBA to Import Excel Worksheet
    By bdaniel in forum Programming
    Replies: 2
    Last Post: 11-23-2010, 10:53 AM
  4. Replies: 1
    Last Post: 11-21-2010, 10:26 PM
  5. Importing the second excel worksheet
    By geoffwbailey in forum Programming
    Replies: 1
    Last Post: 06-25-2010, 12:16 AM

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