Vba excel instr函数,搜索字符串以未知数字结尾

时间:2014-11-06 14:46:27

标签: vba excel-vba excel

我想搜索一个以数字结尾的字符串,但不知道它会是什么数字

所以我使用InStr功能

   InStr(Range("B" & row).Value), "text")

但现在的问题是,我正在搜索的内容可以是" text0"," text1"," text9",我不会&#39 ; t想要创建10次Instr函数来测试10个数字。

我正在寻找的是替换字符,就像你拥有#代表Acces中输入掩码中的任何给定数字一样。像这样的东西

InStr(Range("B" & row).Value), "text" &  #)

offcoarse这不会起作用,因为excel将此作为搜索" text#"并且不会将其解释为#是任何给定的数字。

编辑: 范围(" B"&行).Value将评估例如" 9S 279P3NOV / PDE NN1 PRS NO NVML"

我需要知道的是NN1在哪里,我可以提取它。

但是下一行可以评估为" 9S 2793NOV / PE NN12 REQ BANA" 所以我再次需要知道NN12在哪里,也注意到NN12改变之前的文本以及NN现在有2位数。

5 个答案:

答案 0 :(得分:3)

阅读以下问题后的评论

数字是随机的,我正在寻找的实际字符串总是以NN开头,所以找到的字符串可以是NN1,NN5或甚至是NN25。没有办法告诉数字是什么。

这是你在尝试什么?将LIKE与通配符一起使用。

试试这个

Sub Sample()
    Dim stringToTest(1 To 5) As String
    Dim i As Long

    stringToTest(1) = "Test01"
    stringToTest(2) = "Test01Test"
    stringToTest(3) = "123"
    stringToTest(4) = "01Test01"
    stringToTest(5) = "NNature1234"

    For i = 1 To 5
        If stringToTest(i) Like "NN*#" Then Debug.Print stringToTest(i)
    Next i
End Sub

从评论/最近编辑到问题的跟进

如果您的格式符合您在问题中所示,即会有空格然后尝试这个

Sub Sample()
    Dim s As String, stringToTest(1 To 2) As String
    Dim ar
    Dim i As Long, j As Long

    stringToTest(1) = "9S 279P3NOV/PDE NN1 PRS NO NVML"
    stringToTest(2) = "9S 2793NOV/PE NN12 REQ BANA"

    For i = 1 To 2
        s = stringToTest(i)
        If s Like "*NN*#*" And InStr(1, s, " ") Then
            ar = Split(s, " ")
            For j = LBound(ar) To UBound(ar)
                If ar(j) Like "NN*#" Then
                    Debug.Print ar(j)
                    Exit For
                End If
            Next j
        End If
    Next i
End Sub

<强>输出

NN1
NN12

答案 1 :(得分:0)

这个功能可以适合你吗?

If IsNumeric(Right(Range("B" & row).Value, 1)) Then
    MsgBox "It ends with a Number."
Else
    MsgBox "It does not end with a Number."
End If

答案 2 :(得分:0)

如果我理解正确,简单的循环可能会有所帮助:

Sub SearchNum()
    Dim i As Integer
    Dim strSource As String
    Dim boolNumFound As Boolean

    'Found flag
    numFound = False

    'Put source string to variable
    '(put here your range address)
    strSource = Range("B1").Value

    'Loop through your number range
    For i = 0 To 99
        If InStr(1, strSource, "text" & i) Then
            numFound = True
            MsgBox "text" & i
            Exit For
        End If
    Next i

End Sub

答案 3 :(得分:0)

昨天我遇到了类似的问题。我把答案给了我并编辑它以适合你的问题,但我不能100%的信用:-p。我相信这会让你得到你想要的东西。

    sub test()

    Dim sWords() As String
    Dim s As Variant
    Dim sResult As String


      sWords = Split(ActiveCell.Value, " ")
      For Each s In sWords
        If Left$(s, 2) = "NN" Then
            sResult = sResult & s
            msgbox sResult
            sResult = ""
        End if
      Next s

      end sub

答案 4 :(得分:0)

我认为这将比提供的其他解决方案快一些。 如所写,这不区分大小写,但是删除vbTextCompare将使其区分大小写。我已经测试过了,代码有效。

Function nnNumeric(ByVal textIn As String, Optional ByVal startPos As Long = 1) As Long
    'searches textIn for NN followed by a number; e.g. NN0, NN1, NN2, etc.
    'returns the position when found, otherwise returns #VALUE! error
    Dim i As Long
    i = InStr(startPos, textIn, "NN", vbTextCompare) 'remove vbTextCompare to make this case-sensitive
    Do While i > 0
        If IsNumeric(Mid(textIn, i + 2, 1)) Then
            nnNumeric = i
            Exit Function
        End If
        i = InStr(i + 1, textIn, "NN", vbTextCompare) 'remove vbTextCompare to make this case-sensitive
    Loop
    nnNumeric = CVErr(xlErrValue) '#VALUE! error
End Function
相关问题