删除子串,但保留格式

问题描述:

我有一小段代码将从细胞去除串垃圾,我选择了:删除子串,但保留格式

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 
+0

完美! ..............非常感谢你! – 2015-04-04 14:10:05

+0

GSerg,任何解释句子的机会? – 2015-04-05 07:46:57

+0

@TobyAllen嗯,这是很自我解释 - 你找到单元格的“价值”字符串的位置,并操纵相应的[字符范围](http://*.com/a/11331709/11683),因为这就是你在Excel中保留字符级格式。 – GSerg 2015-04-05 10:34:17