Results 1 to 8 of 8
  1. #1
    rndmxy2k13 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    8

    Import a specific Excel worksheet in Access using VBA

    Hi everybody,

    I m trying to import an Excel worksheet which is not the first thats being displayed in the overview into Access.
    When I try running my VBA code its telling me that I ve entered an expression which does not have the required data type for the argument.

    My Code:


    Code:
    Option Compare Database




    '------------------------------------------------------------
    '
    AutQry
    '
    '------------------------------------------------------------

    Function AutQry()
    On Error GoTo AutQry_Err


    DoCmd.TransferSpreadsheet acImport, 10, "richtigerpfad", True, "Tabelle2!", ""

    DoCmd.OpenForm "Auswahlabfrage", acNormal, "", "", , acNormal



    AutQry_Exit:
    Exit Function

    AutQry_Err:

    MsgBox Error$
    Resume AutQry_Exit

    End Function


    What am I doing wrong?

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You need a File Name argument with a full path and file name.
    See: http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

  3. #3
    rndmxy2k13 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    8
    Quote Originally Posted by JoeM View Post
    You need a File Name argument with a full path and file name.
    See: http://msdn.microsoft.com/en-us/libr...ffice.12).aspx
    Sorry I forgot to translate this part, "richtigerpfad" actually means "correct path".
    I wrote this so I wouldn t have to copy the whole path and forgot to translate it before posting here , the path is actually correct
    Still, I cannot chose the particular worksheet I want to import data from.

    Thanks for the quick response anyhow!

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I often use Named Ranges. If you name the range you want to import in Excel, you can then use that named range in the Range argument.

  5. #5
    rndmxy2k13 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    8
    Thank you for your quick responses, naming ranges is a good idea, but doesnt really seem to solve my problem.
    I'm trying to choose the second worksheet as shown in the picture: Click image for larger version. 

Name:	xcl.jpg 
Views:	13 
Size:	15.9 KB 
ID:	13844
    Of course I could just just switch the two sheets and have it imported with the standard command but I'm trying to get rid of this step to fasten the overall import process.
    Any ideas?
    Attached Thumbnails Attached Thumbnails xcl.jpg  

  6. #6
    rndmxy2k13 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    8
    Solved the issue, I had to use the executecode action and put "public" in front of the function name within my VBA module.

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Thank you for your quick responses, naming ranges is a good idea, but doesnt really seem to solve my problem
    Did you try it? If you used named ranges, it doesn't matter what sheet the range is on, you do not need to include a sheet reference in your range, nor does it need to be the first sheet when the file is opened. Simply reference the named range.
    Try it, you'll see!

  8. #8
    rndmxy2k13 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    8
    Ok JoeM you're right, I just tried it and it worked.
    Thanks for the help

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

Similar Threads

  1. Replies: 9
    Last Post: 06-21-2013, 08:22 PM
  2. Replies: 3
    Last Post: 08-22-2012, 06:28 AM
  3. Replies: 6
    Last Post: 11-05-2011, 09:01 AM
  4. VBA to Import Excel Worksheet
    By bdaniel in forum Programming
    Replies: 2
    Last Post: 11-23-2010, 10:53 AM
  5. Replies: 1
    Last Post: 11-21-2010, 10:26 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