Excel 检测范围是否为空
我想检查Excel中的某个范围是否为空 如何使用VBA代码编写:Excel 检测范围是否为空,excel,vba,range,Excel,Vba,Range,我想检查Excel中的某个范围是否为空 如何使用VBA代码编写: If Range("A38":"P38") is empty 从我得到的评论中找到了解决方案 Sub TestIsEmpty() If WorksheetFunction.CountA(Range("A38:P38")) = 0 Then MsgBox "Empty" Else MsgBox "Not Empty" End If End Sub Dim M As范围 设置
If Range("A38":"P38") is empty
从我得到的评论中找到了解决方案
Sub TestIsEmpty()
If WorksheetFunction.CountA(Range("A38:P38")) = 0 Then
MsgBox "Empty"
Else
MsgBox "Not Empty"
End If
End Sub
Dim M As范围
设置M=选择
如果application.CountIf(M,“0”)<2,则
MsgBox“未选择任何内容,请选择第一个BOM表或下一个BOM表”
其他的
'您的代码在这里
如果结束
根据我的经验,我刚刚了解到你可以做到:
If Selection.Rows.Count < 2
Then End If`
如果Selection.Rows.Count<2
然后结束,如果`
稍后提供澄清(现在我正在工作)如果变量未初始化或显式设置为空,IsEmpty将返回True;否则,它将返回False。如果表达式包含多个变量,则始终返回False。IsEmpty仅返回变量的有意义信息。() . 因此,必须分别检查范围内的每个单元格:
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可以中断循环,并且可以更快地编写代码,尤其是在范围较大且需要检测这种情况的情况下。另外,对于我来说,这段代码比Excel CountA函数更容易理解,因为Excel CountA函数不是VBA函数。另一种可能的解决方案。对空单元格进行计数,并从单元格总数中减去该值
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
如果
选择
中没有包含任何数据的单元格,则返回True
。对于特定范围,只需用range(…)
替换选择
如果您发现自己处于无法使用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
If area.Cells.Count > 1 Then
'save range as array
Dim arr As Variant
arr = area.value
'loop through array
Dim cel As Variant
For Each cel In arr
'if cell is not empty then
If Len(Trim(cel)) > 0 Then
IsRangeEmpty = False
Exit Function
End If
Next cel
Else 'cannot loop on array with one value
'if cell is not empty then
If Len(Trim(area.Value2)) > 0 Then
IsRangeEmpty = False
Exit Function
End If
End If
Next area
IsRangeEmpty = True
End Function
如何使用它的示例:
Sub Test()
Debug.Print IsRangeEmpty(Range("A38:P38"))
End Sub
如果
Range(“A38:P38”)
为空,它将在即时窗口中打印True
;否则它会打印False
这只是对@TomM的
/一个简单的检查功能的一个小小的补充
如果您选择的单元格为空
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
这条线在imho中工作得更好:
Application.Evaluate("SUMPRODUCT(--(E10:E14<>""""))=0")
Application.Evaluate(“SUMPRODUCT(---(E10:E14“”))=0”)
在这种情况下,它会评估范围E10:E14是否为空。请参考API(这是PIA,但VBA中使用的COM应该类似):(可能是Count属性)我对VBA非常陌生,所以这对我来说没有太大意义:)
范围(“A38:P38”)。Count
,请参阅Excel宏代码(不知道是否可以编译)当您有大量迭代时,需要一段时间。如果您已经确定区域中的某个单元格不是空的,并且希望对其进行处理,则需要您的解决方案,但是使用可接受的答案(CountA)来检查确定区域是否为空(即,如果您的代码是必需的)应该更快,甚至可能更快。在VB循环中使用一个“本机”函数也会导致更快的运行时间。这并不总是有效的。例如,如果您的范围在选择后不是连续选择(假设您的范围选择跳过1个单元格,然后跳过5个单元格,返回到1个单元格,等等),那么这将出错。感谢您的回答,您还可以澄清一下,对于未来的访问者,比较到底在做什么?编辑:理解您的编辑如何与原始示例相匹配也有点棘手。@Thor当您在Google上搜索问题的解决方案时,您将获得此页面。但是这一页没有回答这个问题,这就是我回答这个问题的原因。我相信这里的人不只是投票而已。“我可以看出这已经帮助了一些人。”让·弗朗索瓦·科贝特(Jean-FrançoisCorbett)是的,我清醒生活的每一个小时。。。看,这对寻求帮助的人有帮助。我知道当我试图弄明白这一点时,我在谷歌搜索中一次又一次地看到了这个页面。这正是我正在寻找的解决方案,所以我试图帮助其他人并发布它。即使不是100%的直接帮助,也是1000%的间接帮助。注意:“Empty()”不是有效的过程名称@darren bartrup cook,我建议的编辑不会偏离文章的初衷,并保留其目标。如果范围可能包含空字符串值(常数)或可能返回空字符串值的公式,且具有空字符串值的单元格必须视为“空”然后改为使用此表达式:WorksheetFunction.CountBlank(Range(“A38:P38”))=Range(“A38:P38”).Cells.Count(可能还可以使用With语句来清理它)。要处理范围仅为一个单元格的情况,可以使用“If rng.Cells.Count<2 then…”并仅测试此单元格。@LePatay感谢您的提醒,我没有用一个细胞测试过!现在应该修复。这绝对是一个函数!太好了,谢谢:)我在上面对卡诺帖子的评论中的例子,是解决这个问题的一个更简单的方法。@pstraton看起来与DJK的解决方案相匹配
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
Application.Evaluate("SUMPRODUCT(--(E10:E14<>""""))=0")