簡體   English   中英

VBA中If語句中的通配符數組

[英]Wildcard Array in If Statement in VBA

我要用此代碼執行的操作是:

  1. 瀏覽該指定文件夾中的所有文件以及該文件夾中的所有子文件夾。 (該文件夾中的文件通常由下划線分成5個部分。例如,“ XX1_XX2_XX3_XX4_XX5”

  2. 如果myarray中的3個字符指示符中的任何一個與文件名中的XX2相匹配,則在Cell(22,3)上列出XX4,在Cell(22,4)上列出XX5,並繼續重復...... Cell(23,3) ,Cell(23,4),Cell(24,3,),Cell(24,4).....等 我只想要完全匹配。不確定該怎么做。

  3. 文件夾中有一些文件只有3個下划線...因此是“ XX1_XX2_XX3_XX4”。 對於這些文件,如果myarray與XX2匹配,則在Cells(i,3)上列出XX4,並為Cells(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

未經測試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM