簡體   English   中英

如何從 Excel 中的文本字符串中調用正確的數字?

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

考慮以下文本字符串:

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

我的目標是計算該字符串中每個單獨數字之前有多少個左括號(“(”)、括號外的逗號和連字符(例如,數字 10 前面的 3 個左括號、6 個左括號和前面的 3 個連字符) 11)。

我當前的解決方案是首先調用每個單獨數字前面的剩余文本字符串,只需=LEFT(A1,(FIND("1",A1,1)-1)) ,但碰巧 Excel 會調用出現的字符串在第一個“1”之前(即(*4, ),而不是從字符串中的實際數字“1”中調用剩余的字符串(即(*4,14)(7,15)(10,13)(9,12)-( )。

旁注,關於如何計算括號外逗號數量的任何想法?

幫助將不勝感激!

如果您有帶有FILTERXML function 的 Excel 版本(Windows Excel 2013+),您可以使用:

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

該公式創建了一個 xml ,其中s節點是括號內的內容,而t節點是其他所有內容。

如果您沒有FILTERXML function,最好使用 VBA 解決方案。 這取決於您的 Excel 版本,以及是 Windows 還是 MAC。

計數字符

在此處輸入圖像描述

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

暫無
暫無

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

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