在word中處理表格與在excel中處理表格是截然不同的。
如果要刪除word表格中的行,可以使用Table對象的Rows屬性返回具體的行,然后用Delete方法。
同理,如果要刪除word表格中的列,可以使用Table對象的Columns屬性返回具體的列,然后用Delete方法。
如果要判斷是否為空行或者空列,可以遍歷要判斷的行或者列,統計下空白單元格的數量是否與總行數或者總列數一致,如果一致,表示當前行或者列均為空,然后就可以用上述的方法刪除。
以下是批量刪除word表格中的空白列的代碼:
Sub QQ1722187970()
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Dim oT As Table
Dim oRng As Range
Dim oCol As Column
Dim oRow As Row
Dim oCell As Cell
With oDoc
For Each oT In .Tables
With oT
iRow = .Rows.Count
iCol = .Columns.Count
For j = iCol To 1 Step -1
n = 0
For i = iRow To 1 Step -1
Set oCell = .Cell(i, j)
'空白單元格含有兩個字符Chr(13) & Chr(7)
If oCell.Range.Text = Chr(13) & Chr(7) Then
n = n + 1
End If
Next i
If n = iRow Then
.Columns(j).Delete
End If
Next j
End With
Next
End With
End Sub
以下是批量刪除word表格中的空白行的代碼:
Sub QQ1722187970()
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Dim oT As Table
Dim oRng As Range
Dim oCol As Column
Dim oRow As Row
Dim oCell As Cell
With oDoc
For Each oT In .Tables
With oT
iRow = .Rows.Count
iCol = .Columns.Count
For i = iRow To 1 Step -1
n = 0
For j = iCol To 1 Step -1
Set oCell = .Cell(i, j)
'空白單元格含有兩個字符Chr(13) & Chr(7)
If oCell.Range.Text = Chr(13) & Chr(7) Then
n = n + 1
End If
Next j
If n = iCol Then
.Rows(i).Delete
End If
Next i
End With
Next
End With
End Sub
