简体   繁体   English

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

[英]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).我的目标是计算该字符串中每个单独数字之前有多少个左括号(“(”)、括号外的逗号和连字符(例如,数字 10 前面的 3 个左括号、6 个左括号和前面的 3 个连字符) 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)-( ).我当前的解决方案是首先调用每个单独数字前面的剩余文本字符串,只需=LEFT(A1,(FIND("1",A1,1)-1)) ,但碰巧 Excel 会调用出现的字符串在第一个“1”之前(即(*4, ),而不是从字符串中的实际数字“1”中调用剩余的字符串(即(*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:如果您有带有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"),",",""))

The formula creates an xml where the s nodes are what's included inside the parentheses, and the t node is everything else.该公式创建了一个 xml ,其中s节点是括号内的内容,而t节点是其他所有内容。

If you don't have the FILTERXML function, a VBA solution would be best.如果您没有FILTERXML function,最好使用 VBA 解决方案。 Which depends on your version of Excel, and whether it is Windows or MAC.这取决于您的 Excel 版本,以及是 Windows 还是 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA:如何从字符串中提取数字? - Excel VBA : How do I extract number from a string? 如何组合 SUMIF 的 Excel 公式并在文本字符串中查找数字? - How do I combine the Excel formulas for a SUMIF and find number in text string? 如何使用Python将数据从MySQL数据库保存到Excel文件而不是数字 - How do I save data from MySQL db to excel file as Number not text using Python Excel:如何通过包含文本查找单元格编号并返回它 - Excel: how do i lookup a cell number by containing text and return it 如何将数字作为字符串写入Excel文件? - How do I write a number as a string to an Excel file? 如何在Excel中的第一个数字周围拆分/解析字符串 - How do I split/parse a string around the first number in Excel 从一串文本中提取数字 - Excel - extract number from a string of text - Excel 在Excel中,如何将小数转换为“ 234.5”之类的文本,而将整数转换为“ 234”之类的文本? - In Excel, how do I convert a fractional number to text like “234.5” but a whole number to text like “234”? 如何从VBA Microsoft Excel中的字符串中删除文本 - How do you remove text from a string in VBA Microsoft Excel 如何将 Excel 中的文本提取到文本编辑中,在 Excel 中查找文本并替换为文本编辑? 自动化 - How do I extract Text from Excel into Text Edit, find text in Excel and replace into Text Edit? Automation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM