Whats the diff with this If statement than the traditional If-else

J

Jay

a: If ActiveCell.Row = 17 Then GoTo Erxi2
AMsX = ActiveCell.Column
AMsY = ActiveCell.Row
AMsH = 0
if......

-- why am I getting an error in a if I put an "else" or "end if" it prompts
that "else without if" or "end if without if".
--Is it an independent if statement?
--If the condition is true it'll "goto Erxi2" right? and if false it'll
disregard the "goto Erxi2" right? But would it also disregards the next 3
lines and proceed to the next if?

please tell me how it actually works.

Thank you.
Jay
 
B

Bernard Liengme

You can have a one-line IF statement (note there is no "end if ") as in
If ActiveCell.Row = 17 Then GoTo Erxi2

or a multi-line statement as in
If ActiveCell.Row = 17 Then
GoTo Erxi2
end if

The second one allows you to have "elseif" and to have structures like

If ActiveCell.Row = 17 Then
msgbox "Value equals 17"
GoTo Erxi2
elseif ActiveCell.Row > 17 Then
msgbox "Value > 17"
else
msgbox "Value < 17"
end if

Have a look in the VBA Help of Google "Excel VBA IF syntax" for more details
bet wishes
 
L

Luke M

Because its a goto command, it wouldn't have a chance to complete the
following steps (AMsX = ActiveCell.Column) if condition is true. So yea, its
independent, and if the condition is true, it will not complete any of the
following steps (until it finds Erxi2), and if false, it will go straight to
AMsX = ActiveCell.Column
and continue from there.
 
J

Jay

--What about if I have 1 line if-statement and is false
It would disregard the "goto Erxi2" right? But would it also disregards the
next 3
lines and proceed to the next line of code?

If ActiveCell.Row = 17 Then GoTo Erxi2
AMsX = ActiveCell.Column
AMsY = ActiveCell.Row
AMsH = 0

Thank you,
Jay
 
A

aidan.heritage

There are TWO ways of doing an if statement - the first

If ActiveCell.Row = 17 Then GoTo Erxi2

is a line on its own, and will either be executed or not - the lines
following it will continue to be executed.

The other

If Statement=true then

'series of actions

end if


will ONLY carry out the series of actions if the statement is true,
and will allow for else and elseif conditions as well.
 
J

JLGWhiz

This is a complete If...Then statement

If True = True Then Stop

The criteria is evaluated and the result is executed on the same line of code.

This is a block If...Then statement

If True = True Then
Stop
End If

The criteria is evaluated on line one and the result is executed on line
two. The
statement is closed on line three. If there is more than one action to be
taken when the criteria is evaluated then each action would be listed on a
separate line before the End If. This would also be a block If...Then...Else
statement.
If True = True Then
Stop
Else
Beep
MsgBox "Hello"
End If
 
B

Bernard Liengme

No, the one-line IF is just that: a single statement. So it the condition is
false, that statement does nothing, and the next line of code gets executed.

This
If ActiveCell.Row = 17 Then GoTo Erxi2

Is the same as
If ActiveCell.Row = 17 Then
GoTo Erxi2
End if

It is just short to code
best wishes
 
J

Jay

So do you mean that with these lines of code either it is true or false it'll
continue to
execute the 3 lines below the condition?
 
J

Jay

a: If ActiveCell.Row = 17 Then GoTo Erxi2 '//if true goto Erxi2
disregard next line right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top