Results 1 to 7 of 7
  1. #1
    smakkiee is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2012
    Posts
    3

    Import Excel File using a Macro

    I need to create a Macro in Access that will allow me to import an excel file into an access database.

    The requirements are:
    -The code should allow me to choose whether the first row has fields
    -Browse the Excel file (cannot be specific because this excel file may change)
    -Choose the Excel file and specific sheet to transfer (cannot be specific because this sheet may change)
    -Skip 'n' rows in order to get to the data of the excel table
    -Transfer the sheet into an access table
    -Delete original file that was imported

    I attached what i have so far but it's missing two requirements: Skipping rows and being able to choose a specific sheet instead of just a folder that contains the excel file.

    Hope you can help!

    Thanks

    Private Sub btnUpdate_Click()

    Dim strPathFile As String, strFile As String, strPath As String
    Dim strTable As String, strBrowseMsg As String


    Dim blnHasFieldNames As Boolean

    blnHasFieldNames = False
    strBrowseMsg = "Select the folder that contains the EXCEL files:"
    strPath = BrowseFolder(strBrowseMsg)
    If strPath = "" Then
    MsgBox "No folder was selected.", vbOK, "No Selection"
    Exit Sub
    End If

    strTable = "EMT"
    strFile = Dir(strPath & "\*.xls")
    Do While Len(strFile) > 0
    strPathFile = strPath & "\" & strFile
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
    strTable, strPathFile, blnHasFieldNames

    Kill strPathFile
    strFile = Dir()
    Loop
    End Sub
    Last edited by smakkiee; 07-23-2012 at 02:09 PM. Reason: Added current code

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Ken Snell has an excellent site about EXCEL Workbook Files - both importing and exporting. See if this site helps you:
    http://www.accessmvp.com/kdsnell/EXCEL_Import.htm

  3. #3
    smakkiee is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2012
    Posts
    3
    Quote Originally Posted by ssanfu View Post
    Ken Snell has an excellent site about EXCEL Workbook Files - both importing and exporting. See if this site helps you:
    http://www.accessmvp.com/kdsnell/EXCEL_Import.htm
    Hi Sanfu,

    I actually got my code from that site but there are some things that I need on top of it that I listed in the requirements. Any thoughts?

  4. #4
    The VBA Man is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jul 2012
    Posts
    7
    smakkiee, looks like you have a lot of criteria for the target Excel data. have you considered doing things the other way around? i.e. instead of starting at Access, start at Excel. select the rows you want to import, then go to Access, and import only those "active" rows from the "active" sheet

  5. #5
    smakkiee is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2012
    Posts
    3
    Quote Originally Posted by The VBA Man View Post
    smakkiee, looks like you have a lot of criteria for the target Excel data. have you considered doing things the other way around? i.e. instead of starting at Access, start at Excel. select the rows you want to import, then go to Access, and import only those "active" rows from the "active" sheet
    The reason I don't want to use excel is because the current database is using excel and its too slow. That's why I am converting it to Access. Along with this, I can't start with Excel because the spreadsheet I am importing is not going to be the same spreadsheet every time I need to update it.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    From what I see:
    -The code should allow me to choose whether the first row has fields Done
    -Browse the Excel file (cannot be specific because this excel file may change) Done
    -Choose the Excel file and specific sheet to transfer (cannot be specific because this sheet may change) Done
    -Skip 'n' rows in order to get to the data of the excel table............ Can not be Done using Transferspreadsheet (AFAIK)
    -Transfer the sheet into an access table Done
    -Delete original file that was imported Done

    To be able to specify rows to skip, you will have to use other methods to import the data. (VBA or multiple queries/tables)

  7. #7
    lynthel is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    30
    When I apply the same code in my access under a button I created "Import Macro", I get a compile error "Sub or Function not defined" on the "strPath = BrowseFolder(strBrowseMsg)" line...can someone explain why?

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

Similar Threads

  1. Excel Import Macro Help in Access
    By vennies83 in forum Import/Export Data
    Replies: 5
    Last Post: 11-02-2011, 09:24 AM
  2. Excel Import - File with row spaces
    By Jackfam58 in forum Import/Export Data
    Replies: 1
    Last Post: 04-13-2011, 07:35 PM
  3. Import Excel File Using Macro?
    By oregoncrete in forum Import/Export Data
    Replies: 0
    Last Post: 04-05-2011, 12:26 PM
  4. Replies: 3
    Last Post: 12-21-2010, 11:52 AM
  5. Question on Macro to automate file import
    By delkath in forum Access
    Replies: 4
    Last Post: 05-25-2010, 04:28 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