如果检测范围为空(Detect if range is empty)

2019-06-24 23:19发布

我要检查,如果在Excel中的一个范围内是空的。

我如何在VBA代码写的:

If Range("A38":"P38") is empty

Answer 1:

发现从我的意见的解决方案。

Sub Empty()
    If WorksheetFunction.CountA(Range("A38:P38")) = 0 Then
        MsgBox "Empty"
    Else
        MsgBox "Not Empty"
    End If
End Sub


Answer 2:

如果变量未初始化或显式设置为空的IsEmpty返回True; 否则,返回False。 如果表达式包含一个以上的变量假总是返回。 为IsEmpty只返回变种有意义的信息。 ( https://msdn.microsoft.com/en-us/library/office/gg264227.aspx )。 所以,你必须单独检查范围内的每一个细胞:

    Dim thisColumn as Byte, thisRow as Byte

    For thisColumn = 1 To 5
        For ThisRow = 1 To 6
             If IsEmpty(Cells(thisRow, thisColumn)) = False Then
                 GoTo RangeIsNotEmpty
             End If
        Next thisRow
    Next thisColumn
    ...........
    RangeIsNotEmpty: 

当然,这里不是与COUNTA函数计算不为空细胞的解决方案更多的代码,但如果至少有一个不为空单元格中发现goto语句interupt循环,做你的代码运行得更快,特别是如果范围大,需要检测这种情况。 另外这个代码对我来说更容易理解它在做什么,不是用Excel COUNTA函数,而不是VBA函数。



Answer 3:

Dim M As Range

    Set M = Selection

If application.CountIf(M, "<>0") < 2 Then
    MsgBox "Nothing selected, please select first BOM or Next BOM"
Else

'Your code here

End If

从以往的经验我刚刚得知你可以这样做:

If Selection.Rows.Count < 2 
Then End If`

稍后作出了澄清一点(现在我的工作)



Answer 4:

如果你发现自己在一个情况下这是绝对必要的,你遍历一个范围,而不是使用每个单元CountA ,那么它的速度更快,首先,其范围为,而不是循环对数组的值的数组和循环在许多范围转换/细胞。

Function IsRangeEmpty(ByVal rng As Range) As Boolean

    'Converts a range to an array and returns true if a value is found in said array

    Dim area As Range
    For Each area In rng.areas

        Dim arr As Variant
        arr = area.value

        For i = LBound(arr, 2) To UBound(arr, 2)        'columns
            For j = LBound(arr, 1) To UBound(arr, 1)    'rows

                'if cell is not empty then
                If Len(Trim(arr(j, i))) > 0 Then
                    IsRangeEmpty = False
                    Exit Function
                End If

            Next j
        Next i

    Next area

    IsRangeEmpty = True

End Function

如何使用它例如:

Sub Test()
    Debug.Print IsRangeEmpty(Range("A38:P38"))
End Sub

如果Range("A38:P38")是空的,将打印True ; 否则它会打印False



Answer 5:

Dim cel As Range, hasNoData As Boolean

    hasNoData = True
    For Each cel In Selection
        hasNoData = hasNoData And IsEmpty(cel)
    Next

这将返回True如果没有单元Selection包含任何数据。 对于一个特定的范围内,刚刚替补RANGE(...)Selection



Answer 6:

另一种可能的解决方案。 计数空细胞,并从细胞的总数中减去该值

Sub Emptys()

Dim r As range
Dim totalCells As Integer

'My range To check'
Set r = ActiveSheet.range("A1:B5")

'Check for filled cells'
totalCells = r.Count- WorksheetFunction.CountBlank(r)


If totalCells = 0 Then
    MsgBox "Range is empty"
Else
    MsgBox "Range is not empty"
End If

End Sub


Answer 7:

这只是一个轻微的除了@TomM's 答案 / A简单的函数来检查,如果你选择的单元格为空

Public Function CheckIfSelectionIsEmpty() As Boolean
   Dim emptySelection As Boolean:emptySelection=True
   Dim cell As Range
   For Each cell In Selection
       emptySelection = emptySelection And isEmpty(cell)
       If emptySelection = False Then
          Exit For
       End If
   Next
   CheckIfSelectionIsEmpty = emptySelection
End Function


文章来源: Detect if range is empty
标签: excel vba range