在Excel中使用VBA IsEmpty函数时,如何检查用户输入?
我有一个复选框,如果选中,它会更改单元格的颜色。在突出显示的字段中输入值之前,用户无法打印页面。复选框和限制打印模块均正常工作 用户输入值后,如何更改高亮显示字段的颜色?我编写了下面的代码,但它只有在手动运行时才起作用。我是VBA新手,因此非常感谢您的帮助在Excel中使用VBA IsEmpty函数时,如何检查用户输入?,vba,excel,Vba,Excel,我有一个复选框,如果选中,它会更改单元格的颜色。在突出显示的字段中输入值之前,用户无法打印页面。复选框和限制打印模块均正常工作 用户输入值后,如何更改高亮显示字段的颜色?我编写了下面的代码,但它只有在手动运行时才起作用。我是VBA新手,因此非常感谢您的帮助 Private Sub Can_Print() If IsEmpty(Range("G54")) = False Then MsgBox "You may now print." Range("G54"
Private Sub Can_Print()
If IsEmpty(Range("G54")) = False Then
MsgBox "You may now print."
Range("G54").Interior.Color = RGB(221, 235, 247)
End If
End Sub
将此代码放在要为其运行的工作表下:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value <> "" Then
MsgBox "You may now print"
Target.Interior.Color = RGB(221, 235, 247)
End If
End Sub
Private子工作表\u更改(ByVal目标作为范围)
如果Target.Value为“”,则
MsgBox“您现在可以打印”
Target.Interior.Color=RGB(221235 247)
如果结束
端接头
如果您不希望它对任何单元格起作用,则只需将目标替换为您想要的范围。如果您尚未获得它,请检查此项。您需要将代码复制到工作表事件中。您需要将代码附加到工作表更改事件
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("G5:G6")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Application.EnableEvents = False
If IsEmpty(Target) = False Then
'MsgBox "You may now print."
Target.Interior.Color = RGB(22, 235, 247)
Else
Target.Interior.ColorIndex = 0
End If
Application.EnableEvents = True
End If
End Sub
看看这个事件。你也可以用条件格式来完成formula@Comintern感谢您为我指明了正确的方向。您应该使用Intersect()函数来确保正确的单元格被选中colored@VBA_SQL_Programmer谢谢你的帮助!您还可以添加Trim并使用Len,如
中所示,如果不是Len(Trim(Target.Value))=0