【发布时间】:2017-08-29 15:20:37
【问题描述】:
我目前正在研究 VBA 中的用户可定制性,同时搜索其他一些工作簿。我在将Dir() 函数中的FileName 表达式转换为文件夹名称后带有正确反斜杠的路径目录时遇到问题,然后在File 周围使用通配符以允许Dir 搜索所有出现的关键字。目前我认为 \ 被省略了,我还不能确定我的通配符是否有效
' Modify this folder path to point to the files you want to use.
Folder = InputBox("Enter folder directory of files")
' e.g C:\peter\management\Test Folder
File = InputBox("Enter filename keyword")
'e.g. PLACE
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(Folder & "\" & "*" & File & "*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
我假设我的语法对于我想要实现的目标不正确。任何帮助将不胜感激!
编辑:
' Modify this folder path to point to the files you want to use.
Folder = InputBox("Enter folder directory of files")
' e.g C:\peter\management\Test Folder
File = InputBox("Enter filename keyword")
'e.g. PLACE
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(Folder & "\" & File & "*" & ".xls")
Debug.Print (FileName)
' Loop until Dir returns an empty string.
Do While FileName <> ""
是我目前正在使用的。我的 Dir 行中的“\”似乎没有做任何事情,因为我仍然需要在文件之前手动添加最后一个 \ 才能显示在我的错误消息中。
【问题讨论】:
-
文件扩展名是什么?
-
.xls,但它不应该被通配符覆盖吗?
-
使用
FileName = Dir(Folder & "\" & File & ".*") -
这取决于是否存在与用于定位 XLS 的通配符相同的 PDF 等文件名。
-
为什么你不能判断代码是否有效?