PDA

View Full Version : Solved: How the Code Works?



jammer6_9
01-22-2009, 11:14 AM
I was wondering how and why this code works. All I thought is if there is "If" it must be closed by "End If"... On the code below, I have written 5 "If" and closed only with 2 "End If" that I don't understand. Please :help me understand...


Private Sub cmdGo_Click()
On Error Resume Next
If cmbMarket.Value = "" Or cmbtrend.Value = "" Or cmbIN.Value = "" Then
MsgBox ("All fields required, Fill up all to continue!"), vbExclamation, "ofsjcr"
If cmbMarket.Value = "" Then cmbMarket.SetFocus
If cmbtrend.Value = "" Then cmbtrend.SetFocus
If cmbIN.Value = "" Then cmbIN.SetFocus

Else

If cmbMarket.Value = "KSA" And cmbtrend.Value = "SALES COMPS" And cmbIN.Value = "NUMBERS" Then
Sheets("GRAPH SALES KSA").Visible = True
Sheets("GRAPH SALES KSA").Select
Else
ThisWorkbook.Sheets("GRAPH SALES KSA").Visible = xlSheetVeryHidden

End If

Unload Me
End If
End Sub

mdmackillop
01-22-2009, 11:28 AM
If cmbIN.Value = "" Then cmbIN.SetFocus


This counts as a complete statement. No End If is required

crazymatt1
01-22-2009, 11:40 AM
There are two types of If statements: Single-Line and Block.

The single-line syntax is:
If (logical test) Then (Action) : (Action) : (Action) Else (Action)
The block syntax is:
If (logical test) Then
(Action)
(Action)
(Action)
Else
(Action)
End If
Both types will return the same results, but the block is much easier to read if there are multiple actions taken as the result of the test.

Edit: And that's what happens when you forget to hit 'Reply'!

jammer6_9
01-22-2009, 12:45 PM
If cmbIN.Value = "" Then cmbIN.SetFocus


This counts as a complete statement. No End If is required

I guess this answers my question.

crazymatt1, Thanks as well.

Bob Phillips
01-22-2009, 02:09 PM
Actually, there are 3, a functional single liner



myAns = Iif(condition, True_Value, False_Value)