1

It's probably an easy one but I'm kinda new to programming. I probably did some hard-coding beceause I don't know any better.

I would like to add an error message if the first If statement isn't met. I will give you a simplified example of my code. If you want the actual code, let me know and I will edit my question (but careful, it's long!)

If CheckBox1.Value = True and CheckBox2.Value = False then 

(CODE number 1)

Else
MsgBox ("error")
End If

If CheckBox1.Value = False and CheckBox2.Value = True then

(CODE numer 2)

Else
MsgBox("Error")
End if

The problem here is that the If statements keeps coming and the code will run line by line. I am aware of that. But I just don't know any better. So I can't place any msgbox error because the code will keep running and execute the rest.

How Do I fragment theses lines so I can place any messagebox I want for each blocks of if ?

Sorry for my english.

Z Kubota
  • 155
  • 1
  • 3
  • 11
Chadi N
  • 439
  • 3
  • 13
  • When I used MsgBox between `Else` and the `Exit Sub`, nothing happens! If I dont respect the `If` Statement, and click the button to generate the number on my form, nothing happens. No number get generate, but no error message appears neither. @K.Dᴀᴠɪs – Chadi N Jan 31 '20 at 22:43
  • I'm completely lost now! Thanks anwyays for the clues! @K.Dᴀᴠɪs – Chadi N Jan 31 '20 at 23:06
  • Forget what I said earlier, you completely changed your code to be something drastically different than what first appeared. – K.Dᴀᴠɪs Feb 01 '20 at 00:10

1 Answers1

0

Condense that into an If...ElseIf...End If statement.

If CheckBox1.Value = True and CheckBox2.Value = False then 

(CODE number 1)

ElseIf CheckBox1.Value = False and CheckBox2.Value = True then

(CODE numer 2)

Else
MsgBox("Error")
End if

This will check the first statement, and if it evaluates as true, it'll do (CODE number 1). If it's not true, it'll check to see if the second statement is true. If the second statement is true, it'll do (CODE number 2). If the second statement isn't true, it'll do MsgBox("Error")

If you have a BUNCH of these, you can do a Select Case True workaround, but it's generally not advised.

jclasley
  • 668
  • 5
  • 14