且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除Microsoft Excel中特定标记之间的文本

更新时间:2023-11-13 23:16:22

基于宏记录器:

  Sub Test()
'working for选择替换所有< *> section
Selection.Replace What:=&* *>,Replacement:=,LookAt:= xlPart,_
SearchOrder:= xlByRows,MatchCase:= False,SearchFormat:= False, _
ReplaceFormat:= False
End Sub

编辑尝试2,在OP的评论之后:

  Sub Attempt_second()
'用于选择替换所有< * GT; section
Selection.Replace what:=< * code> *< * / * code>,替换:=< code>< / code>,LookAt:= xlPart,_
SearchOrder:= xlByRows,MatchCase:= False,SearchFormat:= False,_
ReplaceFormat:= False
End Sub

它会将文本替换为< code>< / code> 删除其间的其他空格。


I have some text that goes like this:

Lorem ipsum dolor <code>sit amet, consectetuer adipiscing elit,</code> sed diam nonummy nibh euismod tincidunt ut <code>laoreet dolore magna</code> aliquam erat volutpat.

I am trying to remove everything between each pair of "code" tags. I wrote a function that works well when there is only one pair of tags per cell, but it doesn't take care of multiple instances. Here is the desired output:

Lorem ipsum dolor <code></code> sed diam nonummy nibh euismod tincidunt ut <code></code> aliquam erat volutpat.

How would you advise me to do?

Based on macro recorder:

Sub Test()
    'working for selection replacing all <*> sections
    Selection.Replace What:="<*>", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub

Edit attempt 2, after comments from OP:

Sub Attempt_second()
    'working for selection replacing all <*> sections
    Selection.Replace What:="<*code>*<*/*code>", Replacement:="<code></code>", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub

It will replace text into <code></code> removing additional spaces in between.