Vba 如何使此代码更好/更快?
我有一组工作代码,可以从单词表中删除单词,并重新设置表格的大小。我知道我在代码中重复,所以我想让它更精简,希望通过这样做,代码运行起来会更快一点 我完全不懂vba,所以我浏览了网页,并一起访问了工作代码。我正在处理的表是150行,10列,但是这些行每周都会改变Vba 如何使此代码更好/更快?,vba,ms-word,Vba,Ms Word,我有一组工作代码,可以从单词表中删除单词,并重新设置表格的大小。我知道我在代码中重复,所以我想让它更精简,希望通过这样做,代码运行起来会更快一点 我完全不懂vba,所以我浏览了网页,并一起访问了工作代码。我正在处理的表是150行,10列,但是这些行每周都会改变 Option Explicit Sub alterRota() Dim manager manager = "Manager" With Selection.Find .ClearF
Option Explicit
Sub alterRota()
Dim manager
manager = "Manager"
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = manager
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim bar
bar = "Bar"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = bar
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim kitchen
kitchen = "Kitchen"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = kitchen
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim lead
lead = "Lead"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = lead
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim cleaning
cleaning = "Cleaning"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = cleaning
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim floor
floor = "Floor"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = floor
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim timeoff
timeoff = "Time Off"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = timeoff
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim startoff
startoff = "04:00 - 00:00"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = startoff
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim endoff
endoff = "00:00 - 04:00"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = endoff
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim linebreaks
linebreaks = "^p"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = linebreaks
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim employee
employee = "Employee"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = employee
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim tr As Row
For Each tr In ActiveDocument.Tables(1).Rows
tr.HeightRule = wdRowHeightExactly
tr.Height = 9
Next tr
End Sub
您的代码可以按如下方式重新编写
Option Explicit
Sub alterRota()
Dim searchFor As Variant
searchFor = Array("Manager", "Bar", "Kitchen", . . . ) 'add your other words accordingly
Dim i As Long
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = LBound(searchFor) To UBound(searchFor)
.Text = searchFor(i)
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
Next i
End With
Dim tr As Row
For Each tr In ActiveDocument.Tables(1).Rows
tr.HeightRule = wdRowHeightExactly
tr.Height = 9
Next tr
End Sub
您需要一个代码块作为一个过程来完成所有的查找工作,还需要一个文本数组来查找您在现有代码中使用的timeoff和bar,您可以循环调用该过程。这将大大简化它,因为对Find块的每次调用都会执行完全相同的操作,除了它要查找的特定文本之外。