如何引用所有非空单元格?

时间:2013-08-31 21:47:42

标签: excel-vba vba excel

我想引用所有非空单元格。到目前为止,我最好的尝试是:

ActiveSheet.SpecialCells(Not(xlCellTypeBlanks)).NumberFormat = "General"

但它不起作用。你知道怎么让这个工作吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

Sub cutaneous()
    Dim rNonEmpty As Range
    Dim rEmpty As Range, r As Range
    Set rEmpty = Cells.SpecialCells(xlCellTypeBlanks)
    Set rNonEmpty = Nothing

    For Each r In ActiveSheet.UsedRange
        If Intersect(r, rEmpty) Is Nothing Then
            If rNonEmpty Is Nothing Then
                Set rNonEmpty = r
            Else
                Set rNonEmpty = Union(r, rNonEmpty)
            End If
        End If
    Next r
    rNonEmpty.NumberFormat = "General"
    MsgBox rNonEmpty.Address
End Sub