检测范围是否为空

时间:2012-05-30 06:37:07

标签: excel vba range

我想检查Excel中的范围是否为空。

如何使用VBA代码编写:

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

8 个答案:

答案 0 :(得分:48)

从我收到的评论中找到了解决方案。

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

答案 1 :(得分: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`

稍后提供澄清(现在我正在工作)

答案 2 :(得分:3)

如果变量未初始化,或者显式设置为Empty,则IsEmpty返回True;否则,返回False。如果expression包含多个变量,则始终返回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可以中断循环并且更快地执行代码,特别是如果范围很大并且您需要检测这个案例。此代码对我来说比使用不是VBA函数的Excel CountA函数更容易理解它正在做什么。

答案 3 :(得分:2)

如果您发现自己绝对需要遍历某个范围内的每个单元格而不是使用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

答案 4 :(得分:1)

Dim cel As Range, hasNoData As Boolean

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

如果True中的单元格中没有任何数据,则会返回Selection。对于特定范围,只需将RANGE(...)替换为Selection

答案 5 :(得分:0)

另一种可能的解决方案。计算空单元格并从单元格总数中减去该值

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

答案 6 :(得分:0)

这只是@TomM's https://www.tutorialspoint.com/python3/dictionary_update.htm /的一小部分/一个简单的检查功能 如果您选择的单元格为空

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

答案 7 :(得分:0)

单行效果更好,恕我直言:

docker-entrypoint.sh

在这种情况下,它将评估范围E10:E14是否为空。

相关问题