简体   繁体   中英

How do I recall the correct number from a text string in Excel?

Consider the following text string:

(*4,14)(7,15)(10,13)(9,12)-(1,8)(2,6)-5,3-11

My goal is to count how many left brackets ("("), commas outside brackets, and hyphens before each individual number in this string (eg, 3 left brackets in front of the number 10, 6 left brackets and 3 hyphens in front of 11).

My current solution is to first recall the remaining text string in front of each individual number, simply =LEFT(A1,(FIND("1",A1,1)-1)) , but it happens that Excel will recall the string appeared before the first "1" (ie, (*4, ), instead of recalling the remaining string from the actual number "1" in the string (ie, (*4,14)(7,15)(10,13)(9,12)-( ).

Side note, any idea on how to count the number of commas that are outside of brackets?

Help would be much appreciate!

If you have a version of Excel with the FILTERXML function (Windows Excel 2013+), you can use:

=SUM(LEN(FILTERXML("<t>" & SUBSTITUTE(SUBSTITUTE(A1,"(","<s>"),")","</s>") & "</t>","//t")))- LEN(SUBSTITUTE(FILTERXML("<t>" & SUBSTITUTE(SUBSTITUTE(A1,"(","<s>"),")","</s>") & "</t>","//t"),",",""))

The formula creates an xml where the s nodes are what's included inside the parentheses, and the t node is everything else.

If you don't have the FILTERXML function, a VBA solution would be best. Which depends on your version of Excel, and whether it is Windows or MAC.

Count Chars

在此处输入图像描述

Option Explicit

Function countChars(SourceString As String, SourceNumber As Variant, _
  CountChar As String, Optional countRight As Boolean = False) As Long

    Dim NumberDouble As Double
    Dim NumberString As String
    Dim NumberLength As Long
    Dim StringLength As Long
    Dim CurrentStart As Long
    Dim CurrentFound As Long
    Dim i As Long
    Dim isFound As Boolean

    StringLength = Len(SourceString)

    If VarType(SourceNumber) = 8 Then
        If Not IsNumeric(SourceNumber) Then _
          Exit Function   ' SourceNumber is not numeric.
    End If
    NumberDouble = Val(SourceNumber)
    If NumberDouble <> Int(NumberDouble) Then _
      Exit Function       ' SourceNumber is not an integer.
    NumberString = CStr(NumberDouble)
    NumberLength = Len(NumberString)

    CurrentStart = 1
    Do
        CurrentFound = InStr(CurrentStart, SourceString, NumberString)
        GoSub checkNumber
        If isFound Then
            GoSub countTheChars
            Exit Do
        End If
        CurrentStart = CurrentFound + 1
    Loop Until CurrentFound = 0

Exit Function

countTheChars:  ' Can be written better.
    If Not countRight Then
        For i = 1 To CurrentFound - 1
            If Mid(SourceString, i, 1) = CountChar Then
                countChars = countChars + 1
            End If
        Next i
    Else
        For i = CurrentFound + 1 To StringLength
            If Mid(SourceString, i, 1) = CountChar Then
                countChars = countChars + 1
            End If
        Next i
    End If

checkNumber:  ' Check for adjacent numbers.
   Select Case CurrentFound
       Case 0: Exit Function  ' NumberString (initially) not found.
       Case 1                 ' NumberString found at the beginning.
           isFound = Not _
             IsNumeric(Mid(SourceString, CurrentFound + NumberLength, 1))
       Case StringLength - NumberLength + 1   ' NumberString found at the end.
           isFound = Not _
             IsNumeric(Mid(SourceString, CurrentFound - 1, 1))
       Case Else               ' NumberString found in the middle.
           isFound = Not _
             IsNumeric(Mid(SourceString, CurrentFound + NumberLength, 1)) _
             And Not IsNumeric(Mid(SourceString, CurrentFound - 1, 1))
   End Select
Return

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM