【问题标题】:Find a string and delete row in table在表中查找字符串并删除行
【发布时间】:2020-10-14 14:53:12
【问题描述】:

如果存在字符串,我正在尝试从 excel 中执行 VBA 宏以删除 Word 文档中的一行。

For i = startItem To endItem
Dim msWord      As Object
Set msWord = CreateObject("Word.Application")

With msWord
    .Visible = TRUE
    .Documents.Open getSetting("PTC TEMPLATE")        'path of the template in msword format
    .Activate
    'Remove TEST ROW
    'LOOP TEST TO REMOVE
    Dim DirArray        As Variant
    DirArray = ThisWorkbook.Sheets("valveList").ListObjects("valveList").HeaderRowRange.value
    
    For Each element In DirArray
        If element Like "*TEST*" Then
            Debug.Print element & "--> " & Range("valveList[" & element & "]")(i).value
            If Range("valveList[" & element & "]")(i).value = "NO" Then
                .ActiveDocument.Select
                With .Selection.Range.Find
                    .ClearFormatting
                    .Replacement.ClearFormatting
                    .Text = Range("valveList[" & element & "]")(i).value        'Find all strings in col A
                    .Forward = TRUE
                    .Wrap = wdFindStop
                    .MatchCase = FALSE
                    .MatchWholeWord = FALSE
                    .Execute
                    If .Found = TRUE Then
                        
                        .Selection.Rows.Delete
                    End If
                    
                End With
                
            End If
            
        End If
    Next element
    'End REMOVE TEST ROW

这里有个问题,不知道怎么引用找到的字符串,删除字符串所属表的行。

我对VBA不是很熟悉,如果有人可以修改我的代码并解释如何解决这个问题,我将不胜感激

【问题讨论】:

    标签: excel vba ms-word find


    【解决方案1】:

    您的代码存在许多问题,包括重复启动 Word 和打开您正在修改的文档的新副本、使用具有后期绑定的 Word 常量以及使用不合格的 Range 引用。尝试以下方式:

    Sub Demo()
    Dim msWord  As Object, wdDoc As Object, xlSht As Worksheet, DirArray As Variant
    Set xlSht = ThisWorkbook.Sheets("valveList")
    DirArray = xlSht.ListObjects("valveList").HeaderRowRange.Value
    Set msWord = CreateObject("Word.Application")
    With msWord
      .Visible = True
      .ScreenUpdating = False
      Set wdDoc = .Documents.Open(GetSetting("PTC TEMPLATE"))        'path of the template in msword format
      For i = startItem To endItem
        For Each element In DirArray
          If element Like "*TEST*" Then
            If xlSht.Range("valveList[" & element & "]")(i).Value = "NO" Then
              With wdDoc.Range
                With .Find
                  .ClearFormatting
                  .Replacement.ClearFormatting
                  .Text = xlSht.Range("valveList[" & element & "]")(i).Text        'Find all strings in col A
                  .Forward = True
                  .Wrap = 0 'wdFindStop
                  .MatchCase = False
                  .MatchWholeWord = False
                End With
                Do While .Find.Execute
                  If .Information(12) = True Then 'wdWithInTable
                    .Rows(1).Delete
                  End If
                  .Collapse 0 'wdCollapseEnd
                Loop
              End With
            End If
          End If
        Next element
      Next i
      .ScreenUpdating = True
    End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多