下面是我在制作的更大宏中间的内容。我不知道如何创建循环,尤其是将循环成功地插入到较大的宏中。感谢您的帮助
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
答案 0 :(得分:2)
将所有搜索项存储在一个数组中,然后循环替换它们。例如(未测试)
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