首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Excel中调用文本字符串中的正确数字?

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

Stack Overflow用户
提问于 2020-05-02 17:07:16
回答 2查看 63关注 0票数 2

考虑以下文本字符串:

代码语言:javascript
运行
复制
(*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)-()。

附注,关于如何计算括号外的逗号数量,有什么想法吗?

如果能帮上忙,我会很感激的!

EN

回答 2

Stack Overflow用户

发布于 2020-05-02 19:43:56

如果您的Excel版本具有FILTERXML函数(Windows Excel 2013+),则可以使用:

代码语言:javascript
运行
复制
=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函数,那么VBA解决方案将是最好的。这取决于您的Excel版本,以及它是Windows还是MAC。

票数 2
EN

Stack Overflow用户

发布于 2020-05-02 21:38:59

计算字符数

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61556921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档