如何在Excel中删除具有0值的单元格文本并在具有非零值的文本上启动新行?

huangapple go评论66阅读模式
英文:

How to remove in-cell text with 0 values and start a new line for the text with non-zero values in Excel?

问题

请帮助解决Excel中的以下问题。

一个单个单元格中的原始文本值:ABCD 0% #@x; EDFG 0% #@x; HIGK 5% #@x; LMNO 50% #@x; PQRS 90%; TUVW 0% #@x;

目标文本值将为:
HIGK 5%
LMNO 50%
PQRS 90%

注意:#@x必须被删除,任何0%的百分比和其前面的文本必须被删除,任何非0%的百分比和其前面的文本必须被显示,结果显示多行但仍在一个单元格内。

我尝试将单元格拆分成6列,使用分隔符;然后转置这6列为6行,然后进一步拆分每行的文本和百分比数字,然后删除0%的行,然后合并剩余的非零行。但原始单元格是一个包围着许多其他单元格的单元格,转置步骤会影响到周围的单元格。

希望有人可以使用宏VBA脚本来解决这个问题。非常感谢!

英文:

Please help resolve the below issues in Excel.

The original text value in one single cell: ABCD 0% #@x; EDFG 0% #@x; HIGK 5% #@x; LMNO 50% #@x; PQRS 90%; TUVW 0% #@x;

The target text value will be:
HIGK 5%
LMNO 50%
PQRS 90%

Note:#@x have to be removed, any 0% percentage and the text before it have to be removed, any non-0% percentages and the text before it have to be displayed, the result shows multiple rows but still within one single cell.

I tried to split the single cell into 6 columns by using delimiter ; then transpose the 6 columns to 6 rows, then further split the text and percentage numbers for each row, then delete the rows with 0%, and then merge the remaining non-zero rows. But the original cell is a single cell with a lot of other cells surrounded, transposing steps affect the surrounded cells.

Hope anyone could help with this issue by using Macro VBA script. Thanks a lot!

答案1

得分: 2

Formula in A3:

Excel ms365:

=LET(x,TRIM(TEXTSPLIT(A1,{" #@x;",";"},,1)),TEXTJOIN(CHAR(10),,IF(-TEXTAFTER(x," ",-1)<0,x,"")))

Excel 2019:

{=TEXTJOIN(CHAR(10),,FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A1,"#@x",),";","</s><s>")&"</s></t>","//s[not(contains(.,' 0%'))][node()]"))}

VBA:

If a regular expressions and an UDF is what you are after, then try:

\s*([^;]+\b[1-9]\d*%)

See an online demo for more explanation.

Public Function RegexMatch(s As String) As String

Static RE As Object: If RE Is Nothing Then Set RE = CreateObject("vbscript.regexp")

RE.Pattern = "\s*([^;]+\b[1-9]\d*%)"
RE.Global = True

Set REMatches = RE.Execute(s)
If REMatches.Count > 0 Then
For Each Match In REMatches
If RegexMatch = "" Then
RegexMatch = Match.Submatches(0)
Else
RegexMatch = RegexMatch & Chr(10) & Match.Submatches(0)
End If
Next
Else
RegexMatch = vbNullString
End If

End Function

Call this through:

=RegexMatch(A1)

英文:

Without regex or vba:

如何在Excel中删除具有0值的单元格文本并在具有非零值的文本上启动新行?

Formula in A3:

Excel ms365:

=LET(x,TRIM(TEXTSPLIT(A1,{" #@x;",";"},,1)),TEXTJOIN(CHAR(10),,IF(-TEXTAFTER(x," ",-1)<0,x,"")))

Excel 2019:

{=TEXTJOIN(CHAR(10),,FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A1,"#@x",),";","</s><s>")&"</s></t>","//s[not(contains(.,' 0%'))][node()]"))}

VBA:

If a regular expressions and an UDF is what you are after, then try:

\s*([^;]+\b[1-9]\d*%)

See an online demo for more explaination.

Public Function RegexMatch(s As String) As String

Static RE As Object: If RE Is Nothing Then Set RE = CreateObject("vbscript.regexp")

RE.Pattern = "\s*([^;]+\b[1-9]\d*%)"
RE.Global = True

Set REMatches = RE.Execute(s)
If REMatches.Count > 0 Then
    For Each Match In REMatches
        If RegexMatch = "" Then
            RegexMatch = Match.Submatches(0)
        Else
            RegexMatch = RegexMatch & Chr(10) & Match.Submatches(0)
        End If
    Next
Else
    RegexMatch = vbNullString
End If

End Function

Call this through:

=RegexMatch(A1)

答案2

得分: 1

这应该可以工作:

Function removeZero(ByVal val As String) As String
    Dim intermediate As String
    For Each x In Split(val, "";"")
        If x <> """" Then
            intermediate = """"
            For Each y In Split(x, "" "")
                intermediate = intermediate & "" & y
                If Right(y, 1) = ""%" Then
                    If Left(y, Len(y) - 1) <> ""0" Then
                        removeZero = removeZero & Trim(intermediate) & vbLf
                        Exit For
                    End If
                End If
            Next y
        End If
    Next x
End Function

在查看JvdV的答案后,我意识到可能可以这样做:

Function removeZero(ByVal val As String) As String
    For Each x In Split(val, "";"")
        If x <> """" Then
            If InStr(x, "" 0%"") = 0 Then
                removeZero = removeZero & Replace(Trim(x), "" #@x"", """) & vbLf
            End If
        End If
    Next x
End Function

这两个函数都可以工作,但第一个函数有点多余。

结果:

如何在Excel中删除具有0值的单元格文本并在具有非零值的文本上启动新行?

必须启用换行文本,才能使换行工作。

*如果你使用的是较新版本的Excel,可能可以使用内置函数完成,但我使用的是Excel 2010,这是我能做的最好的了。

英文:

This should work:

Function removeZero(ByVal val As String) As String
    Dim intermediate As String
    For Each x In Split(val, ";")
        If x <> "" Then
            intermediate = ""
            For Each y In Split(x, " ")
                intermediate = intermediate & " " & y
                If Right(y, 1) = "%" Then
                    If Left(y, Len(y) - 1) <> "0" Then
                        removeZero = removeZero & Trim(intermediate) & vbLf
                        Exit For
                    End If
                End If
            Next y
        End If
    Next x
End Function

after looking at JvdV's answer, I realized I could probably just do this:

Function removeZero(ByVal val As String) As String
    For Each x In Split(val, ";")
        If x <> "" Then
            If InStr(x, " 0%") = 0 Then
                removeZero = removeZero & Replace(Trim(x), " #@x", "") & vbLf
            End If
        End If
    Next x
End Function

Both of these functions will work, the first one is a little silly though.

result:

如何在Excel中删除具有0值的单元格文本并在具有非零值的文本上启动新行?

Wrap text must be enabled for the linebreaking to work.

*if you have newer version of Excel, this might be possible with built-in functions but I'm on Excel 2010 so this is the best I can do.

huangapple
  • 本文由 发表于 2023年3月1日 14:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定