ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   End If (https://www.excelbanter.com/excel-programming/322977-end-if.html)

Aksel Børve

End If
 
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



JulieD

End If
 
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

"aksel børve" wrote in message
...
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





Chip Pearson

End If
 
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




"aksel børve" wrote in message
...
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





HS Hartkamp

End If
 

Alternatively,

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

Bas Hartkamp.

"aksel børve" wrote in message
...
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





Neal Zimm

End If
 
"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.


"aksel børve" wrote:

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





All times are GMT +1. The time now is 11:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com