Microsoft Office VBA Language Reference - ebook.pdf

(1901 KB) Pobierz
199721633 UNPDF
Microsoft Visual Basic for Applications Language Reference
Page 1 of 1081
Microsoft Visual Basic for
Applications Language Reference
– Operator
Description
Used to find the difference between two numbers or to indicate the negative value of a numeric
expression.
Syntax 1
result = number1–number2
Syntax 2
–number
The – operator syntax has these parts
Part
Description
result
Required; any numeric variable.
number Required; any numeric expression.
number1 Required; any numeric expression.
number2 Required; any numeric expression.
Remarks
In Syntax 1, the – operator is the arithmetic subtraction operator used to find the difference
between two numbers. In Syntax 2, the – operator is used as the unary negation operator to
indicate the negative value of an expression.
The data type of result is usually the same as that of the most precise expression. The order of
precision, from least to most precise, is Byte, Integer, Long, Single, Double, Currency, and
Decimal. The following are exceptions to this order
file://C:\temporary\~hhE561.htm
3/20/2000
199721633.001.png 199721633.002.png
Microsoft Visual Basic for Applications Language Reference
Page 2 of 1081
If
Then result is
Subtraction involves a
Single and a Long,
converted to a Double.
The data type of result is a
Long, Single, or Date
variant that overflows its
legal range,
converted to a Variant
containing a Double.
The data type of result is a
Byte variant that overflows
its legal range,
converted to an Integer
variant.
The data type of result is
an Integer variant that
overflows its legal range,
converted to a Long
variant.
Subtraction involves a Date
and any other data type,
a Date.
Subtraction involves two
Date expressions,
a Double.
One or both expressions are Null expressions, result is Null. If an expression is Empty, it is
treated as 0.
Note The order of precision used by addition and subtraction is not the same as the order of
precision used by multiplication.
See Also
Operator precedence.
Example
This example uses the – operator to calculate the difference between two numbers.
Dim MyResult
MyResult=4-2
' Returns 124.45.
#Const Directive
file://C:\temporary\~hhE561.htm
3/20/2000
MyResult = 459.35 - 334.90
'Returns 2.
199721633.003.png
Microsoft Visual Basic for Applications Language Reference
Page 3 of 1081
Description
Used to define conditional compiler constants for Visual Basic.
Syntax
#Const constname = expression
The #Const compiler directive syntax has these parts
Part
Description
constname Required; Variant (String). Name of the
constant; follows standard variable naming
conventions.
expression Required. Literal, other conditional
compiler constant, or any combination that
includes any or all arithmetic or logical
operators except Is.
Remarks
Conditional compiler constants are always Private to the module in which they appear. It is not
possible to create Public compiler constants using the #Const directive. Public compiler
constants can only be created in the user interface.
Only conditional compiler constants and literals can be used in expression. Using a standard
constant defined with Const, or using a constant that is undefined, causes an error to occur.
Conversely, constants defined using the #Const keyword can only be used for conditional
compilation.
Conditional compiler constants are always evaluated at the module level, regardless of their
placement in code.
See Also
#If...Then...# Else directive, Const statement.
Specifics (Microsoft Access)
In Microsoft Access, you can define a public conditional compiler constant in the Declarations
section of a module. A public compiler constant is applicable to all modules in the current
database, but not in any other database.
You can also declare a public compiler constant by clicking Options on the Tools menu, then
clicking the Advanced tab. Enter the constant in the Conditional Compilation Arguments box.
file://C:\temporary\~hhE561.htm
3/20/2000
Microsoft Visual Basic for Applications Language Reference
Page 4 of 1081
For example, you might enter the following expression into the Conditional Compilation
Arguments box.
conDebug = 1
You can use a public compiler constant in code in any module. For example, you can create an
#If...Then...#Else construct to optionally run debug code.
#If conDebug = 1 Then
.
' Run debug code.
.
.
#Else
.
.
.
#End If
' Run streamlined code.
To create multiple public conditional compilation constants, declare them on separate lines in
the Declaration section. In the Conditional Compilation Arguments box, separate them with
colons. For example, you can enter the following conditional compilation constants in the
Conditional Compilation Arguments box.
conActiveLanguage=1:conDebug = 1
You can use a logical operator to include both constants in an #If...Then...#Else construct. In
the following example, the first segment of code runs only if both constants are currently equal
to 1, so that the expression containing the constants is true.
#If (conActiveLanguage = 1 And conDebug = 1) Then
.
' Run debug code for the active language version.
.
.
#Else
.
.
.
#End If
' Run another code segment.
If either or both constants have a different value, then the code in the #Else portion of the
construct runs. To change which block of code runs, you can simply change the values of the
constants.
Notes
A conditional compilation constant is always evaluated with a text-based string
comparison method. This evaluation is equivalent to having an Option Compare Text
statement in the module and occurs even if the module contains an Option Compare
Database statement.
When writing code for conditional compilation, it may be less confusing to view one
procedure at a time rather than all procedures in the module. To change how you view
your code, click Options on the Tools menu, then click the Module tab. Under Code View,
clear the Full Module View check box.
Example
This example uses the #Const directive to declare conditional compiler constants for use in
#If...#Else...#End If constructs.
file://C:\temporary\~hhE561.htm
3/20/2000
Microsoft Visual Basic for Applications Language Reference
Page 5 of 1081
#Const DebugVersion = 1
' Will evaluate true in #If block.
#If...Then...#Else Directive
Description
Conditionally compiles selected blocks of Visual Basic code.
Syntax
#If expression Then
statements
[#ElseIf expression-n Then
[elseifstatements]]
[#Else
[elsestatements]]
#End If
The #If...Then...#Else directive syntax has these parts
Part
Description
expression
Required. Any expression, consisting
exclusively of one or more conditional
compiler constants, literals, and
operators, that evaluates to True or
False.
statements
Required. Visual Basic program lines or
compiler directives that are evaluated if
the associated expression is True.
expression-n
Optional. Any expression, consisting
exclusively of one or more conditional
compiler constants, literals, and
operators, that evaluates to True or
False.
file://C:\temporary\~hhE561.htm
3/20/2000
199721633.004.png
Zgłoś jeśli naruszono regulamin