【问题标题】:deleting a row in word table if a certain number of cells are empty and/or contain a certain value如果一定数量的单元格为空和/或包含某个值,则删除单词表中的一行
【发布时间】:2022-01-06 19:23:46
【问题描述】:

我有这段代码(附在下面),如果所有单元格行都是空的,它将删除单词表中的行。

但是,我有一个包含八列的表格,其中前四列将始终包含文本。后四个包含链接到 Excel 文档的数据。

问题是;如何让代码搜索后四个(数据)列,如果列为空和/或包含值(例如零),则删除该行。

任何帮助将不胜感激。

提前致谢

    Sub DeleteBlankRowsAndTablesInATable()
  Dim objCell As Cell
  Dim nRowIndex As Integer, nRows As Integer, nColumns As Integer, nColumnIndex As Integer
  Dim varCellEmpty As Boolean
 
  Application.ScreenUpdating = False
 
  If Selection.Information(wdWithInTable) = False Then
    MsgBox ("Put cursor inside a table first!")
    Exit Sub
  Else
    With Selection.Tables(1)
      nRows = .Rows.Count
      For nRowIndex = nRows To 1 Step -1
        varCellEmpty = True
        For Each objCell In .Rows(nRowIndex).Cells
          If Len(objCell.Range.Text) > 2 Then
            varCellEmpty = False
            Exit For
          End If
        Next objCell
          If varCellEmpty = True Then
            .Rows(nRowIndex).Delete
          End If
      Next nRowIndex
 

    End With
  End If

  Set objCell = Nothing

  Application.ScreenUpdating = True
End Sub

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    已解决

    使用了以下代码:

    Sub DeleteBlankRowsAndTablesInATable()
    Dim objCell As Range
    Dim nRowIndex As Integer, nRows As Integer, nColumns As Integer, nColumnIndex As Integer
    Dim varCellEmpty As Boolean
    
        Application.ScreenUpdating = False
    
        If Selection.Information(wdWithInTable) = False Then
            MsgBox ("Put cursor inside a table first!")
            Exit Sub
        Else
            With Selection.Tables(1)
                nRows = .Rows.Count
                For nRowIndex = nRows To 1 Step -1
                    varCellEmpty = True
                    For nColumns = 5 To .Columns.Count
                        Set objCell = .Rows(nRowIndex).Cells(nColumns).Range
                        objCell.End = objCell.End - 1
                        If Len(objCell) > 0 And Not objCell.Text = "0" Then
                            varCellEmpty = False
                            Exit For
                        End If
                    Next nColumns
                    If varCellEmpty = True Then .Rows(nRowIndex).Delete
                Next nRowIndex
            End With
        End If
        Set objCell = Nothing
        Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 2022-07-05
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多