簡體   English   中英

VBA中的instr函數

[英]instr function in VBA

請幫助需要您的幫助,

描述:

我設計了一個VBA代碼,我想在目錄中比較一個字符串與FileName。在這種情況下,我使用了Instr函數,這僅在3種情況下對我有幫助,但不是動態地幫助我。

闡釋:

如果str = 4567並與filename配對,則filename可以是:
1.xs1234567.pdf
2.4567.pdf
3.4567(1).PDF
4.更新了4567(2).pdf

因此,我創建的代碼有助於查找所有文件,但這是不正確的。 它應排除第一個文件名,即:xs1234567.pdf

這是下面的代碼:

Dirfname = finDir
fName = Mid((Dirfname), 1, (InStr(1, (Dirfname), ".") - 1))
fileExt = Mid((Dirfname), (InStr(1, (Dirfname), ".") + 1), 3)


**If (InStr(1, fName, wkvalue) > 0 And wkvalue <> "") Then ** 'checking lookup val in instr
        If (Trim(UCase(fileExt)) = "PDF" Or Trim(UCase(fileExt)) = "TIF") Then
                                Cells(recnum, 2).Value = "Yes"
                                'col = col + 1
                                ws.Hyperlinks.Add Anchor:=Cells(recnum, (col + 1)), _
                                Address:=SourceFolderName & "\" & Dirfname
                                col = col + 1
                                'Else: Cells(recnum, 2).Value = "No"
        End If
  End If

請告知在這種情況下可以做什么。

您可以使用正則表達式為您提供幫助。 我還不太熟練,但是這是一個相對簡單的案例。 這是改編自tmehta.com/regexp的函數,可以與文件夾中文件名的迭代結合使用:

Function RegExpFind(FindIn, FindWhat As String, _
                    Optional IgnoreCase As Boolean = False) As Variant

    Dim i As Long
    Dim rslt() As String

    '// Using Late Binding here, use the commented types if you've added
    '// the "Microsoft VBScript Regular Expressions" reference
    Dim RE As Object 'RegExp
    Dim allMatches As Object 'MatchCollection
    Dim aMatch As Object 'Match

    '// Use "Set RE = New RegExp" if using the VBScript reference        
    Set RE = CreateObject("vbscript.regexp")

    RE.Pattern = FindWhat
    RE.IgnoreCase = IgnoreCase
    RE.Global = True
    Set allMatches = RE.Execute(FindIn)

    '// check if we've found anything, if not return a single element array
    '// containing an empty string. If we've found something return at least 
    '// at least a single element array containing the matched expressions
    If allMatches.Count = 0 Then
        ReDim rslt(0 To 0)
        rslt(0) = ""
    Else
        ReDim rslt(0 To allMatches.Count - 1)
        For i = 0 To allMatches.Count - 1
            rslt(i) = allMatches(i).Value
        Next i
    End If

    RegExpFind = rslt

End Function

您將需要傳入文件名作為FindIn參數,並在FindWhat參數中傳遞正則表達式模式"^4567" 僅當它出現在搜索字符串的開頭時,以這種方式使用它才返回4567 (作為返回數組中的第一個元素)。 如果需要,可以很容易地回收此功能,以與其他搜索一起使用。

假設您要進行匹配的條件是4567之前的字符(如果有)

i = InStr(1, fName, wkvalue)
if i > 0 and wkvalue <> "" Then
    ch = " "
    if i > 1 then
        ch = mid(fName, i - 1, 1)
    end if
    if ch = " " then 
        ...

您沒有描述為什么應該拒絕第一個文件名,但是我認為是因為它在wkvalue之前和/或之后有一個數字(0-9),因此“ 4567”不是整數。 在這種情況下,這將起作用:

    charBefore = ""
    charAfter = ""
    pos = InStr(fName(i), wkvalue)
    If pos = 1 Then
        ' It's at the beginning of the filename.
        ' Get character after the number.
        charAfter = Mid(fName(i), pos + Len(wkvalue), 1)
    ElseIf pos > 1 Then
        ' It's not at the beginning of the filename
        ' Get characters before and after the number.
        charBefore = Mid(fName(i), pos - 1, 1)
        charAfter = Mid(fName(i), pos + Len(wkvalue), 1)
    Else
        ' Number not found.
    End If
    ' Could add another ElseIf to check if it's at the end of the filename.

    If pos > 0 And wkvalue <> "" _
        And Not charBefore Like "#" And Not charAfter Like "#" Then
            ' Number found and not preceded or followed by a digit (0-9).
            ' Do your thing.
    End If

暫無
暫無

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

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