繁体   English   中英

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

[英]Linking Access 2016 to excel sheet using VBA

我尝试使用下面的代码链接访问到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

尝试这个:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM