1

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
Warcupine
  • 4,460
  • 3
  • 15
  • 24
The_Ltrain
  • 13
  • 2

2 Answers2

5

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 
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
1

C7 is not a Range. Nor are C9, C11, C13 or C17

[C7] is a range. A better way to write this, however, would be ActiveSheet.Range("C7") or ActiveSheet.Cells(7,3)

You can then do nifty things like use Error Handling and SpecialCells:

On Error GoTo NoErrors
Dim BlankFields AS Long
BlankFields = ActiveSheet.Range("C7,C9,C11,C13,C17").SpecialCells(xlCellTypeBlanks).Count
MsgBox "Fill out all required fields" & vbCrLf & BlankFields & "field(s) remaining"
NoErrors:
On Error GoTo 0
Chronocidal
  • 6,827
  • 1
  • 12
  • 26