#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 618
Default 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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 345
Default 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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"