如果空白则查找单元格位置

时间:2013-06-16 17:03:49

标签: excel excel-vba vba

我有一个Sud告诉我列是否有空白单元格。

如果它是空白的,有没有办法获得单元格位置,可能有数千行,也许有一两个空白单元格,即使你知道它在那里也容易错过。

由于

Sub CountBlankCellsComments()

Dim Lastrow As Long

Sheets("Comments").Select
With Sheets("Comments")
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

If WorksheetFunction.CountBlank(Range("A2:E" & Lastrow)) = 0 Then

            MsgBox "There Are (0) Blank Cells For ""Comments"" Sheet"
        Else

MsgBox "For Comments Sheet There are:" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("A2:A" & Lastrow)) & ") Blank Cells in Column A" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("B2:B" & Lastrow)) & ") Blank Cells in Column B" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("C2:C" & Lastrow)) & ") Blank Cells in Column C" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("D2:D" & Lastrow)) & ") Blank Cells in Column D" & vbCrLf & vbLf & _
"(" & WorksheetFunction.CountBlank(Range("E2:E" & Lastrow)) & ") Blank Cells in Column E"

End If

End Sub

3 个答案:

答案 0 :(得分:1)

Dim blanks As Range

With Worksheets("Comments")
  On Error Resume Next
  Set blanks = Application.Intersect(.Range("A2", .UsedRange.SpecialCells(xlCellTypeLastCell)), .Range("A:E")).SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0
End With

If blanks Is Nothing Then
  MsgBox "There Are (0) Blank Cells For ""Comments"" Sheet"
Else

  blanks.Select

  MsgBox "For Comments Sheet There are (" & blanks.Cells.Count & ") Blank Cells:" & vbNewLine & vbNewLine & _
    blanks.Address

End If

答案 1 :(得分:1)

只需在代码中添加以下内容:

MsgBox "BlankCells are:" & Range("A2:E" & Lastrow).SpecialCells(xlCellTypeBlanks).Address 

答案 2 :(得分:0)

这个怎么样:

Sub ShowBlanks()
    Dim data As Range, cl As Range, blanks As String

        Set data = Range("A1:E" & Range("A" & Rows.Count).End(xlUp).Row)
        blanks = vbNullString

        For Each cl In data
            If cl.Value = vbNullString Then
                blanks = blanks & cl.Address & vbCrLf & vbLf
            End If
        Next cl

        MsgBox "These cell are empty:" & vbCrLf & vbLf & blanks
End Sub