Results 1 to 3 of 3
  1. #1
    Rxp is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2012
    Posts
    15

    VBA Code to Import Excel Files

    How to write VBA code to import excel files into a table in Access with a click of a button? The excel file and access table has same column name.

  2. #2
    Rxp is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2012
    Posts
    15
    I found it:




    Private Sub Command0_Click()

    ' This subprocedure determines whether
    ' any Excel files exist in the folder stored
    ' in the strcPath constant; if there are,
    ' this subprocedure imports the data in the
    ' Excel files and then moves the files to
    ' the folder stored in the strcNewPath constant.


    ' Store paths:
    Const strcPath As String = _
    "C:\Users\rxp\Desktop\Excel\"
    Const strcNewPath As String = _
    "C:\Users\rxp\Desktop\Excel1\"

    ' Store the name of the table into which
    ' the data will be imported
    Const strcTableName As String = "Table1"

    Dim strPath As String
    Dim strNewPath As String
    Dim strFile As String
    Dim strFileList() As String
    Dim intFile As Integer
    Dim strFullPath As String
    Dim strFullNewPath As String


    ' See if path constant ends in a backslash:
    If Right(strcPath, 1) = "\" Then
    strPath = strcPath
    Else
    strPath = strcPath & "\"
    End If

    ' See if new path constant ends in a backslash:
    If Right(strcNewPath, 1) = "\" Then
    strNewPath = strcNewPath
    Else
    strNewPath = strcNewPath & "\"
    End If


    ' Loop through the Excel files in the folder
    ' (if any) and build file list:
    strFile = Dir(strPath & "*.xlsx")
    While strFile <> ""
    intFile = intFile + 1
    ReDim Preserve strFileList(1 To intFile)
    strFileList(intFile) = strFile
    strFile = Dir()
    Wend


    ' See if any files were found:
    If intFile = 0 Then

    MsgBox strcPath & vbNewLine & vbNewLine _
    & "The above directory contains no Excel " _
    & "files.", _
    vbExclamation + vbOKOnly, "Program Finished"

    GoTo Exit_Import_From_Excel

    End If


    ' Loop through the list of files:
    For intFile = 1 To UBound(strFileList)

    ' Initialise paths:
    strFullPath = strPath & strFileList(intFile)
    strFullNewPath = strNewPath & strFileList(intFile)

    ' Import into Access:
    DoCmd.TransferSpreadsheet acImport, _
    acSpreadsheetTypeExcel97, strcTableName, _
    strFullPath, True

    ' Copy file to new location:
    FileCopy strFullPath, strFullNewPath

    ' Delete old file:
    Kill strFullPath

    Next

    MsgBox UBound(strFileList) & " file(s) were imported", _
    vbOKOnly + vbInformation, "Program Finished"

    Exit_Import_From_Excel:


    Exit Sub

    End Sub

  3. #3
    Kimbertha is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    33

    Import from Excel -- Way Cool!

    This worked for me! Unfortunately myclient's workbook has Instructions on the first tab and that is what gets imported.

    How do I specify a tab and a range within that tab?
    What is the syntax to link a file rather than import?

    I am a VBA noob so I don't have a clue on this.


    Quote Originally Posted by Rxp View Post
    I found it:




    Private Sub Command0_Click()

    ' This subprocedure determines whether
    ' any Excel files exist in the folder stored
    ' in the strcPath constant; if there are,
    ' this subprocedure imports the data in the
    ' Excel files and then moves the files to
    ' the folder stored in the strcNewPath constant.


    ' Store paths:
    Const strcPath As String = _
    "C:\Users\rxp\Desktop\Excel\"
    Const strcNewPath As String = _
    "C:\Users\rxp\Desktop\Excel1\"

    ' Store the name of the table into which
    ' the data will be imported
    Const strcTableName As String = "Table1"

    Dim strPath As String
    Dim strNewPath As String
    Dim strFile As String
    Dim strFileList() As String
    Dim intFile As Integer
    Dim strFullPath As String
    Dim strFullNewPath As String


    ' See if path constant ends in a backslash:
    If Right(strcPath, 1) = "\" Then
    strPath = strcPath
    Else
    strPath = strcPath & "\"
    End If

    ' See if new path constant ends in a backslash:
    If Right(strcNewPath, 1) = "\" Then
    strNewPath = strcNewPath
    Else
    strNewPath = strcNewPath & "\"
    End If


    ' Loop through the Excel files in the folder
    ' (if any) and build file list:
    strFile = Dir(strPath & "*.xlsx")
    While strFile <> ""
    intFile = intFile + 1
    ReDim Preserve strFileList(1 To intFile)
    strFileList(intFile) = strFile
    strFile = Dir()
    Wend


    ' See if any files were found:
    If intFile = 0 Then

    MsgBox strcPath & vbNewLine & vbNewLine _
    & "The above directory contains no Excel " _
    & "files.", _
    vbExclamation + vbOKOnly, "Program Finished"

    GoTo Exit_Import_From_Excel

    End If


    ' Loop through the list of files:
    For intFile = 1 To UBound(strFileList)

    ' Initialise paths:
    strFullPath = strPath & strFileList(intFile)
    strFullNewPath = strNewPath & strFileList(intFile)

    ' Import into Access:
    DoCmd.TransferSpreadsheet acImport, _
    acSpreadsheetTypeExcel97, strcTableName, _
    strFullPath, True

    ' Copy file to new location:
    FileCopy strFullPath, strFullNewPath

    ' Delete old file:
    Kill strFullPath

    Next

    MsgBox UBound(strFileList) & " file(s) were imported", _
    vbOKOnly + vbInformation, "Program Finished"

    Exit_Import_From_Excel:


    Exit Sub

    End Sub

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

Similar Threads

  1. Replies: 1
    Last Post: 06-27-2012, 07:39 AM
  2. Import excel files
    By Jim.H. in forum Import/Export Data
    Replies: 1
    Last Post: 02-21-2012, 06:03 PM
  3. import excel files into access automatically
    By jstei012 in forum Import/Export Data
    Replies: 1
    Last Post: 12-19-2011, 04:12 PM
  4. Replies: 3
    Last Post: 10-06-2011, 06:53 AM
  5. Cannot import excel or text files
    By donald_s in forum Access
    Replies: 2
    Last Post: 04-13-2010, 11:48 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