vba - 给定范围内哪些单元格为空

标签 vba excel

我有一个给定的空数字范围。我想告诉用户哪些单元格是空的。这是函数:

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/

相关文章:

vba - 为什么 VBA 中的 GetValue 函数使用单元格 "A1"?

vba - 在VBA中使用for循环调用连续的变量名(即car1,car2...car10)

excel - 如何显示共同的单元格

vba - 获取枚举的等效文本

r - 获取对角线对称位置的最大值

vba - 检查范围内的所有值是否相同

VBA EXCEL - 提取工作簿的副本

excel - VBA 循环填充表格

excel - 遍历文件夹并复制第一张工作表的值和格式

java - 这个excel方法在做什么?