如果相邻单元格中有值,我正在尝试打印一个空单元格。我正在使用从b3到该列最后一行的范围,但是没有选中空单元格-j变量没有得到空单元格。不使用任何留有空格的公式(例如“”)。这是代码:
Dim ra as Range
Dim i as Integer
Dim cell as Range
Dim j as String
i = 0
Set ra = Range([b3], Cells(Rows.count, "B").End(xlUp))
For Each cell In ra
If IsEmpty(cell) Then
If (cell.Offset(1, 0).value <> vbNullString) Then
j = j & cell.Address(0, 0) & vbNewLine
i = i + 1
End If
End If
Next cell
' If i = 0 Then Exit Sub
msg = msg & vbCrLf & "Sorry, you must enter the missing values in BY Variables sheet in these cells : " & vbNewLine & j
If msg <> "" Then MsgBox msg
答案 0 :(得分:2)
如果您的单元格包含返回零长度字符串的公式(例如""
),则它们不会被视为“空”。使用IsEmpty(cell)测试时,只有一个真正空白的单元格为True。
但是,与真正的空白单元格或包含返回了""
的公式的单元格相比,零长度字符串和vbnullstring都返回True。
For Each cell In ra
If cell.value = vbNullString Then
If cell.Offset(1, 0).value <> vbNullString Then
j = j & cell.Address(0, 0) & vbNewLine
i = i + 1
End If
End If
Next cell
我不知道谁在传播这种“坏习惯”语法,
Set ra = Range([b3], Cells(Rows.count, "B").End(xlUp))
...但是应该放弃它,以代替它,
with worksheets("sheet1") 'you should know what worksheet you are on
Set ra = .Range(.cells(3, "B"), .Cells(.Rows.count, "B").End(xlUp))
end with