我正在使用以下通常有效的代码。但我试图导入超过100个CSV文件并收到错误
错误:
运行时'3125' - FILENAME不是有效名称。确保它确实如此 不包括无效字符或标点符号,也不包含无效字符或标点符号 长。
我的代码下面:
Sub imports()
Const strPath As String = "J:\Centsupp\Risk Management\Debts Reporting (MD)\Adhoc\06 - Daves Work\Useful Tools\Compile Data\All Files\" 'Directory Path
Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
'Loop through the folder & build file list
strFile = Dir(strPath & "*.CSV")
While strFile <> ""
'add files to the list
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 "No files found"
Exit Sub
End If
'cycle through the list of files & import to Access
'creating a new table called MyTable
For intFile = 1 To UBound(strFileList)
DoCmd.TransferText acImportDelimi, , "addresspoint", strPath & strFileList(intFile)
'Check out the TransferSpreadsheet options in the Access
'Visual Basic Help file for a full description & list of
'optional settings
Next
MsgBox UBound(strFileList) & " Files were Imported"
End Sub
答案 0 :(得分:1)
你可能需要用引号包装你的路径 - 并纠正错字:
DoCmd.TransferText acImportDelim, , "addresspoint", Chr(34) & strPath & strFileList(intFile) & Chr(34)
答案 1 :(得分:0)
听起来很简单:文件名可能无效。但是你没有提供它。
文件名无效的最常见原因是因为它们包含非ascii字符。您可以对非ascii字符进行简单测试:
Public Function StringContainsNonASCII(str As String) As Boolean
Dim i As Integer
'Default is false
StringContainsNonASCII = False
'Remove question marks
str = Replace(str, "?", "")
For i = 1 To Len(str)
'Search for question marks
If Asc(Mid(str, i, 1)) = 63 Then
StringContainsNonASCII = True
Exit Function
End If
Next i
End Function
当您包含此功能时,您可以测试StringContainsNonAscii(strFile)
,如果返回true,则可以生成描述性错误。但是,您可以使用Access中的这些文件做很少的事情。您需要先重命名它们。