Excel VBA中的instr函数

Excel VBA中的instr函数,excel,vba,Excel,Vba,请帮忙,我需要你的帮助 说明: 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

请帮忙,我需要你的帮助

说明:

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
我设计了一个VBA代码,我想在其中比较目录中的字符串和文件名。在这个例子中,我使用了Instr函数,这只在3种情况下对我有帮助,但在动态方面没有帮助

解释:

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
如果str=4567并与文件名比较,则文件名可以是:
1.xs1234567.pdf
2.4567.pdf
3.4567(1).pdf
更新版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
Dirfname=finDir
fName=Mid((Dirfname),1,(InStr(1,(Dirfname),“)-1))
fileExt=Mid((目录名),(InStr(1,(目录名),“)+1),3)
**如果(InStr(1,fName,wkvalue)>0且wkvalue“”),则**检查InStr中的查找值
如果(Trim(UCase(fileExt))=“PDF”或Trim(UCase(fileExt))=“TIF”),则
单元格(recnum,2)。Value=“是”
'列=列+1
ws.Hyperlinks.Add锚点:=单元格(recnum,(col+1))_
地址:=SourceFolderName&“\”目录名
col=col+1
'Else:Cells(recnum,2)。Value=“否”
如果结束
如果结束

请告知如何处理这种情况。

假设匹配的标准是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 
        ...
i=InStr(1,fName,wkvalue)
如果i>0且wkvalue为“”,则
ch=“”
如果i>1,那么
ch=mid(fName,i-1,1)
如果结束
如果ch=”“,则
...

假设匹配的标准是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 
        ...
i=InStr(1,fName,wkvalue)
如果i>0且wkvalue为“”,则
ch=“”
如果i>1,那么
ch=mid(fName,i-1,1)
如果结束
如果ch=”“,则
...
您没有描述为什么应该拒绝第一个文件名,但我认为这是因为它在
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
charBefore=“”
charAfter=“”
位置=仪表(fName(i),wkvalue)
如果pos=1,则
'它位于文件名的开头。
'获取数字后的字符。
charAfter=Mid(fName(i),pos+Len(wkvalue),1)
如果位置>1,则
'它不在文件名的开头
'获取数字前后的字符。
charBefore=Mid(fName(i),位置-1,1)
charAfter=Mid(fName(i),pos+Len(wkvalue),1)
其他的
'找不到号码。
如果结束
'可以添加另一个ElseIf以检查它是否位于文件名的末尾。
如果位置>0且值为“”_
而不是像“##”这样的charBefore,也不是像“##”那样的charAfter
'找到编号,但前面或后面没有数字(0-9)。
“做你自己的事。
如果结束
您没有描述为什么应该拒绝第一个文件名,但我认为这是因为它在
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
charBefore=“”
charAfter=“”
位置=仪表(fName(i),wkvalue)
如果pos=1,则
'它位于文件名的开头。
'获取数字后的字符。
charAfter=Mid(fName(i),pos+Len(wkvalue),1)
如果位置>1,则
'它不在文件名的开头
'获取数字前后的字符。
charBefore=Mid(fName(i),位置-1,1)
charAfter=Mid(fName(i),pos+Len(wkvalue),1)
其他的
'找不到号码。
如果结束
'可以添加另一个ElseIf以检查它是否位于文件名的末尾。
如果位置>0且值为“”_
而不是像“##”这样的charBefore,也不是像“##”那样的charAfter
'找到编号,但前面或后面没有数字(0-9)。
“做你自己的事。
如果结束

您可以使用正则表达式来帮助您。我还不是很精通,但这是一个相对简单的案例。下面是一个经过修改的函数,您可以将其与文件夹中文件名的迭代结合使用:

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
参数中传递regexp模式“^4567”。仅当它出现在搜索字符串的开头时,以这种方式使用它将返回
4567
(作为返回数组中的第一个元素)。如果需要,此函数可以很容易地循环使用,以便与以后的其他搜索一起使用。

您可以使用正则表达式来帮助您。我还不是很精通,但这是一个相对简单的案例。下面是一个经过修改的函数,您可以将其与文件夹中文件名的迭代结合使用:

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
参数中传递regexp模式“^4567”。仅当它出现在搜索字符串的开头时,以这种方式使用它将返回
4567
(作为返回数组中的第一个元素)。如果需要,此功能可以很容易地循环使用,以便将来与其他搜索一起使用。

谢谢Hari,它现在正在工作。一旦我需要别的东西,我会告诉你的。非常感谢。谢谢Hari,它正在工作。一旦我需要别的东西,我会告诉你的。非常感谢。