Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 422
Default Clarification please...

I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 510
Default Clarification please...

Hi

If has 2 possible syntaxes. From MS Visual Basic Help:
If condition Then [statements] [Else elsestatements]

Or, you can use the block form syntax:

If condition Then
[statements]

[ElseIf condition-n Then
[elseifstatements] ...

[Else
[elsestatements]]

End If

In your example the single-line form is used. You probably got confused
because the command line was splitted.
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True

is same as
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True Then Cancel =
True


--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)



"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Clarification please...

You have already received the explanation vis-a-vis the If part. An
additional tidbit is that the If is totally unnecessary.

Cancel = ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True

or

Cancel = Not ThisWorkbook.Sheets("Sheet1").Range("A1").Value

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <nEric.16454$VQ3.11320@lakeread06, says...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 422
Default Clarification please...

Arvi:
Thanks for response. So there are two forms of "If";

1) The Single-line form - If True then Statement <<<< Not requiring
"End If"
2) The Block form - If True then Statement, ElseIf, Else, EndIf
<<<<Requiring "EndIf"

The second forms appears plentifully in Code compared to the first. Is
there a "rule"
which directs you to apply #1 versus #2?

TIA,

"Arvi Laanemets" wrote in message
...
Hi

If has 2 possible syntaxes. From MS Visual Basic Help:
If condition Then [statements] [Else elsestatements]

Or, you can use the block form syntax:

If condition Then
[statements]

[ElseIf condition-n Then
[elseifstatements] ...

[Else
[elsestatements]]

End If

In your example the single-line form is used. You probably got confused
because the command line was splitted.
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True

is same as
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True Then Cancel =
True


--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)



"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Clarification please...

There are two styles of if statements.

One logical line and multiple logical lines (block form).

your example is really one logical line--it has the line continuation characters
(space underscore) trailing the True.

if a = b then c = d

if a = b then
c = d
end if

My personal preference is to use the multiple line version--except on almost
trivial code where it's easily seen.

But at my age, I'll sometimes miss those continuation characters and I get
confused.

Next time you're in the VBE, highlight an If and hit F1. You'll see a couple
examples.



JMay wrote:

I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub


--

Dave Peterson



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 422
Default Clarification please...

OK - I see it (Tks Dave)...
If you can get the logical statement "ON ONE LINE" the EndIf is not
required.
If you take it two lines or more EndIf is required.
Is that it?????
Sorry for my denseness, but until I confront it - (the denseness) never goes
away LOL
TIA

"JMay" wrote in message
news:Yasic.16745$VQ3.7640@lakeread06...
Arvi:
Thanks for response. So there are two forms of "If";

1) The Single-line form - If True then Statement <<<< Not requiring
"End If"
2) The Block form - If True then Statement, ElseIf, Else, EndIf
<<<<Requiring "EndIf"

The second forms appears plentifully in Code compared to the first. Is
there a "rule"
which directs you to apply #1 versus #2?

TIA,

"Arvi Laanemets" wrote in message
...
Hi

If has 2 possible syntaxes. From MS Visual Basic Help:
If condition Then [statements] [Else elsestatements]

Or, you can use the block form syntax:

If condition Then
[statements]

[ElseIf condition-n Then
[elseifstatements] ...

[Else
[elsestatements]]

End If

In your example the single-line form is used. You probably got confused
because the command line was splitted.
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True

is same as
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True Then Cancel =
True


--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)



"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub








  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Clarification please...

You are correct, sir.

(Ed McMahon voice.)

JMay wrote:

OK - I see it (Tks Dave)...
If you can get the logical statement "ON ONE LINE" the EndIf is not
required.
If you take it two lines or more EndIf is required.
Is that it?????
Sorry for my denseness, but until I confront it - (the denseness) never goes
away LOL
TIA

"JMay" wrote in message
news:Yasic.16745$VQ3.7640@lakeread06...
Arvi:
Thanks for response. So there are two forms of "If";

1) The Single-line form - If True then Statement <<<< Not requiring
"End If"
2) The Block form - If True then Statement, ElseIf, Else, EndIf
<<<<Requiring "EndIf"

The second forms appears plentifully in Code compared to the first. Is
there a "rule"
which directs you to apply #1 versus #2?

TIA,

"Arvi Laanemets" wrote in message
...
Hi

If has 2 possible syntaxes. From MS Visual Basic Help:
If condition Then [statements] [Else elsestatements]

Or, you can use the block form syntax:

If condition Then
[statements]

[ElseIf condition-n Then
[elseifstatements] ...

[Else
[elsestatements]]

End If

In your example the single-line form is used. You probably got confused
because the command line was splitted.
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True

is same as
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True Then Cancel =
True


--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)



"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub







--

Dave Peterson

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Clarification please...

Not necessary for one-liners : if this then dothat

However,

if this then
dothat1
dothat2....
end if


Paul

"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Clarification please...

In most of my programming, I tend to think of intent rather than lines
of code. So, if the IF statement has a single intent in the THEN and
the ELSE parts, I use a single line form even if it takes multiple
lines of code. An example:

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <_osic.16879$VQ3.8983@lakeread06, says...
OK - I see it (Tks Dave)...
If you can get the logical statement "ON ONE LINE" the EndIf is not
required.
If you take it two lines or more EndIf is required.
Is that it?????
Sorry for my denseness, but until I confront it - (the denseness) never goes
away LOL
TIA

"JMay" wrote in message
news:Yasic.16745$VQ3.7640@lakeread06...
Arvi:
Thanks for response. So there are two forms of "If";

1) The Single-line form - If True then Statement <<<< Not requiring
"End If"
2) The Block form - If True then Statement, ElseIf, Else, EndIf
<<<<Requiring "EndIf"

The second forms appears plentifully in Code compared to the first. Is
there a "rule"
which directs you to apply #1 versus #2?

TIA,

"Arvi Laanemets" wrote in message
...
Hi

If has 2 possible syntaxes. From MS Visual Basic Help:
If condition Then [statements] [Else elsestatements]

Or, you can use the block form syntax:

If condition Then
[statements]

[ElseIf condition-n Then
[elseifstatements] ...

[Else
[elsestatements]]

End If

In your example the single-line form is used. You probably got confused
because the command line was splitted.
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True

is same as
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True Then Cancel =
True


--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)



"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub









  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Clarification please...

In my programming endeavors I tend to think in terms of intent (where
the code simply reflects the developer's intent). So, if there is a
single intent in the IF/THEN/ELSE parts, I use the single-line form,
even if I then use multiple lines for ease-of-reading. It makes the
*intent* clearer to whoever might be reading the code at a later date.
And, the intent itself might include multiple VBA statements!

Dim x As Integer, y As Integer, z As Boolean
z = Application.InputBox("z?", Type:=4)
If z Then _
x = 0: y = 0 _
Else _
x = 99: y = 99
MsgBox x & "," & y

Of course, all this is moot since VB.Net does not support the single-
line form of the IF.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article <_osic.16879$VQ3.8983@lakeread06, says...
OK - I see it (Tks Dave)...
If you can get the logical statement "ON ONE LINE" the EndIf is not
required.
If you take it two lines or more EndIf is required.
Is that it?????
Sorry for my denseness, but until I confront it - (the denseness) never goes
away LOL
TIA

"JMay" wrote in message
news:Yasic.16745$VQ3.7640@lakeread06...
Arvi:
Thanks for response. So there are two forms of "If";

1) The Single-line form - If True then Statement <<<< Not requiring
"End If"
2) The Block form - If True then Statement, ElseIf, Else, EndIf
<<<<Requiring "EndIf"

The second forms appears plentifully in Code compared to the first. Is
there a "rule"
which directs you to apply #1 versus #2?

TIA,

"Arvi Laanemets" wrote in message
...
Hi

If has 2 possible syntaxes. From MS Visual Basic Help:
If condition Then [statements] [Else elsestatements]

Or, you can use the block form syntax:

If condition Then
[statements]

[ElseIf condition-n Then
[elseifstatements] ...

[Else
[elsestatements]]

End If

In your example the single-line form is used. You probably got confused
because the command line was splitted.
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True

is same as
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True Then Cancel =
True


--
Arvi Laanemets
(Don't use my reply address - it's spam-trap)



"JMay" wrote in message
news:nEric.16454$VQ3.11320@lakeread06...
I thought there was this rule where if you use the "If" keyword
somewhere afterwards one had to follow-up with the "End If" phrase.
Obviously not. Maybe the rule only applies in a Standard module
and not a class module like below. Can someone clarify?
Thanks in advance..


Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Sheets("Sheet1").Range("A1").Value < True _
Then Cancel = True
End Sub









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


Similar Threads
Thread Thread Starter Forum Replies Last Post
clarification on VLOOKUP Hamsa Excel Discussion (Misc queries) 3 March 12th 09 02:14 PM
Clarification pmayne181 Excel Discussion (Misc queries) 1 December 2nd 08 12:56 PM
Subtotal - Clarification DumbCluck New Users to Excel 1 December 10th 07 09:27 PM
countif clarification LInda Excel Discussion (Misc queries) 9 March 31st 06 12:55 AM
If and Dates Clarification wal50 Excel Worksheet Functions 4 January 4th 05 06:31 PM


All times are GMT +1. The time now is 07:23 AM.

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

About Us

"It's about Microsoft Excel"