0

Access 2010 の VBA でコードを作成して、Excel シートをリンクし、それらを Access のテーブルに配置しています。私は手順の外で無効を取得し続けstrFile = Dir(StrPath &"*.xls")ますstrPath is invalid outside procedure

助けてください。

Option Compare Database
Option Explicit

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

'Setting the path for the directory

Const strPath As String = "C:\Users\cparson\Documents\Survey_Eqpm\SiteSurveyData.xlsx"

'FileName
Dim strFile As String
'Array
Dim strFileList() As String
'File Number
Dim intFile As Integer

'Looping through the folder and building the file list
strFile = Dir(strPath & "*.xls")
While strFile <> ""
    'adding files to the list
    intFile = intFile + 1
    ReDim Preserve strFileList(1 To intFile)
    strFileList(intFile) = strFile
    strFile = Dir()
Wend
'checking to see if files where found
If intFile = 0 Then
    MsgBox "No Files Found"
    Exit Sub
End If
'going through the files and linking them to access
For intFile = 1 To UBound(strFileList)
    DoCmd.TransferSpreadsheet acLink, , _
    strFileList(intFile), strPath & strFileList(intFile), True, "A5:J17"
Next
MsgBox UBound(strFileList) & "Files were linked"
End Sub
4

3 に答える 3

1

プロシージャ名はありますEnd Subが、プロシージャ名はありませんか?

Option Compare Database
Option Explicit

Const strPath As String = "C:\Users\cparson\Documents\Survey_Eqpm\SiteSurveyData.xlsx"

Dim strFile As String
Dim strFileList() As String
Dim intFile As Integer

Sub Sample() '<~~ You are missing this...
    strFile = Dir(strPath & "*.xls")

    '~~> Rest of your code
End Sub
于 2012-05-22T14:34:24.380 に答える
0

あなたもADOを試すことができます、それは私の意見では簡単な方法です

YourConnObj.execute "SELECT * INTO  YourTableName from [Excel 14.0;DATABASE=c:\temp\data copy.xlsx].[Sheet1]"
于 2012-05-22T23:18:35.973 に答える