如何从Excel中的文本字符串中调用正确的数字?

如何从Excel中的文本字符串中调用正确的数字?,excel,string,excel-formula,ms-office,countif,Excel,String,Excel Formula,Ms Office,Countif,考虑以下文本字符串: (*4,14)(7,15)(10,13)(9,12)-(1,8)(2,6)-5,3-11 我的目标是计算此字符串中每个数字前面的左括号(“”)、括号外的逗号和连字符数(例如,数字10前面的3个左括号、数字11前面的6个左括号和3个连字符) 我目前的解决方案是首先调用每个数字前面的剩余文本字符串,只需=LEFT(A1,(FIND(“1”,A1,1)-1)),但Excel将调用出现在第一个“1”(即(*4,)之前的字符串,而不是从字符串中的实际数字“1”中调用剩余字符串(即,

考虑以下文本字符串:

(*4,14)(7,15)(10,13)(9,12)-(1,8)(2,6)-5,3-11
我的目标是计算此字符串中每个数字前面的左括号(“”)、括号外的逗号和连字符数(例如,数字10前面的3个左括号、数字11前面的6个左括号和3个连字符)

我目前的解决方案是首先调用每个数字前面的剩余文本字符串,只需
=LEFT(A1,(FIND(“1”,A1,1)-1))
,但Excel将调用出现在第一个“1”(即
(*4,
)之前的字符串,而不是从字符串中的实际数字“1”中调用剩余字符串(即,
(*4,14)(7,15)(10,13)(9,12)-(

旁注,你知道如何计算括号外的逗号数吗


非常感谢您的帮助!

如果您有一个带有
FILTERXML
功能的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"),",",""))
=SUM(LEN(FILTERXML(“”&SUBSTITUTE(替换为(A1),(“,”),”)和“//t”))-LEN(替换为(FILTERXML(“&SUBSTITUTE(替换为(A1),(“,”),”),”)和“//t”),”,“,”)
该公式创建了一个xml,其中
s
节点是括号中包含的内容,而
t
节点是其他所有内容

如果您没有
FILTERXML
功能,最好使用VBA解决方案。这取决于您的Excel版本,以及它是Windows还是MAC。

Count Chars

选项显式
函数countChars(SourceString作为字符串,SourceNumber作为变量_
CountChar作为字符串,可选COUNTRRIGHT作为布尔值=False)作为长度
暗数双倍
作为字符串的Dim NumberString
暗数等长
长度和长度一样长
尽可能长的启动
我发现它很长
我想我会坚持多久
我们发现它是布尔型的
StringLength=Len(源字符串)
如果VarType(SourceNumber)=8,则
如果不是数字(SourceNumber),则_
退出函数的SourceNumber不是数字。
如果结束
NumberDouble=Val(SourceNumber)
如果是NumberDouble Int(NumberDouble),那么_
退出函数的SourceNumber不是整数。
NumberString=CStr(NumberDouble)
NumberLength=Len(NumberString)
CurrentStart=1
做
CurrentFound=InStr(CurrentStart、SourceString、NumberString)
GoSub支票号码
如果我找到了
戈苏布伯爵
退出Do
如果结束
CurrentStart=CurrentFound+1
循环直到CurrentFound=0
退出功能
countTheChars:“可以写得更好。
如果不是这样的话
对于i=1到CurrentFound-1
如果Mid(SourceString,i,1)=CountChar,则
countChars=countChars+1
如果结束
接下来我
其他的
对于i=CurrentFound+1到StringLength
如果Mid(SourceString,i,1)=CountChar,则
countChars=countChars+1
如果结束
接下来我
如果结束
checkNumber:'检查相邻的号码。
选择案例CurrentFound
案例0:未找到退出函数“NumberString(最初)”。
案例1“在开始处找到编号字符串”。
isFound=Not_
IsNumeric(中间(SourceString,CurrentFound+NumberLength,1))
Case StringLength-在末尾找到的NumberLength+1'NumberString。
isFound=Not_
IsNumeric(Mid(SourceString,CurrentFound-1,1))
案例中的“编号字符串”在中间找到。
isFound=Not_
IsNumeric(中间(SourceString,CurrentFound+NumberLength,1))_
而不是数字(Mid(SourceString,CurrentFound-1,1))
结束选择
返回
端函数

什么版本的Excel?VBA解决方案可以接受吗?google sheets regex公式可以接受吗?如果你不想在邮件中使用VBAThanks,你可以使用google sheets!Ron:Mac 2017!Tom:我正在尝试看看,仅仅使用公式是否能实现这一点,因为我在使用VBA方面很弱,Rocky:我最好对regex进行更多调查!再次感谢Ron!成功了ks因为我现在正在使用Mac,刚刚从微软网站上查到Mac版本不提供FILTERXML()…现在是时候买一台PC了!@Bobby我想微软也没有为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