gpt4 book ai didi

vba - 删除子字符串但保留格式

转载 作者:行者123 更新时间:2023-12-02 10:27:30 25 4
gpt4 key购买 nike

我有一小段代码可以从我选择的单元格中删除子字符串垃圾:

Sub RemoveJunk()
Dim r As Range
For Each r In Selection
r.Value = Replace(r.Value, "junk", "")
Next r
End Sub

该代码有效,但它破坏了单元格中保留的字符的格式。所以如果我从以下开始:

enter image description here

我最终得到:

enter image description here



有什么方法可以避免扰乱保留字符的格式吗??

最佳答案

Sub RemovePreserveFormatting(ByVal Where As Range, Expression As String, Optional ByVal Compare As VbCompareMethod = VbCompareMethod.vbBinaryCompare)
Dim c As Range

For Each c In Where
Dim pos As Long: pos = 0

Do
pos = InStr(pos + 1, c.Value, Expression, Compare)
If pos = 0 Then Exit Do

c.Characters(pos, Len(Expression)).Delete
Loop
Next

End Sub

关于vba - 删除子字符串但保留格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29447125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com