基于空單元格由Chr(7)跟隨的段落標(biāo)記組成,可以使用Range對(duì)象檢測(cè)空單元格,代碼如下:
Sub CheckTableCells()
Dim rngCell As Cell
Dim rngRow As Row
For Each rngRow In Selection.Tables(1).Rows
For Each rngCell In rngRow.Cells
If rngCell.Range.Text = Chr(13) & Chr(7) Then
MsgBox '第' & rngCell.RowIndex & '行,第' & rngCell.ColumnIndex & '列為空.'
End If
Next rngCell
Next rngRow
使用Range變量,將其設(shè)置為標(biāo)記每個(gè)單元格的區(qū)域,移動(dòng)到區(qū)域的末端,使單元格末端標(biāo)記和段落標(biāo)記不包含在區(qū)域中(要執(zhí)行此操作,只需將其移動(dòng)一個(gè)字符),并獲取區(qū)域內(nèi)的文本。如果單元格為空,則區(qū)域內(nèi)沒(méi)有文本。代碼如下:
Sub CheckTableCells1()
Dim rngCell As Cell
Dim rngRow As Row
Dim rngRange As Range
For Each rngRow In Selection.Tables(1).Rows
For Each rngCell In rngRow.Cells
Set rngRange = rngCell.Range
rngRange.End = rngRange.End - 1
If Len(rngRange.Text) = 0 Then
MsgBox '第' & rngCell.RowIndex & '行,第' & rngCell.ColumnIndex & '列為空.'
End If
Next rngCell
Next rngRow
下面的代碼與方法1類似,只是在給出空單元格信息之前先選擇該單元格。
Sub CheckTableCells2()
Dim rngCell As Cell
Dim rngRow As Row
Dim rngRange As Range
For Each rngRow In Selection.Tables(1).Rows
For Each rngCell In rngRow.Cells
If rngCell.Range.Text = Chr(13) & Chr(7) Then
rngCell.Select
MsgBox '第' & rngCell.RowIndex & '行,第' & rngCell.ColumnIndex & '列為空.'
End If
Next rngCell
Next rngRow
End Sub
聯(lián)系客服