【问题标题】:Issues using wildcards with strings in Dir function VBA在 Dir 函数 VBA 中使用带有字符串的通配符的问题
【发布时间】: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 &amp; "\" &amp; File &amp; ".*")
  • 这取决于是否存在与用于定位 XLS 的通配符相同的 PDF 等文件名。
  • 为什么你不能判断代码是否有效?

标签: vba excel


【解决方案1】:

当我尝试您的代码时,它对我有用。不用说,要提供令人满意的答案有点棘手!

以下是我解决相同问题的尝试。

我没有要求用户手动输入文件夹地址,而是使用了Excel's built-in folder picker。这避免了检查和处理拼写错误的需要。

Sub FindFiles()
    Dim fldDialog As FileDialog         ' Holds a reference to the folder picker.
    Dim path As String                  ' Folder selected by user.
    Dim fileFilter As String            ' Provided by user, wildcard supported.
    Dim found As String                 ' Used to display returned results.

    ' Config dialog.
    Set fldDialog = Application.FileDialog(msoFileDialogFolderPicker)
    fldDialog.Title = "Pick a folder"       ' Caption for dialog.
    fldDialog.AllowMultiSelect = False      ' Limit to one folder.
    fldDialog.InitialFileName = "C:\"       ' Default starting folder.

    ' Display to user.
    If fldDialog.Show Then

        ' Config filter.
        path = fldDialog.SelectedItems(1)
        fileFilter = InputBox("Select a filter (*.*)", "File filter", "*.*")

        ' Get results.
        found = Dir(path & "\" & fileFilter)
        Do Until found = vbNullString

            MsgBox found, vbInformation, "File found"
            found = Dir()
        Loop
    Else

        MsgBox "User pressed cancel", vbInformation, "Folder picker"
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多