根据条件删除单元格中的某些文本

时间:2020-04-19 01:12:32

标签: excel

enter image description here

我需要您的支持才能在B列中获取结果,基本上可以看到A单元格中的逗号和破折号(,&-)之间是否有5位数字,然后从,到-删除其余的文本如图所示,文本的内容必须保持不变。我正在寻找一个普通的Excel公式而不是VBA代码。预先感谢!

2 个答案:

答案 0 :(得分:3)

根据您的截屏,而不是您编写的内容,您似乎想保留aaaa-nnnn格式的子字符串,其中nnnnn代表四个或更多数字。如果您具有FILTERXMLTEXTJOIN函数,则可以使用以下公式:

=SUBSTITUTE(TEXTJOIN(",",TRUE,FILTERXML("<t><s>" & SUBSTITUTE(SUBSTITUTE(A1,"-",",-"),",","</s><s>") & "</s></t>","//s[number(.)<-999] /preceding::*[1] | //s[number(.)<-999]")),",-","-")
  • 在逗号和连字符上创建XML拆分(但保留连字符)
  • 构造一个xPath,该xPath同时选择值小于-999的数字节点之前的节点和该数字节点本身。
    • 数字来自保留连字符
  • 使用TEXTJOIN和逗号分隔符将值放回一起
    • 删除连字符前面的逗号。

enter image description here

如果您的Excel版本没有这些功能,则VBA或Power Query可能是更好的解决方案。

如果最终更喜欢VBA解决方案,我建议寻找满足您明显的带连字符子字符串标准的子字符串,其中右半部分是一个数字>999。如有必要,可以很容易地检查左侧是否全部为大写字母添加。

Option Explicit
Function getStr(S As String) As String
    Dim V, W
    Dim sTemp As String

V = Split(S, ",")
For Each W In V
    If Val(Split(W, "-")(1)) > 999 Then _
        sTemp = sTemp & "," & W
Next W

getStr = Mid(sTemp, 2)

End Function

答案 1 :(得分:1)

遗憾的是,不允许使用通配符

使用SEARCHREPLACE唯一可以想到的是以下公式:

=IF(ISNUMBER(SEARCH("????-, ",A1)),REPLACE(A1,SEARCH("????-, ",A1),7,""),IF(ISNUMBER(SEARCH(", ????-",RIGHT(A1,7))),REPLACE(A1,LEN(A1)-6,7,""),A1))

仅删除您要删除的字符串的第一个匹配项。

作为安慰,我提供了一个简单的VBA解决方案,该解决方案通过默认来删除5-character字符串中的所有", "-delimited子字符串。

在VBA(CTRL-F11)中,将新模块插入需要的工作簿中。在模块(可能是Module1)的代码表中,复制/粘贴以下代码:

Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:    From a delimited string, removes all sub strings containing
'             a specified number of characters and returns the remainder
'             of the string.
' Returns:    A string, if there are any substrings with a different number
'             of characters than the specified number of characters,
'             or "", if not.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FilterC(SourceValue As Variant, _
  Optional NumberOfCharacters As Long = 5, _
  Optional Delimiter As String = ", ") As String

    Dim vntS As Variant   ' Source Array
    Dim vntT As Variant   ' Target Array
    Dim i As Long         ' Source Array Elements Counter
    Dim iTA As Long       ' Target Array Elements Counter
    Dim strC As String    ' Current String

    ' Check if SourceValue is text.
    If VarType(SourceValue) <> vbString Then Exit Function
    ' Check if SourceValue is "". For a cell in Excel this refers to an empty
    ' cell or a cell with a formula evaluating to "".
    If SourceValue = "" Then Exit Function

    ' Initialize Target Array Elements Counter.
    iTA = -1
    ' Write SourceValue to elements of Source Array (using 'Split').
    vntS = Split(SourceValue, Delimiter)

    ' Loop through elements of Source Array.
    For i = 0 To UBound(vntS)
        ' Write current element in Source Array to Current String.
        strC = vntS(i)
        ' Check if the length of Current String is NOT equal
        ' to NumberOfCharacters.
        If Len(strC) <> 5 Then GoSub TargetArray
    Next

    ' If only 'NumberOfCharacters' character strings are found.
    If iTA = -1 Then Exit Function

    ' Write elements of Target Array to FilterC (using "Join").
    FilterC = Join(vntT, Delimiter)

Exit Function

' Write String to Target Array.
TargetArray:
    ' Increase Target Array Elements Counter.
    iTA = iTA + 1
    ' Check if Target Array Elements Counter is greater than 0 i.e. if
    ' there already are any elements in Target Array.
    If iTA > 0 Then
        ' All, except the first element.
        ReDim Preserve vntT(iTA)
    Else
        ' Only the first element.
        ReDim vntT(0)
    End If
    ' Write Current String to Target Array.
    vntT(iTA) = strC
    ' Note: Target Array Elements Counter (iTA) was initalized with -1, so when
    ' the first time the code redirects to TargetArray (Subroutine),
    ' iTA will be 0 and only this time run through the Else clause
    ' and finally write Current String to Target Array.
Return

End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

在Excel中,对于A1的结果,请使用以下公式:

=FilterC(A1)

这是标准公式的简短的默认行为:

=FilterC(A1,5,", ")
相关问题