【问题标题】:How can I extract 8 letter capital word from a sentence in a cell in excel?如何从excel单元格中的句子中提取8个字母的大写单词?
【发布时间】:2021-12-29 02:20:17
【问题描述】:

示例 - 单元格中的句子 A1WO# 000000.0 for ITSHTJ in Q0 20000_ITAI0006:TBD Replacement PO,我想提取 ITAI0006

我在UDF下面用过,但是没用。

Function findUcase(Rng As Range) As Boolean
    Dim Txt
    Dim Desc() As String

    Desc = Split(Rng.Value, " ")
    For Each Txt In Desc
        If (UCase(Txt) = Txt) Then
            findUcase = True
            Exit Function
        End If
    Next
End Function

【问题讨论】:

  • @braX 直到现在我已经尝试了下面的函数,但它不适用于 8 个字母的单词 Function findUcase(Rng As Range) As Boolean Dim Txt Dim Desc() As String Desc = Split(Rng. Value, " ") For Each Txt In Desc If (UCase(Txt) = Txt) Then findUcase = True Exit Function End If Next End Function
  • @SiddharthRout 我已经提到了我的方法,请帮忙
  • 你能再举几个例子来说明你的文本是什么样的吗?你想从中提取什么?
  • @shailzachaudhary 那么你的代码怎么没用呢?你想让函数返回字符串而不是布尔值吗?是否要求字符串必须是8个字符?
  • 如果有两个单词满足大写和8个字符的要求怎么办?

标签: excel vba excel-formula


【解决方案1】:

你可以试试:

B1中的公式:

=FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"_"," "),":"," ")," ","</s><s>")&"</s></t>","//s[string-length(.)=8][translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '')='']")

XPath 表达式过滤节点的位置:

  • [string-length(.)=8] - 所有 8 个字符长的子字符串;
  • [translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '')=''] - 确保当所有大写字母字符和数字都被替换为空时,节点也将不等于空。

请注意,一旦您制定了更具体的规则,公式方法会变得更加简单,例如:

  • 是否总是第一个冒号前的 8 个字符?
  • 是否总是在下划线和冒号之间?
  • 总是字母数字吗?

【讨论】:

    【解决方案2】:

    Regex 方法 - 这将返回给定单元格中的任意 8 个字符(A-Z 和数字)。

    Function findUcase(Rng As Range) As String
        If Rng.Cells.Count = 1 Then
            Dim regex As Object
            Set regex = CreateObject("vbScript.Regexp")
            
            regex.Global = False
            regex.Pattern = "[\dA-Z]{8}"
            If regex.Test(Rng.Value) Then
                findUcase = regex.Execute(Rng.Value)(0)
            End If
        End If
    End Function
    

    编辑 - 另一个版本将返回字符串中至少包含 A-Z 的字符串,并且还支持 8 个字符的格式,任意数量的 AZ,一个空格,然后是任意数量的数字。

    Function findUcase(Rng As Range) As String
        Const firstPattern As String = "[\dA-Z]{8}"
        Const secondPattern As String = "[A-Z]+ [\d]+"
        Const charLimit As Long = 8
        
        If Rng.Cells.Count = 1 Then
            Dim rngValue As String
            rngValue = Rng.Value
            
            Dim regex As Object
            Set regex = CreateObject("vbScript.Regexp")
            
            Dim matchColl As Object
            Dim match As Object
                
            regex.Global = True
            regex.Pattern = firstPattern
            If regex.test(rngValue) Then
                Set matchColl = regex.Execute(rngValue)
                
                For Each match In matchColl
                    If Not IsNumeric(match) Then
                        findUcase = match
                        Exit For
                    End If
                Next match
            Else
                regex.Pattern = secondPattern
                If regex.test(rngValue) Then
                    Set matchColl = regex.Execute(rngValue)
                    For Each match In matchColl
                        If Len(match) = charLimit Then
                            findUcase = match
                            Exit For
                        End If
                    Next match
                End If
            End If
        End If
        
        Set matchColl = Nothing
        Set regex = Nothing
    End Function
    

    【讨论】:

    • 大写单词超过8个字符怎么办?
    • 如果 OP 的数据集如图所示,则可能不会发生此问题。此时我不确定如何处理括号中的文本(如评论中的 OP 所示)
    • @RaymondWu 非常感谢,它有帮助,但它不适用于 WO#12344050.0 等 ENG ID ABEFPQRS 的句子:开始日期 5/4/2021 里程碑,代码返回数值,即 12344050 以及如何在正则表达式方法中包含空格,因为我要提取更多的 8 个字母字符,例如:CD 00001、ASI 1234
    • @shailzachaudhary 似乎您既没有正确说明您的标准,也没有给出足够多的不同排列示例。通过在正则表达式中包含空格,您将面临更多误报。 WO# 000000.0 for ITSHTJ in Q0 20000_ITAI0006:TBD Replacement PO 将返回“ ITSHTJ ”,因为它也满足条件。 Subcontracted consulting services for Acrylic major ltd. (XYWRTVSD) - Custom CLOUD AA 相同 - 返回“ CLOUD A ”。
    • @shailzachaudhary 我已经用另一个版本更新了答案,请尝试编辑后的版本。
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2014-07-11
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多