首页 > 解决方案 > 使用文件对话框时,如果文件名包含单词,则过滤文件

问题描述

我想在使用文件对话框选择文件时过滤文件。我想排除名称中包含“冲突”的任何文件。这可能吗?

Sub OpenMultipleFiles()
Dim fd As FileDialog
Dim fileChosen As Integer
Dim basename As String
Dim fso As Variant
Set fso = CreateObject("Scripting.FileSystemObject")
Set fd = Application.FileDialog(msoFileDialogFilePicker)
basename = fso.getBaseName(ActiveWorkbook.Name)
fd.InitialFileName = ActiveWorkbook.Path ' Set Default Location to the Active Workbook Path
fd.InitialView = msoFileDialogViewList
fd.AllowMultiSelect = True
fd.Filters.Add "All supported files", "*.xlsm"

fileChosen = fd.Show
If fileChosen = -1 Then

    StartAlgo

    Dim myArray As Variant
    Dim FullPath As String
    Dim FileName As String
    Dim x As Long
    
    'open each of the files chosen
    For x = 1 To fd.SelectedItems.Count
        FullPath = fd.SelectedItems(x)
        FileName = fso.getFileName(fd.SelectedItems(x))
        CopyContractTbls FullPath, FileName
    Next x

    EndAlgo
Else
    MsgBox "Aborted"
    Exit Sub
End If

End Sub

标签: excelvbaopenfiledialog

解决方案


推荐阅读