[英]How to open files contained in a folder in current file path
我想打开一个文件( file
),该文件存储在与当前工作簿位于同一目录的文件夹( Source
)中。 我收到一个运行时错误1004,指示找不到该文件。 我在做什么?
Set x = Workbooks.Open(ThisWorkbook.Path & "\Source\file*.xlsx")
由于要保留通配符,因此需要遍历文件夹中的文件。 您可能会对这样的事情感兴趣:
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
祝好运!
将通配符替换为实际的文件名。
设置x = Workbooks.Open(ThisWorkbook.Path和“ \\ Source \\ file.xlsx”
我将file * .xlsx更改为file。 xlsx ...希望您的代码有效。
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.