VBA中的If语句中的通配符数组

我试图用这个代码做的是:

  1. 浏览指定文件夹中的所有文件以及该文件夹中的所有子文件夹。 (该文件夹中的文件通常用下划线分隔5个部分,例如“XX1_XX2_XX3_XX4_XX5”

  2. 如果my myray中的3个字符指示符与文件名中的XX2相匹配,则在单元格(22,3)和单元格(22,4)上的XX5列出XX4并继续重复…… Cell(23,3) ,细胞(23,4),细胞(24,3),细胞(24,4)…..等。 我只想要完全匹配..不知道如何做到这一点。

  3. 文件夹中只有三个下划线的文件…所以“XX1_XX2_XX3_XX4”。 对于这些文件,如果myarray匹配XX2,然后在单元格(i,3)上列出XX4并显示单元格(i,4)的“NO INDICATOR”

如果我对上述说明的任何部分不清楚,请告诉我。 这是我迄今为止:

Sub tracker() Const FPATH As String = "\\KEVINXX\FILESXX\FILES\" Dim f As String, i, j As Long, arr, sht As Worksheet Dim pvt As PivotTable, pvtCache As PivotCache Dim myarray As Variant myarray = Array("ABC", "XYZ", "YYY", "XXX", "BBB", "CCC", "DDD", "EEE", "FFF", "JJJ") Set sht = ActiveSheet f = Dir("\\KEVINXX\FILESXX\FILES\") i = 22 Do While f <> "" 'split filename on underscore arr = Split(f, "_", 5) If UBound(arr) = 4 And "*" & myarray(j) & "*" Like arr(1) Then sht.Cells(i, 3).Value = arr(3) sht.Cells(i, 4).Value = arr(4) f = Dir() ' next file i = i + 1 Else sht.Cells(i, 3).Value = arr(3) sht.Cells(i, 4).Value = "No Indicator" f = Dir() i = i + 1 End If Loop 

未经testing:

 Sub tracker() Const FPATH As String = "\\KEVINXX\FILESXX\FILES\" Dim f As String, i, j As Long, arr, sht As Worksheet Dim pvt As PivotTable, pvtCache As PivotCache Dim myarray As Variant myarray = Array("ABC", "XYZ", "YYY", "XXX", "BBB", _ "CCC", "DDD", "EEE", "FFF", "JJJ") Set sht = ActiveSheet f = Dir("\\KEVINXX\FILESXX\FILES\") i = 22 Do While f <> "" 'split filename on underscore arr = Split(f, "_", 5) If UBound(arr) >= 3 Then If Not IsError(Application.Match(arr(1), myarray, 0)) Then sht.Cells(i, 3).Value = arr(3) If UBound(arr) >= 4 Then sht.Cells(i, 4).Value = arr(4) Else sht.Cells(i, 4).Value = "No indicator" End If i = i + 1 End If 'got match End If f = Dir() 'next file Loop End Sub