If Statement Variations

N

Nedan Nedzatra

Hia!

How is the day going friends!

I am stuck into another crazy programming thing!!!

3 questions;

I know normal VBA if statements and Excel or SQL ifs. However;

What is if (something) ---- why do you need bracket?
What is if xxxxx = 0& ----- why do you need “&â€
What is #if ----- why that “#â€

Could someone explain?
 
C

Chip Pearson

What is if (something) ---- why do you need bracket?

You don't need the outer enclosing parentheses, but they are harmless
if included. Some programmers use them as a matter of personal coding
style, but they are not required to do so.
What is if xxxxx = 0& ----- why do you need “&”

In this context, the & character forces the compiler to use a Long
integer data type (32-bit signed) rather than a short Integer (16-bit
signed). It is required for some calculations whose inputs and result
may fit in a short integer but whose intermediate calculations
overflow a short Integer. For example, consider the following code:

Dim X As Integer
Dim Y As Integer
X = 30000
Y = (X * 2) / 2
Debug.Print Y

Here, the inputs and output of the calculation are within the bounds
of a short Integer (+/- 32K), but the intermediate calculation (X*2)
overflows a short integer and the code will blow up with an overflow
error. If you change the line of code to

Y = (X * 2&) / 2

the & character forces the compiler to use a Long integer when
calculating X*2, so there is no overflow error.

What is #if ----- why that “#”

The # character is for "conditional compilation". This allows you to
exclude blocks of the code from being compiled. In the end, it isn't
really any different than commenting out the same block of code, but
is simpler. For example, when you are developing and debugging a
program, you can set a DEBUG flag with the #CONST statement and then
test that flag to compile or not compile the block of code. This makes
it simpler to switch to and from debug and testing and production
code. E.g,

#CONST DEBUG = True
' lots of code
#If DEBUG = True Then
' diagnostic and trace code
#End If

Simply changing the single #CONST line can cause many debug and trace
blocks of code to be excluded in the production version. Much simpler
than try to remember which lines of code should be commented at
release.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
 
N

Nedan Nedzatra

Hia!

Chip!

Thank You.

I am Ok with the first 2 now. Could you please extend the 3rd as you
explained the second? I do not think I could get assistance on this from VBA
help; I am trying

Chip Pearson said:
What is if (something) ---- why do you need bracket?

You don't need the outer enclosing parentheses, but they are harmless
if included. Some programmers use them as a matter of personal coding
style, but they are not required to do so.
What is if xxxxx = 0& ----- why do you need “&â€

In this context, the & character forces the compiler to use a Long
integer data type (32-bit signed) rather than a short Integer (16-bit
signed). It is required for some calculations whose inputs and result
may fit in a short integer but whose intermediate calculations
overflow a short Integer. For example, consider the following code:

Dim X As Integer
Dim Y As Integer
X = 30000
Y = (X * 2) / 2
Debug.Print Y

Here, the inputs and output of the calculation are within the bounds
of a short Integer (+/- 32K), but the intermediate calculation (X*2)
overflows a short integer and the code will blow up with an overflow
error. If you change the line of code to

Y = (X * 2&) / 2

the & character forces the compiler to use a Long integer when
calculating X*2, so there is no overflow error.

What is #if ----- why that “#â€

The # character is for "conditional compilation". This allows you to
exclude blocks of the code from being compiled. In the end, it isn't
really any different than commenting out the same block of code, but
is simpler. For example, when you are developing and debugging a
program, you can set a DEBUG flag with the #CONST statement and then
test that flag to compile or not compile the block of code. This makes
it simpler to switch to and from debug and testing and production
code. E.g,

#CONST DEBUG = True
' lots of code
#If DEBUG = True Then
' diagnostic and trace code
#End If

Simply changing the single #CONST line can cause many debug and trace
blocks of code to be excluded in the production version. Much simpler
than try to remember which lines of code should be commented at
release.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




Hia!

How is the day going friends!

I am stuck into another crazy programming thing!!!

3 questions;

I know normal VBA if statements and Excel or SQL ifs. However;

What is if (something) ---- why do you need bracket?
What is if xxxxx = 0& ----- why do you need “&â€
What is #if ----- why that “#â€

Could someone explain?
.
 
R

Rick Rothstein

To get to the help files for the conditional If statement, type #If in any
code window and, with the text cursor in or next to it, press F1. You can
also get to the help files for the conditional Const statement by typing
#Const in any code window and, with the text cursor in or next to it, press
F1. Don't forget to click the Example links so you can see the example of
these keywords in use.

--
Rick (MVP - Excel)


Nedan Nedzatra said:
Hia!

Chip!

Thank You.

I am Ok with the first 2 now. Could you please extend the 3rd as you
explained the second? I do not think I could get assistance on this from
VBA
help; I am trying

Chip Pearson said:
What is if (something) ---- why do you need bracket?

You don't need the outer enclosing parentheses, but they are harmless
if included. Some programmers use them as a matter of personal coding
style, but they are not required to do so.
What is if xxxxx = 0& ----- why do you need “&â€

In this context, the & character forces the compiler to use a Long
integer data type (32-bit signed) rather than a short Integer (16-bit
signed). It is required for some calculations whose inputs and result
may fit in a short integer but whose intermediate calculations
overflow a short Integer. For example, consider the following code:

Dim X As Integer
Dim Y As Integer
X = 30000
Y = (X * 2) / 2
Debug.Print Y

Here, the inputs and output of the calculation are within the bounds
of a short Integer (+/- 32K), but the intermediate calculation (X*2)
overflows a short integer and the code will blow up with an overflow
error. If you change the line of code to

Y = (X * 2&) / 2

the & character forces the compiler to use a Long integer when
calculating X*2, so there is no overflow error.

What is #if ----- why that “#â€

The # character is for "conditional compilation". This allows you to
exclude blocks of the code from being compiled. In the end, it isn't
really any different than commenting out the same block of code, but
is simpler. For example, when you are developing and debugging a
program, you can set a DEBUG flag with the #CONST statement and then
test that flag to compile or not compile the block of code. This makes
it simpler to switch to and from debug and testing and production
code. E.g,

#CONST DEBUG = True
' lots of code
#If DEBUG = True Then
' diagnostic and trace code
#End If

Simply changing the single #CONST line can cause many debug and trace
blocks of code to be excluded in the production version. Much simpler
than try to remember which lines of code should be commented at
release.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




Hia!

How is the day going friends!

I am stuck into another crazy programming thing!!!

3 questions;

I know normal VBA if statements and Excel or SQL ifs. However;

What is if (something) ---- why do you need bracket?
What is if xxxxx = 0& ----- why do you need “&â€
What is #if ----- why that “#â€

Could someone explain?
.
 
N

Nedan Nedzatra

Hia!

Rick!

Thanks.

I think now I am OK with #If but still I may be forming wrong ideas about
#Const;

Last night I saw a set of codes from that I understand #Const is turned off
that is if it is set to "False" then the code execution takes a short cut
without going through the #if block. As Chip said I can turn it on for my
work and turn it off when I give it to customer.

Do I make sense...

Then I saw #Const Debugversion = 1 ....

Is that meant for #If Debugversion then.....(1 just stand for true)

Then I saw #Const Debug_ = True ...

It is used with #If Debug_ then....

If I am saying something wrong please give a warning;

Thank You.


Rick Rothstein said:
To get to the help files for the conditional If statement, type #If in any
code window and, with the text cursor in or next to it, press F1. You can
also get to the help files for the conditional Const statement by typing
#Const in any code window and, with the text cursor in or next to it, press
F1. Don't forget to click the Example links so you can see the example of
these keywords in use.

--
Rick (MVP - Excel)


Nedan Nedzatra said:
Hia!

Chip!

Thank You.

I am Ok with the first 2 now. Could you please extend the 3rd as you
explained the second? I do not think I could get assistance on this from
VBA
help; I am trying

Chip Pearson said:
What is if (something) ---- why do you need bracket?

You don't need the outer enclosing parentheses, but they are harmless
if included. Some programmers use them as a matter of personal coding
style, but they are not required to do so.

What is if xxxxx = 0& ----- why do you need “&â€

In this context, the & character forces the compiler to use a Long
integer data type (32-bit signed) rather than a short Integer (16-bit
signed). It is required for some calculations whose inputs and result
may fit in a short integer but whose intermediate calculations
overflow a short Integer. For example, consider the following code:

Dim X As Integer
Dim Y As Integer
X = 30000
Y = (X * 2) / 2
Debug.Print Y

Here, the inputs and output of the calculation are within the bounds
of a short Integer (+/- 32K), but the intermediate calculation (X*2)
overflows a short integer and the code will blow up with an overflow
error. If you change the line of code to

Y = (X * 2&) / 2

the & character forces the compiler to use a Long integer when
calculating X*2, so there is no overflow error.


What is #if ----- why that “#â€

The # character is for "conditional compilation". This allows you to
exclude blocks of the code from being compiled. In the end, it isn't
really any different than commenting out the same block of code, but
is simpler. For example, when you are developing and debugging a
program, you can set a DEBUG flag with the #CONST statement and then
test that flag to compile or not compile the block of code. This makes
it simpler to switch to and from debug and testing and production
code. E.g,

#CONST DEBUG = True
' lots of code
#If DEBUG = True Then
' diagnostic and trace code
#End If

Simply changing the single #CONST line can cause many debug and trace
blocks of code to be excluded in the production version. Much simpler
than try to remember which lines of code should be commented at
release.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




On Tue, 12 Jan 2010 14:20:01 -0800, Nedan Nedzatra

Hia!

How is the day going friends!

I am stuck into another crazy programming thing!!!

3 questions;

I know normal VBA if statements and Excel or SQL ifs. However;

What is if (something) ---- why do you need bracket?
What is if xxxxx = 0& ----- why do you need “&â€
What is #if ----- why that “#â€

Could someone explain?
.

.
 
R

Rick Rothstein

Let's see if this helps. Constants defined by a #Const statement can only be
seen by a #If statement block and not by normal code.

So, if you have this statement in your (General)(Declaration) section of
your code window...

#Const CompilerConstant As Long = 123

and then try to use this in normal code, you won't see the assigned value.
For example...

If CompilerConstant = 123 Then
MsgBox "Yes"
Else
MsgBox "No"
End If

This statement will **not** display the "Yes" MessageBox because a normal If
statement (that is, an If statement without a # sign) is not aware of the
#Const assignment. However, just add the # signs...

#If CompilerConstant = 123 Then
MsgBox "Yes"
#Else
MsgBox "No"
#End If

and now the "Yes" MessageBox is displayed.

If you use an Option Explicit statement so that you are warned about
undeclared variables, VB will help keep you straight on this (if you tried
to use CompilerConstant in normal code, you would get "Variable not defined"
warning); however, if you don't use an Option Explicit statement, you can
easily make mistakes when misusing #Const constants because you will not get
the warning and VB will just create a variable named CompilerConstant and
default it to a value of 0 (yes, you can have a #Const and a variable both
with the same name).

--
Rick (MVP - Excel)


Nedan Nedzatra said:
Hia!

Rick!

Thanks.

I think now I am OK with #If but still I may be forming wrong ideas about
#Const;

Last night I saw a set of codes from that I understand #Const is turned
off
that is if it is set to "False" then the code execution takes a short cut
without going through the #if block. As Chip said I can turn it on for my
work and turn it off when I give it to customer.

Do I make sense...

Then I saw #Const Debugversion = 1 ....

Is that meant for #If Debugversion then.....(1 just stand for true)

Then I saw #Const Debug_ = True ...

It is used with #If Debug_ then....

If I am saying something wrong please give a warning;

Thank You.


Rick Rothstein said:
To get to the help files for the conditional If statement, type #If in
any
code window and, with the text cursor in or next to it, press F1. You can
also get to the help files for the conditional Const statement by typing
#Const in any code window and, with the text cursor in or next to it,
press
F1. Don't forget to click the Example links so you can see the example of
these keywords in use.

--
Rick (MVP - Excel)


message
Hia!

Chip!

Thank You.

I am Ok with the first 2 now. Could you please extend the 3rd as you
explained the second? I do not think I could get assistance on this
from
VBA
help; I am trying

:


What is if (something) ---- why do you need bracket?

You don't need the outer enclosing parentheses, but they are harmless
if included. Some programmers use them as a matter of personal coding
style, but they are not required to do so.

What is if xxxxx = 0& ----- why do you need “&â€

In this context, the & character forces the compiler to use a Long
integer data type (32-bit signed) rather than a short Integer (16-bit
signed). It is required for some calculations whose inputs and result
may fit in a short integer but whose intermediate calculations
overflow a short Integer. For example, consider the following code:

Dim X As Integer
Dim Y As Integer
X = 30000
Y = (X * 2) / 2
Debug.Print Y

Here, the inputs and output of the calculation are within the bounds
of a short Integer (+/- 32K), but the intermediate calculation (X*2)
overflows a short integer and the code will blow up with an overflow
error. If you change the line of code to

Y = (X * 2&) / 2

the & character forces the compiler to use a Long integer when
calculating X*2, so there is no overflow error.


What is #if ----- why that “#â€

The # character is for "conditional compilation". This allows you to
exclude blocks of the code from being compiled. In the end, it isn't
really any different than commenting out the same block of code, but
is simpler. For example, when you are developing and debugging a
program, you can set a DEBUG flag with the #CONST statement and then
test that flag to compile or not compile the block of code. This makes
it simpler to switch to and from debug and testing and production
code. E.g,

#CONST DEBUG = True
' lots of code
#If DEBUG = True Then
' diagnostic and trace code
#End If

Simply changing the single #CONST line can cause many debug and trace
blocks of code to be excluded in the production version. Much simpler
than try to remember which lines of code should be commented at
release.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




On Tue, 12 Jan 2010 14:20:01 -0800, Nedan Nedzatra

Hia!

How is the day going friends!

I am stuck into another crazy programming thing!!!

3 questions;

I know normal VBA if statements and Excel or SQL ifs. However;

What is if (something) ---- why do you need bracket?
What is if xxxxx = 0& ----- why do you need “&â€
What is #if ----- why that “#â€

Could someone explain?
.

.
 

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