如何将多个查找和替换转换为更有效的循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将多个查找和替换转换为更有效的循环相关的知识,希望对你有一定的参考价值。

下面是我在一个更大的宏中间所拥有的东西。创建循环,特别是成功地将它们插入更大的宏是我不知道该怎么做。任何帮助表示赞赏

F列有一个标题。可以占用行的距离是动态的。当我这样做时,我选择了整个列而不是偏移1单元格向下控制向下移动选择只是因为输入足够小而无关紧要。现在输入量更大,并且减慢了一切。

 ' remove FIRST style tags from description *Add new ones here*
    Sheets("Columns").Select
    Columns("F:F").Select
    Selection.Replace What:="<span style*>", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    '2
        Sheets("Columns").Select
        Columns("F:F").Select
        Selection.Replace What:="<div style=*>", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    '3
        Sheets("Columns").Select
        Columns("F:F").Select
        Selection.Replace What:="<p style*>", Replacement:="<p>", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

...

    '18
        Sheets("Columns").Select
        Columns("F:F").Select
        Selection.Replace What:="<tbody>", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

以下是新代码

 Sub Sample()
Dim StartTime As Double
Dim SecondsElapsed As Double

'Remember time when macro starts
  StartTime = Timer
    Dim Ar(1 To 17) As String '~~> 4 to number of items
    Dim i As Long
    Dim Ar2(1) As String '~~> 1 to number of items
    Dim a As Long
    Ar(1) = "<span style*>"
    Ar(2) = "<div>"
    Ar(3) = "<div style=*>"
    Ar(4) = "<tbody>"
    Ar(5) = "</div>"
    Ar(6) = "<ul style=*>"
    Ar(7) = "<li style=*>"
    Ar(8) = "<table style*>"
    Ar(9) = "<col style*>"
    Ar(10) = "<tr style=*>"
    Ar(11) = "<td class=*>"
    Ar(12) = "<colgroup>"
    Ar(13) = "</colgroup>"
    Ar(14) = "</tbody>"
    Ar(15) = "</td>"
    Ar(16) = "</tr>"
    Ar(17) = "</table>"

    For i = 1 To UBound(Ar)
        Sheets("Columns").Columns(6).Replace What:=Ar(i), _
                                             Replacement:="", _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByRows, _
                                             MatchCase:=False, _
                                             SearchFormat:=False, _
                                             ReplaceFormat:=False
    Next i
    Ar2(1) = "<p style*>"
    For a = 1 To UBound(Ar2)
        Sheets("Columns").Columns(6).Replace What:=Ar2(a), _
                                             Replacement:="<p>", _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByRows, _
                                             MatchCase:=False, _
                                             SearchFormat:=False, _
                                             ReplaceFormat:=False
    Next a

    'Determine how many seconds code took to run
  SecondsElapsed = Round(Timer - StartTime, 2)

'Notify user in seconds
  MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
答案

将所有搜索项存储在一个数组中,然后在循环中替换它们。例如(UNTESTED)

Sub Sample()
    Dim Ar(1 To 4) As String '~~> 4 to number of items
    Dim i As Long

    Ar(1) = "<span style*>"
    Ar(2) = "<p style*>"
    Ar(3) = "<div style=*>"
    Ar(4) = "<tbody>"

    For i = 1 To Ubound(Ar)
        Sheets("Columns").Columns(6).Replace What:=Ar(i), _
                                             Replacement:="", _
                                             LookAt:=xlPart, _
                                             SearchOrder:=xlByRows, _
                                             MatchCase:=False, _
                                             SearchFormat:=False, _
                                             ReplaceFormat:=False
    Next i
End Sub

以上是关于如何将多个查找和替换转换为更有效的循环的主要内容,如果未能解决你的问题,请参考以下文章

Python:替换多个 for 循环、多个迭代器

结合两个代码片段?将用户输入的 Youtube url 转换为嵌入 url,然后将 iframe src 替换为转换后的 url

如何将自引用的复杂游标转换为更高效的 SQL 代码? CTE、交叉应用还是其他?

除非被特定标签包围,否则替换字符

Google 表格 – 查找和替换多个代码

如何将多个 Stream 合并为更高级别的 Stream?