我正在尝试使用 if 语句来检查空白并在存在空白字段时返回 msgbox。如果没有空白字段,它将运行另一个代码块。然而,即使您填写了所有字段,消息框也始终会返回,并且下一个代码块不会运行。我对 VBA 和一般编写代码还很陌生,所以任何建议都会有帮助。
有问题的代码:
'Check required fields
If IsEmpty(C3) Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C7) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C9) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C11) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C13) = True Then
MsgBox ("Fill out all required fields")
ElseIf IsEmpty(C17) = True Then
MsgBox ("Fill out all required fields")
Else
Run Code Online (Sandbox Code Playgroud)
您可以引用这样的范围:
If Len(Range("C3").Value) = 0 Then
MsgBox "Fill out all required fields"
Run Code Online (Sandbox Code Playgroud)
但做这样的事情更短:
If Application.CountA(Range("C3,C7,C11,C13,C17")) < 5 Then
MsgBox "Fill out all required fields"
End if
Run Code Online (Sandbox Code Playgroud)