使用通配符与DIR功能中的字符串的问题



我当前正在搜索其他一些工作簿时,正在搜索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:petermanagementTest 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:petermanagementTest 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行中的" "似乎没有做任何事情

当我尝试您的代码时,它对我有用。不用说,这使得提供一个令人满意的答案变得有些棘手!

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

而不是要求用户手动输入我使用Excel的内置文件夹选择器的文件夹地址。这避免了需要检查并处理错别字。

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

最新更新