3.1.
, , VBA. , , : , . , VBA , , .
, Visual Basic, . , (C++, Java, Delphi, VBScript JavaScript, Perl . .) . , , , , - , . , VBA .
, VBA.
VBA, ( Visual Basic for Applications), Visual Basic.
:
VBA ;
, (') REM;
(");
VBA (, , ) 255 ;
( , C, Java, JavaScript, );
( 308 ). :
MsgBox " 1": MsgBox " 2"
:
MsgBox " " _
& vUserName
3.2.
VBA. , VBA - .
VBA 7. : (+), (−), (*), (/), :
(^). , 2^3 = 8;
(\). , ( ) . , 5\2 = 2;
(Mod). , . , 5 Mod 2 = 1.
|
|
VBA . :
Let nVar = 10
:
nVar = 10
. " nVar 10", :
If (nVar = 10)
" nVar 10".
, .
VBA 8:
(=). , If (nVar = 10);
, (>) , (<). , If (nVar > 10);
(>=) (<=). , If (nVar >= 10);
(<>). , If (nVar<>10);
(Is). , . , If (obj1 is obj2);
(Like). , .
True ( ) False ( ).
:
;
;
/ . , ,
Option Compare Text
Like :
1 Like 2
1 VBA, 2 , Like. (. 3.1).
3.1. Like
# | 0 9 |
* | ( ) |
? | |
[a,b,c] | |
[!a,b,c] | , |
:
And . ;
Or . ;
Not . True, ;
Xor . E1 Xor E2 True, E1 = True E2 = True, False;
Eqv , True, ;
Imp , E1 Imp E2 False, E1 = True E2 = False, True.
And, Or, Not, .
VBA , . . . VBA (+) (&). (&), :
(&) - ;
(+) Null Null.
(&):
MsgBox " " & vUserName
|
|
: , , . , . .
3.3.
. . - , . , " " . VBA :
Dim nMyAge As Integer
nMyAge = nMyAge + 10
MsgBox nMyAge
. - :
Dim nMyAge As Integer
.
Dim . VBA 4 .
Dim . Dim , , .
Private VBA , Dim. ( ).
Public , . , Dim.
Static . , , . - . :
Static nVar1 As Integer
nVar1 = nVar1 + 1
MsgBox nVar1
, Dim.
(nMyAge) ( , ) . VBA (, , , . .):
;
( );
255 ;
;
(, ) .
VBA , . ( Microsoft, Charles Simonyi, ):
, . , :
str ( s) String, ;
fn ( f) ;
sub ;
c ( ) ;
b Boolean, (True False);
d ;
obj ( o) ;
n ;
, :
MsgBox objMyDocument.Name
Sub CheckDateSub()
VB Const, , , , COMPANY_NAME.
( Const .
|
|
(As Integer) . , .
VBA :
:
Byte 0 255;
Integer −32 768 32 767;
Long −2 147 483 648 2 147 483 647;
Currency 19 , 4 ;
Decimal 29 ( 0 28 );
Single Double (Double 2 ));
! Decimal (, Dim n As Decimal) . Decimal, Variant (Dim n), Variant VBA . Variant , Decimal, VBA . , Decimal - Variant CDec().
(String ( 2 ) ( 65 400 ));
(Date 01.01.100 31.12.9999);
(Boolean True False);
(Object );
Variant , .
, Type. ( ).
, :
, . - ;
, (Single Double). , , ;
((%) Integer, ($) String . .). , :
' Dim nVar1 As Integer
:
nVar1% = nVar1% + 1
.
. , :
Dim nVar1
Variant.
, VBA . ,
nVar1 = nVar1 + 1
MsgBox nVar1
. , Variant. ! . :
: (, );
, . , Excel :
|
|
:
Dim oWbk As Workbook
Set oWbk = Workbooks.Add
:
Set oWbk = Workbooks.Add
oWbk .
. ( ):
Option Explicit
, Options Require Variable Declarations ( Tools | Options, Editor).
, , :
Dim n
n = n + 1
MsgBox
. . - : n N, . . VBA, , Variant, . . Option Explicit .
, , :
Dim nVar1 As Integer, s1 As String
:
nVar1 = 30
, :
nVar1 = nVar1 + 1
"", "".
:
:
sVar1 = "Hello"
/ "" (#):
dVar1 = #05/06/2004#
, / "" : 05 , 06 . (, ) ;
, (&H):
nVar1 = &HFF00
( ) (&O).
:
0;
"" ( );
ASCII 0 ( );
Variant Empty. (. . ) IsEmpty();
Object ( ).
3.1.
:
1. Excel C:\LabVariablesOperators.xls. A1, A2 A3 .
2. Visual Basic Excel .
3. Tools | References Microsoft Word 11.0 Object Library.
4. :
Public Sub FromExcelToWord()
MsgBox Range("A1").Text
MsgBox Range("A2").Text
MsgBox Range("A3").Text
Dim oWord As Word.Application
Dim oDoc As Word.Document
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add()
oDoc.Activate
oWord.Selection.TypeText " "
End Sub
A1, A2 A3, Word " ".
5. , .
:
, " " A1, A2 A3 .
3.1
:
Public Sub FromExcelToWordAnswer()
Dim sA1, sA2, sA3, sText As String
sA1 = Range("A1").Text
sA2 = Range("A2").Text
sA3 = Range("A3").Text
sText = sA1 + " " + sA2 + " " + sA3
Set oWord = CreateObject("Word.Application")
|
|
oWord.Visible = True
Set oDoc = oWord.Documents.Add()
oDoc.Activate
oWord.Selection.TypeText sText
End Sub