如何在当前文件路径中打开文件夹中包含的文件 [英] How to open files contained in a folder in current file path
本文介绍了如何在当前文件路径中打开文件夹中包含的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打开一个文件(file
),该文件存储在与当前工作簿位于同一目录的文件夹(Source
)中.我收到一个运行时错误1004,指示找不到该文件.我在做什么?
I want to open a file (file
) that is stored in a folder (Source
) which is in the same directory as the current workbook. I get a runtime error 1004 indicating that it the file can't be located. What am I doing worng?
Set x = Workbooks.Open(ThisWorkbook.Path & "\Source\file*.xlsx")
推荐答案
由于要保留通配符,因此需要遍历文件夹中的文件.这样的事情可能会让您感兴趣:
Since you want the wildcard to stay, you need to loop through the files in the folder. Something like this may be of interest to you:
Sub FileOpen()
Dim sPath As String
Dim sFile As String
Dim wb As Workbook
sPath = ThisWorkbook.Path & "\Source\"
sFile = Dir(sPath & "file*.xlsx")
' Loops while there is a next file found in the specified directory
' When there is no next file the Dir() returns an empty string ""
Do While sFile <> ""
' Prints the full path of the found file
Debug.Print sPath & sFile
' Opens the currently found file
Set wb = Workbooks.Open(sPath & sFile)
' Place your code here
' Place your code here
' Place your code here
' Close the current workbook and move on to the next
wb.Close
' This line calls the Dir() function again to get the next file
sFile = Dir()
Loop
End Sub
祝你好运!
这篇关于如何在当前文件路径中打开文件夹中包含的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文