excel 检测范围是否为空

1mrurvl1  于 2022-11-18  发布在  其他
关注(0)|答案(8)|浏览(210)

检查Excel中的区域是否为空。
如何编写VBA代码:

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

kulphzqa1#

从我得到的评论中找到了解决办法。

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

mum43rcc2#

如果您发现自己处于不能使用CountA的情况下,那么首先将范围存储为数组,然后对数组的数据进行循环,这比单独对范围/单元格数据进行循环要快得多。

Function IsRangeEmpty(ByVal rng As Range) As Boolean
    
    ''Returns true if a value is found in parameter range.
    ''Converts parameter range to an array to check it quickly.
    
    'if rng has cells in it then
    If Not rng Is Nothing Then
    
        Dim area As Range
        For Each area In rng.Areas 'checks for range with multiple cell groups e.g., rng=Range("A1:B5,C6:D9")
            
            'if rng has more than one cell then
            If area.Cells.Count > 1 Then
            
                'save range as array
                Dim arr As Variant
                arr = area.value
                
                'loop through array
                Dim arrCell As Variant
                For Each arrCell In arr
                
                    'if cell is not empty then
                    If Len(Trim(arrCell)) > 0 Then
                        IsRangeEmpty = False
                        Exit Function
                    End If
    
                Next arrCell
                
            Else 'unnecessary to loop on a single cell
                
                'if cell is not empty then
                If Len(Trim(area.Value2)) > 0 Then
                    IsRangeEmpty = False
                    Exit Function
                End If
                
            End If
    
        Next area
    End If
    
    IsRangeEmpty = True

End Function

使用方法示例:

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

如果Range("A38:P38")是空的,它会在[立即 windows ]中打印True

0lvr5msh

0lvr5msh3#

如果变量未初始化或显式设置为Empty,则IsEmpty返回True。否则,返回False。如果表达式包含多个变量,则始终返回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函数,但后藤可以中断循环,如果至少有一个非空单元格被发现,并做你的代码更快,特别是如果范围很大,你需要检测这种情况。而且这段代码对我来说更容易理解它是做什么,比Excel的CountA函数,这不是VBA函数。

ufj5ltwl

ufj5ltwl4#

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`

稍后将提供澄清(目前我正在工作)

wztqucjr

wztqucjr5#

Dim cel As Range, hasNoData As Boolean

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

如果Selection中没有包含任何数据的单元格,则返回True。对于特定区域,只需用RANGE(...)替换Selection

u59ebvdq

u59ebvdq6#

另一个可能的解决方案。计算空单元格的数量,然后从单元格总数中减去该值

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
rks48beu

rks48beu7#

这只是对@TomM'sanswer/的一个小补充,一个简单的函数,用于检查您的选择的单元格是否为空

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
kyvafyod

kyvafyod8#

下面这一行的效果更好:

Application.Evaluate("SUMPRODUCT(--(E10:E14<>""""))=0")

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

相关问题