VBA Multiple IFs

Endeavouring

Board Regular
Joined
Jun 30, 2010
Messages
115
Hi
I'm Trying to put some error checking in place on a form but feel there must be a better way of writing the following. Can anyone advise.
Thank you

Dim check As Integer
check = 0
If Ref.Value = "" Then check = check + 1
If Ref.Value = "" Then MsgBox "No Gender Selected"
If TextBox3.Value = "" Then check = check + 1
If TextBox3 = "" Then MsgBox "No Course Entered"
If TextBox4.Value = "" Then check = check + 1
if TextBox4 = "" Then MsgBox "No age Entered"
If check > 0 Then GoTo error
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this (Not Tested)

Dim check As Integer
check = 0
If ref.Value = "" Then
check = check + 1
MsgBox "No Gender Selected"
End If
If textbox3.Value = "" Then
check = check + 1
MsgBox "No Courses Entered"
End If
If textbox4.Value = "" Then
check = check + 1
MsgBox "No age Entered"
End IF
If check > 0 Then GoTo Error
End If
 
Last edited:
Upvote 0
You can reduce it somewhat but nothing major..

Code:
if Ref.value="" or TextBox3.Value = "" or TextBox4.Value = "" then
          If Ref.Value = "" Then MsgBox "No Gender Selected"
          If TextBox3 = "" Then MsgBox "No Course Entered"
          if TextBox4 = "" Then MsgBox "No age Entered"
          Goto error
end if

Edited as you don't need to record the check value, I was being daft keeping that in unless your error checking continues for some reason
 
Last edited:
Upvote 0
Hi there,

Try:

Code:
Sub test()
    Dim check As Integer
    check = 0
    
    If Ref.Value = "" Then
        check = check + 1
        MsgBox "No Gender Selected"
    End If
    
    If TextBox3.Value = "" Then
        check = check + 1
        MsgBox "No Course Entered"
    End If
    
    If TextBox4.Value = "" Then
        check = check + 1
        MsgBox "No age Entered"
    End If
    
    If check > 0 Then GoTo Error
End Sub
 
Upvote 0
Hi All
Many thanks for your responses. They work well regarding the msbboxes, but even when all fields are filled in it jumps to the "error" label which is simply
error:
Exit Sub

So the full code reads

If Ref.Value = "" Or TextBox3.Value = "" Or TextBox4.Value = "" Then
If Ref.Value = "" Then MsgBox "No Gender Selected"
If TextBox3 = "" Then MsgBox "No Course Entered"
If TextBox4 = "" Then MsgBox "No age Entered"
GoTo error

error:
Exit Sub

I used "Exit Sub" for the error as I want to leave the user on the form.
What am I doing wrong?
 
Upvote 0
You need to exit the sub before you enter the error label.

Code:
Sub MySub()

'DO STUFF

exit sub
Error:
'Error Handling stuff

End Sub
 
Upvote 0
Hi

Forgive me as I'm fairly new to VBA but when I put the
error:
Exit Sub
Outside the End Sub

It Results in an automation error
 
Upvote 0
Why would you put it after End Sub. "Exit Sub" needs to come before the "Error" label which, in turn, needs to come before "End Sub" as I laid out earlier.
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,304
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top