VBA对话FileFilter部分文件名 [英] VBA Dialogue FileFilter Partial File Name

查看:2383
本文介绍了VBA对话FileFilter部分文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个.txt文件的目录。比方说:

pre $ hi.txt
hello.txt
hello_test.txt
test.txt

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



下面看起来应该可以,但不会:

$ p $ Sub TestIt()
Dim test As Variant'傻vba没有返回类型..
test = Application.GetOpenFilename(FileFilter:=test(* test.txt),* test.txt )
结束小组

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

解决方案

我看到你关心的是将文本放在文件名框中,但这正是你需要做的,似乎是你的情况的规范。我挂断了同样的问题。



这是我用的:
$ b $ pre $ Public Sub Browse_Click

Dim fileName As String
Dim result As Integer
Dim fs

With Application.FileDialog(msoFileDialogFilePicker)
.Title =Select测试文件
.Filters.AddText 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


I have a directory with several .txt files. Let's say

hi.txt
hello.txt
hello_test.txt
test.txt

Using a file dialogue in VBA, how can I filter to show only "*test.txt" matching files (ie last two) in the dropdown? Or can I only use *. filters?

The following seems it should work but does not:

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

edit: clarifying in case this wasn't clear: I want to filter "test.txt" instead of ".txt" files so I can only select from hello_test.txt and test.txt in the chooser.

解决方案

I see that you are concerned about putting text in the file name box, but that is exactly what you need to do and appears to be the norm for your situation. I got hung up on the exact same issue.

This is what I used:

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

这篇关于VBA对话FileFilter部分文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆