我有一个给定的空数字范围。我想告诉用户哪些单元格是空的。这是函数:
Sub check()
' Goal: check if there are any entry truth table cells for outputs
Dim outputsrange As range
Set outputsrange = range(inputnum + 1 & ":" & inputnum + outputnum)
If IsEmpty(outputsrange) Then
MsgBox ("The following cells are empty:" & vbNewLine & emptycell)
Else
Call makekmap
End If
End Sub
我应该在“emptycell”所在的位置放置什么?
编辑:新代码仍然无法运行,但具有正确的对象定义。
Sub check()
Dim outputsrange As range, emptycells As range
Set outputsrange = range(inputnum + 1 & ":" & cases)
Set emptycells = outputsrange.SpecialCells(xlCellTypeBlanks)
If Not emptycells Is Nothing Then
MsgBox "The following cells are empty:" & vbNewLine & emptycells.Address
Else
MsgBox ("No cells are empty")
End If
End Sub
最佳答案
检查是否Range.SpecialCells method使用 xlCellTypeBlanks 选项报告空白单元格并输出联合的 Range.Address property如果它们存在的话。
Dim inputnum As Long, outputnum As Long
Dim outputsrange As Range, mtCells As Range
inputnum = 1: outputnum = 6
Set outputsrange = Range(inputnum + 1 & ":" & inputnum + outputnum)
On Error Resume Next
Set mtCells = outputsrange.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not mtCells Is Nothing Then
MsgBox "The following cells are empty:" & vbNewLine & _
mtCells.Address(0, 0)
Else
Call makekmap
End If
请注意,包含返回零长度字符串的公式的单元格(例如 ""
并不是真正的空白。
交替而不发生错误继续下一步
Dim inputnum As Long, outputnum As Long
Dim outputsrange As Range
inputnum = 1: outputnum = 6
Set outputsrange = Range(inputnum + 1 & ":" & inputnum + outputnum)
If CBool(Application.CountBlank(outputsrange)) Then
MsgBox "The following cells are empty:" & vbNewLine & _
outputsrange.SpecialCells(xlCellTypeBlanks).Address(0, 0)
Else
Call makekmap
End If
您必须小心这一点,因为工作表的 COUNTBLANK function会将包含零长度字符串的单元格计为空白,但 SpecialCells(xlCellTypeBlanks) 不会。有可能,你可以 有一种情况,你进入了MsgBox区域,但没有什么可报告的。
关于vba - 给定范围内哪些单元格为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246981/