如果一定数量的单元格为空和/或包含一定值,则删除单词表中的一行



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

然而,我有一个有八列的表,其中前四列将始终包含文本。后四个包含链接到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

已解决

使用了以下代码:

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

最新更新