VBA对话FileFilter部分文件名

end*_*and 4 filesystems vba

我有一个包含几个.txt文件的目录.让我们说吧

hi.txt
hello.txt
hello_test.txt
test.txt
Run Code Online (Sandbox Code Playgroud)

在VBA中使用文件对话框,如何过滤以在下拉列表中仅显示"*test.txt"匹配文件(即最后两个)?或者我只能使用*.过滤器?

以下似乎应该有效,但不是:

Sub TestIt()
   Dim test As Variant 'silly vba for not having a return type..
   test = Application.GetOpenFilename(FileFilter:="test (*test.txt), *test.txt")
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑:澄清如果不清楚:我想过滤" test.txt"而不是" .txt"文件,所以我只能从选择器中的hello_test.txt和test.txt中选择.

And*_*que 11

我发现你担心将文本放在文件名框中,但这正是你需要做的,并且似乎是你情况的标准.我完全挂了同样的问题.

这是我用过的:

Public Sub Browse_Click()

Dim fileName As String
Dim result As Integer
Dim fs

With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select Test File"
    .Filters.Add "Text File", "*.txt"
    .FilterIndex = 1
    .AllowMultiSelect = False
    .InitialFileName = "*test*.*"

    result = .Show

    If (result <> 0) Then
        fileName = Trim(.SelectedItems.Item(1))

        Me!txtFileLocation = fileName

    End If
End With
Run Code Online (Sandbox Code Playgroud)