使用VBA将Access 2016链接到Excel工作表

时间:2018-01-22 15:33:19

标签: excel vba ms-access access-vba

我正在尝试使用以下代码链接Access到Excel,但不断收到“找不到文件”。我检查了位置,它指向我想要链接的文件。

Option Compare Database

Option Explicit

'code will link to excel and pull site survey files into access tables

'Setting the path for the directory

Sub LinkExcel()
Dim iFile As String 'Filename
Dim iFileList() As String 'File Array
Dim intFile As Integer 'File Number

Dim iPath As String

iPath = "C:\Users\mchattopad004\Documents\Files\TestGDAST.xlxs" 'Directory Path

'Loop through the folder & build file list
iFile = Dir(iPath & "*.xlxs")
While iFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve iFileList(1 To intFile)
    iFileList(intFile) = iFile
    iFile = Dir()
Wend

'see if any files were found
If intFile = 0 Then
    MsgBox "No files found"
    Exit Sub
End If

'cycle through the list of files & link to Access
For intFile = 1 To UBound(iFileList)
    DoCmd.TransferSpreadsheet acLink, , _
    iFileList(intFile), iPath & iFileList(intFile), True, "MA!A1:J3299" 'Set your range here.
Next

MsgBox UBound(iFileList) & " Files were Linked"

End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

iPath = "C:\Users\mchattopad004\Documents\Files\" 'Directory Path

'Loop through the folder & build file list
iFile = Dir(iPath & "*.xlxs")