从ms word表中删除包含特定单词的特定行



我在MS Word中有一个表,并试图从中删除一些特定的行。我有一个vba脚本,但它删除行从所有的表。我只想删除2号表中的行。下面是我的代码:

Sub DeleteRowWithSpecifiedText()
Selection.Find.ClearFormatting
With .Tables.Find
.Text = "Moderate"
.Wrap = wdFindContinue
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Rows.Delete
End If
Loop
End Sub

如何指定其中的表来删除包含Moderate的行在第三栏?我对VBA完全陌生。请帮我解决这个问题

更新需求

  1. 我需要删除列3中包含Moderate或Empty的所有行
  2. 我需要根据列3中的值对表进行排序,这些值是Critical, High, Moderate3.将cell3/row的颜色更改为临界,高:红色,中:橙色,低:黄色(如果可能)

我正在处理上述任务,但没有找到在MS Word中完成的合适方法。是否有任何可用的扩展或可以做任务或VBA更好的东西?非常感谢你的时间

脚本中的更多问题
如何删除Col3中没有内容的行,因为有medium和Empty?对不起,我太笨了,

例如:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, r As Long, Clr As Long, bFit As Boolean
Const StrFnd As String = "|Moderate|Empty"
Const MyColorOrange As Long = 41215
With ActiveDocument.Tables(2)
bFit = .AllowAutoFit
.AllowAutoFit = False
For r = 1 To UBound(Split(StrFnd, "|"))
Set Rng = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = Split(StrFnd, "|")(r)
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchCase = False
.MatchWholeWord = True
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Do
If .Cells(1).ColumnIndex = 3 Then .Rows(1).Delete
.Collapse wdCollapseEnd
Loop
End With
Next
For r = 2 To .Rows.Count
With .Cell(r, 3)
Select Case Split(.Range.Text, vbCr)(0)
Case "Critical": Clr = wdColorRed
Case "High": Clr = MyColorOrange
Case "Moderate": Clr = wdColorYellow: .Range.Text = "X"
Case "Low": Clr = wdColorBrightGreen: .Range.Text = "Y"
Case Else: Clr = wdColorAutomatic: .Range.Text = "Z"
End Select
.Row.Shading.BackgroundPatternColor = Clr
End With
Next
.Sort ExcludeHeader:=True, FieldNumber:=3, SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[XYZ]"
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Do
If .Cells(1).ColumnIndex = 3 Then
Select Case .Text
Case "X": .Text = "Moderate"
Case "Y": .Text = "Low"
Case "Z": .Rows(1).Delete
End Select
End If
.Collapse wdCollapseEnd
Loop
End With
.AllowAutoFit = bFit
End With
Application.ScreenUpdating = True
End Sub

对于长表,查找/替换应该比遍历所有行更快。

你们附加的说明是不明确的。您的第一个规范包括删除第3列中包含"Moderate"的所有行,但在规范2 &你说你想对这些行进行排序和阴影处理。规范3介绍了包含"Low"的单元格,但在规范2的分类要求中没有提到这些。也不清楚"临界"应该有什么样的阴影。按照如此不一致的规范进行编码是不可能的。如果您想保留'Moderate'行,请从: 中删除'|Moderate'
Const StrFnd As String = "|Moderate|Empty"

对于您的附加规范,仍然需要循环遍历所有剩余的表行。请参阅更新后的代码。我假设所有保留的行需要不同的阴影根据他们是否临界,高,中,或低。更改阴影规格(即Clr)以适应您的实际需求。

如果要仅对条件单元格而不是整行进行阴影处理,请更改:

.Row.Shading.BackgroundPatternColor = Clr

:

.Shading.BackgroundPatternColor = Clr

您可以非常简单直接地解决这个问题。试试这个。

sub deleteRowsWithModerate()
Dim T As Table
Dim r As row
'  Set your 2nd Table as the one to be worked on
Set T = ActiveDocument.Tables(2)
'iterate over all rows, check if the 3rd cell (= 3rd column) of each row has the desired String in it.
For Each r In T.Rows
If InStr(r.Cells(3).Range.text, "Moderate") Then r.Delete
Next
End sub

注意

If r.Cells(3).Range.text = "Moderate" then

不起作用,因为单元格的文本中总是有一个隐藏的占位符,所以单元格文本就像"moderate",杆(13),杆(7).

相关内容

最新更新