我在Excel xlsm文件中有一个宏,用于打开和关闭某个文件夹中的许多csv文件,同时从中提取信息。以下代码代表:
Dim wbPath As String
Dim PathTextPart2 As String
Dim FName As String
PathTextPart2 = 123456
wbPath = ThisWorkbook.Path '="H:\Folder"
FName = Dir(wbPath & "\" & PathTextPart2 & "PathTextPart3*.csv")
Workbooks.Open (FName)
宏失败,因为FName返回“123456PathTextPart3789.csv”,不知何故在字符串的开头留下“H:\ Folder \”。我已经搜索了我的问题的解决方案,但没有发现任何问题。有谁能发现问题所在?
答案 0 :(得分:0)
答案 1 :(得分:0)
如果您打算遍历文件夹中的所有文件" H:\ Folder \ 123456"使用下面的代码。
注意:我已添加了条件测试Like "*.csv"
,这只会打开带有" .csv"的文件。扩展
<强>代码强>
Sub LoopCSVFiles()
Dim wbPath As String
Dim PathTextPart2 As String
Dim FName As String
PathTextPart2 = 123456
wbPath = ThisWorkbook.Path & "\" & PathTextPart2 & "\" ' "H:\Folder"\123456\"
FName = Dir(wbPath)
' loop through all files in folder
Do Until FName = ""
If FName Like "*.csv" Then ' only open files with .csv extension
Workbooks.Open (FName)
' put the rest of your code here
End If
FName = Dir()
Loop
End Sub