当我选择单元格C1时,如果A1为空,我想要弹出一个消息框。虽然没有实际发生(也没有错误信息)。我的语法错了还是我错了?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "C1" And Range("A1") Is Nothing Then
MsgBox "Please fill out previous fields to continue"
End If
End Sub
尝试:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$1" And Range("A1").Value = "" Then 'or IsEmpty(Range("A1"))
MsgBox "Please fill out previous fields to continue"
End If
End Sub
Target.Address
将返回$C$1
- 所以那里需要。并且Is Nothing
不用于确定细胞是否为空 - 这将是IsEmpty()
或.Value = ""
- Is Nothing
更多地用于确定Object
是否为Set
。
Range("A1")
返回一个Range
对象,代表A1
的细胞 - 这永远不会是Nothing
,因为A1
将永远作为一个细胞存在。
你可以And IsEmpty(Range("A1"))
看看它是否有价值。
试试这个
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$1" And Len(Range("A1")) = 0 Then
MsgBox "Please fill out previous fields to continue"
End If
End Sub
这适合你:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) = "C1" And IsEmpty(Range("A1")) = True Then
MsgBox "Please fill out previous fields to continue"
End If
结束子