End If

  • Thread starter Thread starter aksel børve
  • Start date Start date
A

aksel børve

I use the "If" on many of my codes, But when I try to write "End If" after
the code, A message tell me that i can't "End If" without "Block If". Why?
and Is there any reason to "End If" when the code works without?
Thank You
Aksel
 
Hi

it depends on how you write the IF statement
a single line IF doesn't need an End IF
e.g.

IF Range("A1").value = 1 Then Exit Sub

needs no End If (and doens't want one)

however, if you write it as follows

If Range("A1").value = 1 Then
Exit Sub

you need an End If (the code won't run without).

hope this helps
Cheers
JulieD
 
An 'End If' is required if you use 'block style' If/Then/Else
statements. For example,

If whatever Then
Debug.Print "yes"
Else
Debug.Print "no"
End If

An End If is not allowed if you use 'line style' If/Then/Else
statements. For example,

If whatever Then Debug.Print "yes" Else Debug.Print "no"

What style of If statements are you using?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Alternatively,

You can have another loop of some sort (e.g. While, Repeat, Select Case,
etc.) crossing the if / end if statements.

Bas Hartkamp.
 
"If" you have not already looked at it, the Excel help explanation on the
'If' statement is pretty good. Put your cursor on the word: If in your vba
code and press F1 function key.
 

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

Back
Top