Dir function in VBA, is there any ways to use logics operator in wildcard?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub f()
Dim foldername As String
Dim filename As String
Dim lastrow As Integer
Dim allrange As Range
foldername = "C:UsersJay.XieDesktopSW FolderDataData"
a = "*Group*"
filename = Dir(foldername & a)
</code>
<code>Sub f() Dim foldername As String Dim filename As String Dim lastrow As Integer Dim allrange As Range foldername = "C:UsersJay.XieDesktopSW FolderDataData" a = "*Group*" filename = Dir(foldername & a) </code>
Sub f()

Dim foldername As String
Dim filename As String
Dim lastrow As Integer

Dim allrange As Range


foldername = "C:UsersJay.XieDesktopSW FolderDataData"

a = "*Group*"


filename = Dir(foldername & a)

I want to search all the files with the word “Group” or “Proportion” in it, is there any ways to achieve that?

I tried using operator in the string itself but it doesn’t seem to work.

I can also create the same code again to so that it runs on the word “Proportion” but I don’t think its effective.

New contributor

Jay Xie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

You can using the Command-Line DIR

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Option Explicit
Sub ListFiles()
Const PATTERN = "*Group*,*Proportion*"
Dim FolderName As String
FolderName = "C:UsersJay.XieDesktopSW FolderDataData"
Dim wsh As Object, s As String, f, r As Long
Set wsh = CreateObject("WScript.Shell")
s = wsh.Exec("cmd /c dir /b " & FolderName & PATTERN).StdOut.ReadAll
For Each f In Split(s, vbCr & vbLf)
If Len(f) > 0 Then
r = r + 1
Sheet1.Cells(r, 1) = f
End If
Next
MsgBox r & " files found", vbInformation
End Sub
</code>
<code>Option Explicit Sub ListFiles() Const PATTERN = "*Group*,*Proportion*" Dim FolderName As String FolderName = "C:UsersJay.XieDesktopSW FolderDataData" Dim wsh As Object, s As String, f, r As Long Set wsh = CreateObject("WScript.Shell") s = wsh.Exec("cmd /c dir /b " & FolderName & PATTERN).StdOut.ReadAll For Each f In Split(s, vbCr & vbLf) If Len(f) > 0 Then r = r + 1 Sheet1.Cells(r, 1) = f End If Next MsgBox r & " files found", vbInformation End Sub </code>
Option Explicit

Sub ListFiles()

    Const PATTERN = "*Group*,*Proportion*"
    Dim FolderName As String
    FolderName = "C:UsersJay.XieDesktopSW FolderDataData"

    Dim wsh As Object, s As String, f, r As Long
    Set wsh = CreateObject("WScript.Shell")
    
    s = wsh.Exec("cmd /c dir /b " & FolderName & PATTERN).StdOut.ReadAll
    For Each f In Split(s, vbCr & vbLf)
        If Len(f) > 0 Then
            r = r + 1
            Sheet1.Cells(r, 1) = f
        End If
    Next
    MsgBox r & " files found", vbInformation

End Sub

Check Whether File Names Contain Any String Using Dir

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Option Explicit
Sub LoopThroughFiles()
Const FOLDER_PATH As String = _
"C:UsersJay.XieDesktopSW FolderDataData"
Const FILE_PATTERN As String = "*.xls*" ' adjust!
' The following two must be 'in sync'.
Const COMPARE_STRINGS As String = "Group,Proportion" ' add more
Const COMPARE_DELIMITER As String = ","
Dim CompareStrings() As String:
CompareStrings = Split(COMPARE_STRINGS, COMPARE_DELIMITER)
Dim UpperLimit As Long: UpperLimit = UBound(CompareStrings)
Dim fName As String: fName = Dir(FOLDER_PATH & FILE_PATTERN)
Dim i As Long, BaseName As String, IsMatch As Boolean
Do While Len(fName) > 0
' Remove the file extension.
BaseName = Left(fName, InStrRev(fName, ".") - 1)
' Loop through the compare array to determine whether it's a match.
IsMatch = False
For i = 0 To UpperLimit
If InStr(1, BaseName, CompareStrings(i), vbTextCompare) > 0 Then
IsMatch = True
Exit For
End If
Next i
' Do something if it's a match, e.g.:
If IsMatch Then
Debug.Print fName
End If
fName = Dir ' next file
Loop
End Sub
</code>
<code>Option Explicit Sub LoopThroughFiles() Const FOLDER_PATH As String = _ "C:UsersJay.XieDesktopSW FolderDataData" Const FILE_PATTERN As String = "*.xls*" ' adjust! ' The following two must be 'in sync'. Const COMPARE_STRINGS As String = "Group,Proportion" ' add more Const COMPARE_DELIMITER As String = "," Dim CompareStrings() As String: CompareStrings = Split(COMPARE_STRINGS, COMPARE_DELIMITER) Dim UpperLimit As Long: UpperLimit = UBound(CompareStrings) Dim fName As String: fName = Dir(FOLDER_PATH & FILE_PATTERN) Dim i As Long, BaseName As String, IsMatch As Boolean Do While Len(fName) > 0 ' Remove the file extension. BaseName = Left(fName, InStrRev(fName, ".") - 1) ' Loop through the compare array to determine whether it's a match. IsMatch = False For i = 0 To UpperLimit If InStr(1, BaseName, CompareStrings(i), vbTextCompare) > 0 Then IsMatch = True Exit For End If Next i ' Do something if it's a match, e.g.: If IsMatch Then Debug.Print fName End If fName = Dir ' next file Loop End Sub </code>
Option Explicit

Sub LoopThroughFiles()
    
    Const FOLDER_PATH As String = _
        "C:UsersJay.XieDesktopSW FolderDataData"
    Const FILE_PATTERN As String = "*.xls*" ' adjust!
    ' The following two must be 'in sync'.
    Const COMPARE_STRINGS As String = "Group,Proportion" ' add more
    Const COMPARE_DELIMITER As String = ","
    
    Dim CompareStrings() As String:
    CompareStrings = Split(COMPARE_STRINGS, COMPARE_DELIMITER)
    Dim UpperLimit As Long: UpperLimit = UBound(CompareStrings)
    
    Dim fName As String: fName = Dir(FOLDER_PATH & FILE_PATTERN)
    
    Dim i As Long, BaseName As String, IsMatch As Boolean
    
    Do While Len(fName) > 0
        ' Remove the file extension.
        BaseName = Left(fName, InStrRev(fName, ".") - 1)
        ' Loop through the compare array to determine whether it's a match.
        IsMatch = False
        For i = 0 To UpperLimit
            If InStr(1, BaseName, CompareStrings(i), vbTextCompare) > 0 Then
                IsMatch = True
                Exit For
            End If
        Next i
        
        ' Do something if it's a match, e.g.:
        If IsMatch Then
            Debug.Print fName
        End If
        
        fName = Dir ' next file
    Loop

End Sub

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật