Search code examples
vbaif-statementmsgbox

VBA If statement to call Msgbox always calls msgbox


I am trying to use an if statement to check for blanks and return a msgbox if there are blank fields. If there are no blank fields it runs another block of code. However even when you fill out all the fields the msgbox is always returned and the next block of code doesn't run. I'm pretty new to VBA and writing code in gneneral so any advice would be helpful.

Code in question:

'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

Solution

  • You would reference a range like this:

    If Len(Range("C3").Value) = 0 Then
        MsgBox "Fill out all required fields"
    

    But shorter to do something like this:

    If Application.CountA(Range("C3,C7,C11,C13,C17")) < 5 Then
        MsgBox "Fill out all required fields"
    End if