Вы находитесь на странице: 1из 449

by

Steve

Cummings

Hungry Minds

HUNGRYMINDS,INC.
Bes-tSe
nilgBooksDg
ita
i lDownoladseB
-ooksAnswerNew
t ok
rse-Newsele
trsBa
rndedWebSe
tiseL
-ean
rmg
NewYork,NY*Ce
l vea
l nd,OHIndianapolis,IN

-
2002

32.973.26-018.2.75
18
681.3.07
""
. . .
.
"'
: info@dialektika.com, http://www.dialektika.com

, .
18

VBA "", 3- . : . . . :
"", 2001. 448 . : . . . .
ISBN 5-8459-0230-4 (.)
VBA
VBA , , .
,
.
, Office XP, .
,
Windows, VBA.
32.973.26-018.2.75

.

, , ,
Hungry Minds, Inc.
Copyright 2002 by Dialektika Computer Publishing.
Original linglish language edition copyright 2001 by Hungry Minds. Inc.
All rights reserved including the right of reproduction in whole or in part in any form.
This edition published by arrangement with the original publisher. Hungry Minds. Inc.
For Dummies and Dummies Man are trademarks under exclusive license to Hungry Minds, inc. Used by
permission.

ISBN 5-8459-0230-4 (.)


ISBN 0-7645-0856-3 (.)

"". 2002
Hungry Minds. Inc., 2001

18

I. VBA

23

1. , !

25

2. ,

41

3. VBA

50

4. VBA-

71

5. Visual Basic

86

II. VBA

113

6. VBA-

115

7.

140

8.

164

9. "" : 185
10. VBA-

209

III.
V B A

253

11. VBA

255

12. -

283

13. :

303

IV. VBA

321

14. VBA Office

323

15. VBA Word

333

16. VBA- Excel

350

17.

364

18.

386

19. VBA-

390

V.

407

20. ( ) VBA
21. VBA

409
427

433


...

I, " VBA"
II, " "
III. " HaVBA'
IV. " VBA"
V. " "

,
Web- VBA ""

I. VBA
1 . , - !
VBA?
VBA - ,
VBA-
VBA-
VBA
Visual Basic ?


, VBA



BASIC
VBA
VBA
ActiveX
- VBA
VBA (, )
56
"" VBA
VBA Visual Basic

2. ,


- ,


16
17
17

18
18
18
19
19
19
20
20
20
20
21
22

23
25
25
26
27
27
28
28
29
29
30
30
31
32
33
34
35
35
37
38
38
39
39

41
41
42
42
42
43
43



, , ?





Visual Basic

43
44
44
45
45
45
46
47
47
48

3. VBA
Visual Basic

50
50

Visual Basic
Visual Basic
!
VBA
VBA-

-
VBA

VBA-
1- . -
2- .
3- .
4- .
VBA-

50
51
53
53
54
54
55
56
57
58
58
59
67
69
69

VBA-

70

4. VBA-

71
71
72






Word, Excel Access
Visio

72
73
73
74
75
80
83

VBA-

5. Visual Basic
Visual Basic





, -


83

86
86
87
87
88
91
93
93
93
94
95

II. VBA
6. VBA-



VBA
, ?


VBA-





Sub
Function









VBA


96
96
96
98
100
100
101
102
102
103
103
103
103
107
110
110
110
111

113
115
115
115
116
117
117
118
118
118
119
119
119
120
121
121
123
124
126
127
127
127
128
128
129
129
130
131
132
133
133
134

-
!




Rem

7.








--











Variant




-

8.



!




If...Then
If...Then
if...
If...Then...Else
If...Then
Select Case

135
135
136
137
138
138
139

140
140
141
141
142
142
145
146
147
148
149
149
150
150
151
151
153
154
156
156
156
156
157
158
159
160
161
162
163

164
164
165
165
165
166
166
166
167
168
168
169
169
169
172

Select Case
Select Case
Case Bse
Case

Do
For...Next
Go To
Go
Go

172
173
173
174
174
175
180
183
183
184

9. "" : 185



, ,

!



Immediate
Locals



: Watches

Add Watch


"" On Error Err


10. VBA-


~






UserForm
, !
Enabled Locked



10

185
186
186
186
187
188
192
194
195
196
197
198
199
199
200
201
201
202
203
204
204
204
204

209
209
209
210
210
211
212
214
216
221
222
225
226
226
227
228
230







, !


,..
Change



231
233
234
235
237
238
239
239
242
245
248
248
249
250
250

III.
VBA

253

11. VBA

255

255


Format
Format

llf

256
257
257
258
259
260

Len Left










260
261
261
262
264
265
265
267
267
270
270
273
274
274
274
277


12. -

VBA-


279
283
283

283
284
285
285

11





VBA -





-


,






Is

With
For Each...Next

13. :






...

Collection

Collection









,

12

286
286
287
287
287
288
288
289
289
290
290
291
291
292
292
292
292
293
294
294
296
298
299
300
300
301

303
303
304
304
304
305
308
309
313
313
313
314
314
316
316
316
317
317
318
318
319
319
320

IV. VBA

321

14. VBA Office

323




Office



Excel, PowerPoint Project

15. VBA Word


Application
Word VBA


, ,

VBA



View

Selection
Range
Range
Range
Word VBA


,


VBA Word



16. VBA- Excel


Excel
Range
Range
Cells




323
323
324
327
327
327
330
331
332

333
333
334
334
334
334
335
335
336
336
336
337
337
338
339
339
340
341
342
342
344
345
345
346
346
347
348
348

350
350
351
351
352
353
354
354
356
357

13





Excel



17.

357
358
359
359
359
360
360
361
362

VBA:


SQL VBA


:

Access
ADO

ADO

Recordset
Command
SQL
SQL
SQL
SQL VBA-
SELECT
SQL

18.
-



19. VBA-










14

364
364
364
365
365
366
367
367
368
369
369
369
369
371
377
379
379
360
380
380
385

386
386
387
387
388
388

390
390
390
391
392
393
395
395
396
396
398
400
401
401

403

403
404
404

V.

407

20. ( ) VBA
Windows






VBA

VBA
Microsoft Scripting Runtime


,

Dictionary
Dictionary
,




ActiveX

ActiveX

21. VBA


Microsoft

Web-, VBA
ActiveX




, !


, !

409
409
410
411
411
411
412
412
413
413
414
414
415
415
415
416
419
420
420
420
421
421
423
424
424
426

427
427
427
427
428
428
429
429
429
430
430
431
431
431
431
432

433
15

20 ,
, , COBOL C++, VBA Visual Basic. , Office 97,
"". , , , PC World, Macworld, PC
Magazine, PC/Computing PC Week.

&
97-

Hungry Minds, :
(James Russel),
, (Jade Williams) (Kelly Oliver).
VCommunications (www. v - c o m . com) , System Commander Partition Commander. ,
beta- Microsoft Office XP.
(Lisa Robbins) Waggener Edstrom, Microsoft,
VBA,
Microsoft, .


, , , VBA
( Visual Basic for Applications, Visual Basic ).
VBA . ,
.
, , , , ! ,
. ( ) , .
, ,
. , , VBA.

/
, , , VBA , , Microsoft Office, Microsoft Project, Visio AutoCAD , , .
VBA.
:
V / VBA;
;: S ;
\ / VBA- ;
- S ;
'i / , VBAt
.
VBA 6 6.3 (VBA6.3
Microsoft Office ). Microsoft, VBA, 6 6.3, .
Microsoft , ,
. ,
VBA , VBA 6.0 VBA 6.3, .

<5
,..
. , "", , ,
Windows. , ,
, , 18

, , "", Windows "" (


Windows 95, Windows 98, Windows Me Windows 2000). , VBA,
,
VBA-. Microsoft Office, , . , , :
S Microsoft Office Word, Excel, PowerPoint, Access, Outlook
FrontPage;
S Microsoft Project;
S CorelDraw 9 10 Corel WordPerfect Office 2000;
S - iGrafx Micrografx;
* Visio 4.5, 5 2000;
^ AutoCAD R14, AutoCAD 2000 AutoCAD 2000i Windows;
S Autodesk Map;
^ TurboCAD Professional;
S M.Y.O.B. Accounting Software;
f Great Plains ( VBA);
S Omni Trader, .

(UaafWtfu
, VBA, , , ...
,
.

I. " VBA"
, , VBA.
, ,
, VBA- .
, .

II. " VBA"


Visual Basic , , VBA , , , , . . . ,
,

19

, .
II , ,
, , .

III. " VBA"


. , ,
VBA. VBA-
, , . VBA
. ,
, , Word,
Excel CorelDraw. ,
.

IV. " VBA"


, Office, ,
Office .
Word Excel, ,
VBA-, , .

V.""
VBA , .
. , , ,
. VBA, , .
, VBA-, ,
.

//
, , .
,
, :
VJhatEverltls -Color = "Chartreuse"
, : Debug. P r i n t .
,
.
,
. ,
, ( ).
20

, ,
, .
, , "" Internet
www. d i a l e k t i k a . com. ,
, .
VBA- ( ) , .
, , VBA (__).
.
I S , ,
|
" ^ " . , i .
I S

1S

, , .

I^

, , .

IS

, , , , . ,
.
,
.

, , ,
.

,
, , . , , ,

"", , .

21

Web'C// ">#"
Web-,
:
www.seldenhouse.com/vba
:
"' / ;
J , - ;
S

, ,
;

S , VBA, ,
VBA-.

, . , - ,
, Web-,
.

22

VBA

", ! Mbt , ,
.
, !'

& ...
I ,
VBA.
,
, VBA
. . ,
.
2-5 ,
VBA- , .
,
VBA- .
, - , '

. ,
.

, - !
/3 1 ...
>

VBA-

>

VBA-

>

, ,

>

VBA ,

*
, ,
^ ^ , .
-, .
- VBA.
, VBA , ,
.

/
VBA , , ,
VBA, , .
- , ,
. Visual Basic VBA .
, VBA
. , , (,
), < ,
.
VBA , . , VBA .
| S

, ,
- .
,
? , ,
!

1. , -

25

I ^ .
I
, I
?
1 V - , I
.
V
?
VBA ;
, VBA
. , " , VBA",
.
, - VBA? : VBA ,
. VBA , VBA. (, VBA-, ,
VBA-. "VBA
(, )".)
VBA, Visual Basic.
Microsoft, Visual Basic
.
, VBA- ,
,
VBA . DLL ( ),
VBA , \ P r o g r a m Files\Common F i l e s \
Microsoft
SharedWBA.

/fio tuMtfftcu, JC

, VBA .
: VBA , . (
VBA- , VBA, , VBA,
V-.)
Microsoft , , VBA VBA : Office 97, Office 2000, Office , Word, Excel, Access PowerPoint. Outlook,
Microsoft Office, VBA, VBScript. , Outlook 2002 (
Outlook 2000) VBA Outlook. VBA

26

I. VBA

FrontPage, Web- Web Microsoft, Visio ,


Microsoft Project.

VBA-
Microsoft VBA. VBA, .
| S
\

Corel WordPerfect Ofilce 2000, Microsoft Office, ,


, .

| ^
S
|

CorelDraw. , CorelDraw

, ,
Web-.

| S
I

AutoCAD. Computer Aided Design;


VBA-.

I S M.Y.O.B. Accounting. .
I ^
I

Micrografx iGrafx. - -, .., -.

\ S OmniTrader. .
, ,
VBA, VBA. , , VBA-,
VBA- He-VBA- .

VBA-
, , ,
, .
YBA .
Microsoft Office,
Office . , VBA- (
WordPerfect, Visio, AutoCAD).
, VBA-
. , . , ,
. VBA ,
, , , ,
, .
VBA , .

1. ,

27

, . VBA,
,
JTOM .

/
, , . , ,
. , .
S , .
S .
^ , , .
. , , -
.
VBA , ,
.
, . VBA . VBA-:
Sub S e l e c t N e x t Q u e s t i o n ( )
If S e l e c t i o n S t r a t e g y = Randomly Then
AskRandomQuestion
E l s e If C u r r e n t Q u e s t i o n = T o t a l Q u e s t i o n s Then
CurrentQuesti.cn = 1
Else
C u r r e n t Q u e s t i o n = C u r r e n t Q u e s t i o n +1
End If
End Sub
, , .
VBA ,
. , ,
, VBA-.

Visual Basic ?
, VBA . , ,
.
VBA , ( , , , ..), . VBA . VBA
, .

28

I. VBA

,
, , , .
, VBA , .
, ,
. , , ,
.


VBA , ,
. .
, .
, , .
, . , , ,
, VBA. ,
, , - , . , , ,
.
10.


,
.
1. .
, , .
2. .
. 1 , , . .
3. .
, , , ,
, . , .
4.

.
,
( , VBA ,
).

5.

.
. 3 4 , .

, , , , VBA.

1. ,

29

JIoqfwSnee

&

, " VBA?", , VBA


. ,
, - VBA.
, : , VBA,
, .
, ,
, .


, VBA,
, .
( , ,
, , , , ,
.) VBA , .
, , , . . 1.1 Microsoft Word
VBA .

iTlp

/ tiJU- J M l f T . . .

, ; . : -

-,.;.

* ! * <|

IM
I "Uu
LMKpJK
.D

, 1 0

II!

) l l ^ b . l . ^ >11 V E A ^ : B L
: = ' jsi;u.:Keicn?.-K=m.Ki4f<4

c-pai.

i u o c p j ' . n - n o - n . 4 - K 4 L i i u e l i -
: > T T Y 7 V H ^ H K

H i

ESfiiLL

. /. /. VBA, Word ,

30

I. VBA

, , ,
VBA
.
.
VBA . , VBA. , , VBA
- , .
, , , . VBA , , , .
VBA, VBA. VBA- , , ,
( VBA ).


VBA , . , ,
, . ,
? ,
, VBA-, .
VBA- , .
VBA- .
, Microsoft Word
.
, , ,
. VBA ,
( Word ,
/, , ). VBA-
:
P u b l i c Sub C o p y B l o c k T o F i l e { )
Selection.Range.Copy
'
'
Documents.Add
'
Selection.Range.Paste
'
'
1

1. ,

31

Dialogs(wdDialogFileSaveAs).Show

ACtiveDocument.Close
End Sub

, VBA , VBA- . ,
,
. , .
S VBA- ,
, .
Word, , ,
;
, ,
X
. ,
'
, .

|- S
:.
S

'-

VBA- ( ) .
VBA- ,
(. 1.2). VBA ,
, .

. S VBA- . (, , |
, .)

, :.

My*

. 1.2.
, ,

VBA- , .


VBA-, , ,
.

32

I. VBA

,
; VBA ,
, .
, VBA, VBA-. (
VBA- VBA ). , .
. ,

VBA-.
, VBA .
, .
VBA- (
, Visual Basic ). ,
, " VBA , Visual Basic".

tnotii
, VBA , BASIC. , , , , ,
.
BASIC 1960-
. BASIC Beginner's Allpurpose Symbolic Instruction Code. ,
C++ FORTRAN, BASIC
, .
"" , VBA,
BASIC . ""
VBA , , BASiC.
VBA BASIC, . VBA,
BASIC . , VBA
, BASIC, ,
.
, VBA , , , (
" VBA"). VBA -
, BASIC , . , VBA .

1. ,

33

VBA
VBA .
, ,
, .
, VBA, Visual
Basic. Visual Basic
, , , .
. 1.3 Visual Basic, Visio,
, , VBA-.
VBA- Visual Basic (
, " "" VBA").

Jj

. 1.3. Visual Basic Visio


Visual Basic 5 . ,
, .
S ( ), .

34

I. VBA

, , ,
( "
ActiveX ").
, VBA,
.
VBA, - .
,
, (
, ).

VBA
VBA .
, VBA-. ,
VBA-.
, , VBA-,
, , , , . ,
,
.
VBA-
. VBA-
, . , , , . ,
VBA-. VBA- . 2.

ActiveX
Windows ,
- , .
, ,
, ,
.
. 1.4.
VBA . Windows
. .
(, ),

1. ,

35

( , . 1.5), "" .
, ActiveX, , VBA. Microsoft, ActiveX
, ,
. , VBA,
ActiveX, , , . ActiveX VBA-, , C++ Java.
VBA-
ActiveX, ( 14). ,
, VBA.

Tins b a frame control

Frame of thetk btraes

<*" Happy option button

P Porridge

sad option button

" Curds and whey


l - F Cherry pie

- -<~ Intffferent

numb* of copies Pamsparm

*".

jn.^ "-^vji

m * a tabs! in
multipage control

. 1.4.

. 1.5.
VBA

, , , . Windows , -

I. VBA

ActiveX. , , , , .
VBA , . , , . 1.6,
ActiveX, , . , . ,
. ,
, VBA ! ,
, ?
ActiveX
(
5

In the value

C6

10

tt

13

14

18

17

20

21

24

27

28

23

30

31

6Be
12
53
2
6

| J1B99

Cp
1

Make it

. 1.6. , VBA
ActiveX ,
,
. ActiveX 14.
ActiveX. Visual Basic 5 6 ( , ),

- VBA
Visual Basic () .
VBA, . 12,
.

1. ,

37

VBA (, )
VBA , , ..
VBA-. VBA-, , Microsoft Word, Visio,
AutoCAD VBA-.
, Visual Basic
VBA- (, , Visual Basic). Visual Basic . , , ( ,
, ). VBA . , , VBA . , VBA, . ,
. , Din (, ,
, ,
). , VBA- Dim MyVariable as I n t e g e r
M y V a r i a b i e ,
.
{ ), , VBA- .
, , VBA-
VBA-, . , , VBA- , , VBA .
, , , Visio. , Word. , VBA- , .
. ,
Word, Excel PowerPoint Microsoft Office VBA-. ,
!
, VBA VBA. ,
()
(). ,
12.

VBA ,
Office 2000, iGrafx Microgra/x, Corel WordPerfect

38

I. VBA

2000 CorelDraw, V'BA 5 Microsoft Office 97, VBA-


VBA. , .
.
^

VBA 5 WinHelp,
VBA HTML Help
Microsoft ( , WinHelp}.
V B A 6 VBA 13 ,
. VBA , VBA 6,
11.
VBA 6 , VBA 5,
. 10.

VBA 6
) , .

"" VBA
VBA- " "" ,
Visual Basic, VBA. '"" VBA- Visio, WordPerfect, AutoCAD , , '" " Microsoft Word, Excel PowerPoint.
Microsoft Access VBA-. Access
, , VBA-. Access ( ) VBA. Access 2002 Visual Basic, Access 97 . VBA-, .
(, Internet Explorer Outlook) VBA, VBScript. VBScript- Visual Basic, . .

VBA Visual Basic


, VBA , , , VBA, ,
, Visual Basic.

VBA , Visual Basic


, VBA , , VBA Visual Basic VBA-
.

1. ,

39

: VBA-
VBA-.
( )
( ,
). ,
, , , .
, Visual Basic,
,
, ,
, VBA- VBA-.
VBA-, .
I. .
. VBA
, , , ,
.
1.
.
2. -! .
2 .
VBA- ,
, .
VBA-, . , VBA-
, .
, - , , .
, VBA (
"" VBA-), ,
VBA, , , Visual Basic.

VBA Visual Basic


VBA Visual Basic, ,
. ,
VBA Visual Basic. .
2001 Visual Basic Visual Basic.NET. ,
VBA. , VBA, Visual Basic.NET. ,
Visual Basic 5 6 ,
VBA.
, VBA Visual Basic .

40

/. VBA

,

...
>

>

> : ,
>

Visual Basic

aneM , ?
, , , . , , ,
.

VBA- Microsoft Word, Excel PowerPoint


, . , , , .
, ,
.
, . ,
, , , , ,
, .
*"*'

VBA-, , VBA-. , Visio


4.5 5 Visual Basic. , VBA Access VBA.
Access, , , VBA- ( " "). Access , .

2. ,

41


- VBA-.

VBA. VBA. , 6, - , . ( , Sub,
. ?)
: Access, VBA, Access VBA. . Access , , ,
: -.-.


. .
, , . . , , ,
- .
,
. , , \ . , ,
- , .
.

,
,
VBA. ,
- . 3 VBA ""
. , " ".


, :
, , .
, ,
,

42

I. VBA

, .
,
.
.
, . , .
, , ,
, ,
.
,
.
, .
, , Microsoft Word. , ,
. , , ,
.
. , , . ,
, .


, , . ,
.


.
| ^ ^^ . Office
:*
, |
.
I S ,
t;
. , Microsoft Office
|
Visual Basic.
.
. 2.1 Microsoft Excel (
-).


, 1 ( 2, ). , .

2. ,

43

FfOTW

P W V M

$
' : '
Orl

:
I M S K P C C

27 -002 ( )

OK

. 2.1. Excel
, , ,
. -,
.
| S , . .
S .
, , :
S
^ . , . I
. VBA I n v a l i d p r o c e d u r e
i
name ( ).
VBA 6.


, . .
i /
;i ^
\
\ S
?
I
"':

, .
, . , , .
,
. , !
.

, , ?
VBA- .
,
.
,
. , Excel , (, <CtrI+M>),
(. . 2.1). Word

44

I. VBA

,
, (. 2.2).
, ,
.
,
- . ,
,
.

(Normal .do!)

IMOTEOC sanwan 2 09 2O0;

. 2.2. Word



. , , (. 2.3). , -.

. 2.3.


,
. . , , ,
, .
, !
. , , Visual Basic (
" Visual Basic").


, , (. . 2.3). , VBA-. (

2. ,

45

, , ,
'" Visual Basic".)
Word, . , ,
( ).
, .
(. 2.4).
. ,
.

. 2.4.
Word


, , . , , ,
, , ( ,
, ,
, .)
, VBA-, , VBA-,
.
(<Ait+F8>), ,
( PowerPoint ; . 2,5). VBA- 4.

I ^-|

I"

21 -

.
= ^:-' ^^

. 2.5. (
VBA-
)

46

I. VBA

""
. , , . , , ,
, , ,
. , ,
, ,
, . :
, , , (<Ctrl+Z>).

&
//tcoqa
,
, VBA-. , . , VBA-
, , .
, ?
, VBA.
, . ,
, ,
VBA .
,
, , {.
", , ?").


Visual Basic
, VBA- (, , ).
.
^ ^ <Alt+F8>,
(. . 2.5).
2. .
, , , ,
, , , .
1.

3.

Visual Basic , . . 2.6.

2. ,

47


Visual Basic. , ,
VBA- , , .
. , .
.

Sg VBAProfKt i
JiGen.r.l)

VHAPru)M |

A;tivePindo.vie.eote51ide index :=Activee:e3er, t a t ion


ArtlveHindow.View.GotcSlide Index: = '.
Act lveWindow.viewTYpe = ppVieve'-atlme
ArrtiveFcesefitation .slides .Renge.-lect
ActiveFtesenieticn.Fonts.Replace Original:="Acial" ( F

.2.6. , - ,
. , ,
( -
) 11 . , ,
F o r . . . N e x t . ,
F o r . . . N e x t ? VBA,
8.
.
, F o r . . . N e x t , I n p u t B o x . ,
, . I n p u t B o x 11.
.
, , .
I n p u t B o x , , . ,
I n p u t B o x 11.
. , -
. I f . . . End If VBA ,

48

I. VBA

I
I
|

,
- .
I f . . . End I f 8.


VBA-, . VBA-
.

2. ,

49

VBA
/3 *
>

Visual Basic

> VBA

VBA

,
^/w VBA-. VBA-. Visual Basic VBA
VBA-. , .

fieqaxtnofta Visual Susie


Visual Basic VBA.
VBA-, VBA-, VBA. Visual Basic
5, , .
- , , ,
Visual B a s i c
. , ,
Visual Basic . VBA- .

| s ^^ Visual Basic.

' / <Alt+Fll>. , ,
I
Visual Basic.
, . 3.1.
,
Visual Basic. Tools ().

Visual Basic
Visual
Lasic. . VFi - Microsoft Office (Word. Excel
PowerPoint) Visual Basic (. 3.2).
.

50

I. VBA

. 3.1. Visual Basic


, 3.2.
Visual Basic Word
, ,
. (
, . . 3.2).
.
Visual Basic.
Visual Basic,
, . Office, ,
<Alt>.

, VBA.
Office VBA ,
VBA-.
,
,
VBA .

Visual Basic
Visual Basic . , , ? ,
( Visual Basic , , , ).
, , .

3. VBA

51

5, (. 3.1).
3.1.

Project Explorer ( )

VBA-

Code ( )

UserForm ( )

(
)

Toolbox ( )

(
} , VBA-

Properties ( )

W a t c h ( )

Locals ( )

Immediate ( )

Object Browser ( )

, VBA-, , .
. 3.3 3.4 .
- . ! - M o d u l e l (Code)
T j

| Getting StartedWHhVBA

Sub GettingStactedBithVBAO
Dim SomeMumbet As Single
Dun SomeText As T t r i n g
Dim SomeShape As AcadEntity
SomeNumber = 53456
SoraeText = "
Randomize
SomeNumber = EomeNumb&r + (1D0 * Rnd ())
End

. 3.3. , , VBA

52

I. VBA

. 3.4.

, , . , - VBA
-, .

/

, ,
, -, .
- ? , , VBA-.
Visual Basic.
, ,
.
, Visual Basic - ,
, .
, Visual Basic . , VBA. - , , ,
. , VBA
.
Microsoft "" VBA, VBA-
. ,
Office .

VBA
VBA - VBA-.
WinHelp, Windows 3.11. VBA 6 HTML H e l p Microsoft. Windows 98, HTML Help
Web-. , HTML Help, , , ,
WinHelp, , .

3. VBA

53

VBA, 1^ Microsoft Visual


Basic VBA . HTML Help VBA 6
. 3.5. , .

. 3,5. VBA 6' HTML Help

VBA-
, Visual Basic , VBA-.
. .
, VBA , .
1. , <F2> ^
( 6).
2.


VBA-.

3.

4.

<F1>
, .
, , ,
, , .


VBA Windows
WinHelp, HTML Help. .
% S , , :
.

54

I.

f S , \
, .
, ,
. , , ,
VBA Office . ,
, - ,
.

-
Visual Basic <> - , .. , ,
. , , ,
, , .
.
<F1> , (
, ). ,
, - VBA-.
For . . . N e x t , , . <FI>. ,
, . 3.6.

. 3.6. Add Visio. , ,


Add

<1> . -

3. VBA

55

( , , , , ..). ,
, <F1>. , , , <>.
<F1> .
S <>
.
, .
S , <F1>
, ,
. , , .
<F1>
, .

VBA
VBA ,
. ! , . 3.7, VBA .
, , , .

. 3.7. ,
VBA Word
. 3.2 ,
.

I.

3.2.

See also (. )

,
,

Example ()

Example,
.
. ,
VBA-
( "
")
, .
, ,

Methods, Properties, Events


(, )
Applies to ( ...)

,
, . ,
, ,


, VBA. , . ,
,
. Visual Basic
<Ctrl+V>.


, , , , ,
VBA. - , VBA
, , .
, Office ,
VBA. Office . , ,
( ?), VBA.
,
. Office
. ,
, . 3.8.
Visual Basic.

3. VBA

57

Microsoft Oilier XP (|1(1-']*||- )

<01*,
VBA &
OffK,

, J

<"

Q&-w> I

11

. 3.8 VBA, Visual Basic


Office,
, VBA Outlook
. VBA Outlook,
v a l u p a c k \ m o r e h e l p
- Office , Office.

, Visual Basic VBA, . 1, . , , . .

1- . -
, VBA- , , , . -
, ( ). , .
, ,
.
, ,
(UserForm).
.
: , , ,
.

I. VBA

,
VBA- ( ,
; 6). , .
, , .
, .
, . ,
, , ,
.
, ,
. .
, VBA- . ,
.

2- .
, , . ,
, , VBA-.


Visual Basic , , . Visual Basic InsertsUserForm, .
, ? Windows
, .
. 3.9, . , , , (

. 3.9.

3. VBA

59

). ( ) (
).
(. 3.10), , .


~
I

. 3.10. VBA
,


, .
, , , .
,
, .
. - ,
, , .
, .
1. , , .
, .
2. .
3. , , , ,
,
( . 3.11 ).

. 3.11.

I. VBA

VBA ,
, ,
L a b e l 1. , , .
, .


.
.
VBA
.
. . 3.12,
. ,
.

Label1 Label
AJM
je
tW jrateiomQedj
u
i toSzie Fssl
BackCoolr ZJ
*
eackSty-l 1 -rfi^ckStyle
(
Border Styel 0-f BcderSli
Capotin Labctl
C
rtrcJIpIe.t u
E
no
a
ru
=o
nb
teld T
Taihco"6000001^
F
o
r
e
C
o
o
l
r

e
Hegiht 13 H
eplContexD
tl 0
Lfft
ISone) >
McS
-jE
tcon (N
M
o
u
e
i
P
c
r
a
e
r
0
Fe)MoosePoi
PEu
t-e N
(-W
nPicturePo
tsLc^e
*P
o
satln^'-f
nSpecialEM
sj|P
p
E
i
f
e
0

albn
ldy .1

. 3.12.

- Name
() C a p t i o n (). L a b e l l l b l N o w
( , ).
, Name (
( N a n . e } ) , , .
, . (
, , .
6 , .)
C a p t i o n .
. ? ,
.
, , , . ,
. , . ,
.

3. VBA

61

C a p t i o n -
? VBA. .


( )
, .
- , , , - .
, . , .
1. , .
, ,
, .
2. , (. . 3.11).
3. , , .
. . 3.13.

. 3.13. ,

. ,
.
1. (Name) OKButton.
, , , .
2. C a p t i o n .
.
. VBA- -
. , <F5>
Visual Basic.
.
, VBA- ( Visual Basic;
62

I. VBA

. 3.14). , - , . , Windows
.
} > <" *

I*

3!

" 3 F 3

,*

3 Backttwndt

*4*'"

a?

iou/

,a

. 3.14.


VBA . , ,
. , , .


, .
. ,
? ,
.
.

3. VBA

63

: S View^Code .
r

J <F7>.
'' S \
View Code.

(. 3.15). VBA
. ,
- .
f l O K y M O , i T u \ V i s i o V 8 A v s d - . . .

Private

Sub OKButton_Click()

End Sub

. 3.15. VBA ,

, , VBA, .

P r i v a t e Sub O K B u t t c n _ C l i c k ( )
VBA- (
. Sub, .. ) . P r i v a t e Sub
VBA, .. ,
VBA. VBA,
, . ,
VBA 6. VBA OKButton__Click, .

End Sub
Sub.
VBA . .


VBA . ,
. :
Unload Me
64

I. VBA

U n l o a d .
Me, VBA .
.


, , , VBA. , ,
. , , U n l o a d Me
- . VBA , U n l o a d Me
.
,
. C o m p i l e e r r o r .
Expected:
end of s t a t e m e n t
( . ).
,
. , ,
.


, , .
. ,
, .
1. ,
OKButton, .
, (. 3.16).

u\VisioVE)A.vsd

- (Code)

Userrorm
(General)
IblNow
OKBiitton

Erivat-e UserForm___Activate ()
[":& Quote String
Quote = "He : "
ibJNow.Caotior, - Quote & Format (Now, "d.ddddd, hi
End 'J-Jv.

. 3.16.
, ,

2. UserForm.
VBA C l i c k ().
,
, .
, .

3. VBA

65

3. , ,
, , VBA UserForm.
,
, (. 3.17).

i.Jm
\ nanyMH
t' TM\Vsiu
iVf)A-v}"sdAdiuate
- (Code) 1-r3 j
UseiForm
1

AddCcntrci

"^

End Sub

Befc-eDtopGrPasle
Click
P r i v a t e Sub UsetForm A c t i v e Dfclick
Dim Quote As 3 t r i r . : [
Quote = "He
l b l N o w . C a p t i o n - Quote Fa
End Sub

~
i

. 3.17. ,

4. A c t i v a t e .
, .
VBA U s e r F o r m _ A c t i v a " : e ,
.
5. UserForm_Click, ,
<Del>.

. - , ,
, .


, , ,
. ,
VBA, . :
Private UserForm_Activate()
Dim Quote As String
Quote = "He : "
lblNow.Caption = Quote & Format(Now, "dddddd, hh . nun ."}
End Sub

Dim Quote As S t r i n g
Quote , .

Quote = " : "
" : " Q u o t e . , , , . ,

66

\. V

, .
,
i b l N o w . C a p t i o n = Quote 5 Format(Now,

"dddddd,

hh . nim . " )

,
. i b l N o w
, . , , l b i N o w C a p t i o n .
, . ,
, .
, .
Q u o t e .
"" VBA , , ,
Q u o t e . Now VBA,
, . F o r m a t , . ,

11.

3- .
? ,
.

Visual Basic
, ,
.
Visual Basic, .
1. , , .
, , ,
( -).
2. .
^ Run^Run Sub/UserForm .
- */ Run () Standard
() Visual Basic. ( Standard
Visual Basic , 5.)
/ <F5>.

VBA- ( Visual Basic). , ,
. 3.18.

3. VBA

67

*. 25 IBM ., IS .09*.

. 3.18. -

. 1, VB! , ( - ).

VBA- , (
1). ,
VBA.
.
. , .
Debug ().
Debug , -.
,
, ,
.
, Help (), , . End ()
Visual Basic.


, , , , , , , .
.
VB.A,
. , , VBA, .
, :
.
.
.
?
. VBA .
68

I. VBA

, . , , .
; . ,
Windows Must Die !, , . , .

4- .
( ) .
. , .
, : , , . ,
, , . ( - -,
.)
, VBA
. 14.

/$ cpoftMM
,
Visual Basic,
, , -. VBA-,
Visual Basic Sub (), ,
() . .
1. Visual Basic Insert^Module, .
2. :
Sub ShowQuote{)
UserForml.Show
End Sub
U s e r F o r m i , - ,
.
, ShowQuote ,
4. , , 10.

3. VBA

69

, VBA- Visual Basic Windows


. <Alt-Tab> , ,
. .
1 J View^AutoCAD ( , }.
:. ^ View Visual
:
Basic , .
} S <Alt+Fll>.
1 / Windows.
Visual Basic <Alt+Tab>, , )
<A!t- -Fi 1> ^ ^ Visual Basic.
Visual Basic , ,
, .
I s

F i l e o d o s e and Return to <>.

i S

<AIt+Q>.

70

I. VBA

VBA-
' 3( ...
>

> -:
>
>

VBA- ,
^ ^ . .
, ,
VBA-, .
Visual Basic
, <F5> Run () Standard ()
Visual Basic. , . ,
.
,
VBA- VBA-. ( ,
VBA-, ; 2. " , ".) , . VBA-
, ,
, , .
, , 2,
, Visual Basic,
6, II III.

VBA- .
, , . VBA- VBA. , , , ). () ,

4. VBA-

71

.. , .
- ? 6,
.
? , . VBA-,
, "" . ,
, M a i n .

/
VBA- . , , , , .


Office Visio, .
- V ^ ^ .
[ S <Alt+F8>.
VBA- , .
. 4.1. ,
, VBA-.

. 4. /.
PowerPoint

VBA-.

72

I. VBA

, , ,
Sub (.. ) .
( 2, ,
.) , F u n c t i o n (.. )
Sub . 6.


.
.
1. .
2. .
, ? .

, .
, .


VBA- ()
. , ,
.
Excel, , (.. . ),
. .
, . . 4.2.

K'tp a3

pHSCK
SofcH

;,,

-
-- - (

. 4.2. Excel - () ,

4. VBA-

73

Word , , ii .
,
, . . 4.3.

. 4.3. Word
, , ,
. ,
,
.

nftotfteuui
, , ! - , I-U-.V.'OAVUUO,
, MCIKHV , , , .
VBA-.
, . - .
, , (, , 2), VBA . , ,
, VBA- , .
, 2.
,
, , . . , . (
) .

74

I. VBA


,
! ,
, . , , VBA-, .
- VBA-
, , . Office. -, Visio, , , VBA, .
! - , VBA !


, ,
. . ,
. .
Office drag-and-drop VBA- .

Office
Microsoft Word, Excel, PowerPoint, Access, Outlook VBA-
.
,
.
Word
Word VBA-, .
1. .
.
2. ,
,
.
. 1,
^.
3.
4.

(. 4.4).
.
, .
, ,
-. ( . 4.4 .)

5. ( VBA-, ).
6. , , . . , ,
. .

4. VBA-

75

i JXssJ
"* f * J * , BuSepfna
^5 " ** .
*1*1 \

TenelatePreleet.TrinjliteBynigvfl.MAlK
TemplatePrO|ett,T4nsl9teWtird9.MAIN

J3JJ | Normal

. </."/.

,
.
7. , .

VBA-. !
FrontPage Excel
FrontPage ! VBA-. VBA- . 1-4 . , ,
:
(. 4.5). .

f
1)"4, 9 "

Or

HVTPAI

<
j

J3J
. 4.5. Excel,

1. ,
, .
,
. , ,
, .
2. , , , . 4.6.

76

I. VBA

. 4 5, VBA-
Excel FrontPage
3. .
,
.
4. .
,
, .
" ".
5. .

Access
Access Office, VBA- . ,
F u n c t i o n , Sub, Office. 6
.
F u n c t i o n ,
, .
1. =>, ( , Office).
Word . 4.4.
2. , ,
.
3.
( , Office).
4. , , .
. 4.5.
5. , .
Access.

4. VBA-

77

6. - ( Function).
, ,
.
=__ ()
7. .
, ,
" ".

-
VBA-
. , Visio VBA-. ; CorelDraw ,
, Word, PowerPoint FrontPage.
1. Tools^Options (^).
2. , , Commands

().
3. Macros () .
.

,
..^
,
. , . .
1.
(. " Access", nri. 1-3).
2. .
.
2. , , .
. 4.7.
4. - ,
. 8, .
5. .
,
, . 4.8.
6. , , .

,
. , .
.
7. (. . 2).
S. .
78

I. VBA

&^

tff**

*+**

* fiwi

to [>

'

. 4.7,

1::..: i i ! !
!:"

'

!5

-2=J
!

'

Rpocwni

. 4.(?.
Office

, , .


Office , . ,
> <Alt>. X, , .

VBA-
, VBA- . VBA
VBA, , .
. , VBA .

4. VBA-

79

VBA !
Office C o r m a n d B a r B u t t o n s , CommandBarControls. CommandBar ( ,
CcmmandBarControls) ComrnandBars,
A p p l i c a t i o n Document.
Visio, , T o o l b a r l t e m . ,
T o o l b a r l t e m , T o o l b a r l t e m s , .
T o o l b a r l t e m s T o o l b a r s ,
T o o l b a r S e t , UlObj e c t ( ). , .
, : .
VBA- , , , .

Word, Excel Access


, . , ,
, ,
- .
- .
VBA- - Word, Excel Access , VBA-. , , .

Word
, VBA-
Word. , Word
D o u b l e C u r r e n t F o n t S i z e <Alt+Shift+>
(.. <Alt+Shift+KjiaBnuia >), .
1.

= ,

.
2. .
(. 4.9).
3. .
4. .
,
.
5. , .
, ,
, , .

80

I. VBA

,
Xi


1 ;
t

21
Cdtfawrt > ; .JNormal

. 4.9. VBA-
Word

6. , ,
.
.


, VBA- (.. ) .
Office . . ,
=>, ( ), . , .
.
, .
, .
, Visio . , Window^Show ShapeSheet. ShapeSheet Action. ( , Actions, Insert^Section Actions.)
EditoAction.
.
, , VBA- .

4. VB-

81

Excel
Excel VBA- , <Ctrl> (,
). , .
1. (
<Alt+F8>?)
2. .
3. .
4. (. 4.10) ,
<Ctrl>.
Excel . ( ,
, ^Shiff>. , <Shift>.)
5. , *

4.10. VBA Excel

Access
Access . Access . ,
, , . 1 10.
, Access ,
. ,
VBA-, Access. (, Access
VBA.) Access ,
.
1. ,
.
(. 4.11).
2. ,
. 4.11.
3. ,
.
' ,
;: \'~.

42

I. VBA

<Ctrl>. + <Shift>. , . ,

J
<Ctrb-k> , <Shift-'-F8> - - , ).
5.


VBA, .
F u n c t i o n .

6. , .
Access , A u t o K e y s .

are Q
|( Visual Basic.
' ^
-
. rpouesyp). tnpaew na.(*tt# F l .

//. 4. / /. Access

Visio
Visio VBA- , . .
1.
2.
3.
4.
5.

, .
F o r m a t ^ Behavior.
Double-Click.
VBA- Run Macro.
.


VBA-.

V/3*A-nftotfuuui
, VBA-, ,
, .
, , . ,
, , .

4. VBA-

83

, VBA- VBA , , .
. 4.1-4.3 ,
VBA- VBA-.
4 . 1 . VBA-

Microsoft Word
Microsoft Access

,

VBA VBA

/mVBA
/


. VBA,
Access

4.2. VBA- Word



VBA-

AutoExec

Word

AutoNew

AutoOpen

AutoClose

AutoExit

Word

. A u t o E x e c ( n o r m a l . d o t ) .
4.3. VBA-
( 14)

Visio

Document

DocumentOpened

Visio

Document

DocumentSavedA
s

Microsoft Word

Document

Open

Microsoft Word

Document

New

Microsoft Word

Document

Close

84

I.

. 4.3

Microsoft Excel

Application

WorkbookOpen

Microsoft Excel

Workbook

Open

Microsoft Excel

Workbook

NewSheet

. Visio, Word Excel .

Windows

VBA
(. . 4.1).
DOS, Windows,
.
Windows . ,
, ,
, .
, .
.
. , Word
VBA, :
C:\Microsoft Office\WINWORD.EXE

/mAutomaticNovel

, ,
.
Windows . , .

4. VBA-

85

Visual Basic
& & ...

Visual Basic

* ,

Visual Basic

"

, Visual Basic

- YBA-

"

/ . ? ^ . , ,
- , .
Visual Basic , .
, . - , ,
. ,
, Visual Basic, .
, ( ), Visual Basic
( , ) npoi VBA. Visual Basic
, .
, Project Explorer ( ). Object Browser ( ) Code ( )- . , . , UserForm ( ).
Properties ( ), , 9
10. R \ .

/ fteqeuanofta
Visual Basic
\ isual Basic Microsoft , ] Microsoft Office. , VBA Office.

86

I. VBA

, -Microsoft
Visual Basic. , VBA R
Microsoft, - ,
Office.


, , . 1 , , , Visual Basic .
Visual Basic . , - , , , View (). . , .

T o o l s ^ Project

Properties
Visual Basic

View^Toolbarso
^

Rurr=>Design Mode

,
, -
File

Design Mode, , , Run?



. ,
,

,


Word, Excel PowerPoint, , Visual Basic , .
.
Visual Basic .
. S
;;

Standard (). , .) ,
Visual Basic. : , , .

. S Edit ().
'
. Edit.
:

I S
*

Debug (). -.
.

: S UserForm ( ). ;:\ . Format


, .)'.: \ .

5. Visual Basic

87

.
( ) ( ). .
1.

.
(. 5.1) . , .

Debug
. 5.1,


2. ,
.
, .
Visual Basic , .


.
Visual Basic

. 5.2 Visual Basic . ,


Visual Basic.
,
. , ( ) .
. .
: ,
Visual Basic.
, , . , ( , ).


, - , Visual Basic. , !
, , " " ( , Office).

88

I. VBA

Microsoft Viauol Basic

:\ iLicyMBHTu\VisiQVBAvsd [ h l ' t a k j

Eife EOff liie* insert Format

ELiwate Sub UseirFcrm_Activate ()


LisfBoxl .Addlteir. " &
Li tBoxl .AddItem " -'
Lx?"Eoxl.Addltem " ^LiatBoxl .Addltesn " ;'
Li rBoxl .Addltem "; s 'iinf

| Fiivar.- ;;.,!. changed

/. 5 2 Visual Basic
,


, , , <AIt>,
. <Alt> .
It / .
I
, I
<Alt> <Ctrl>.
I S ,
I
- ( , I
). , I
.
| / , , .
I
X, ,
I
.


.
.
,
.

5, Visual Basic

89

1.
.
(. 5.3).

. 5.3.

Visual Basic

2. , , ,
.
. ,
.
3. , , , .
.
4. ,
.
,
<Alt>. , , . <Ctrl>.


Visual Basic,
(. . 1 ). .
. , .
, .
,
, (. 5.3 5 4>'.

90

I. VBA


4n
Isert Fte.
%
r E
<
W
R 4s..
Rtfnoye <]11>,,.
JprM,,.
_

Sebcrcd commeid:

Window

. 5.4. Visual Basic


, , Visual Basic (..
) . . ,
.')> .
, (. . 5.4).
. .


. 5.1 , Visual Basic.
, Windows
. , <SiiifHF10>
, ,
.

5. Visual Basic

91

5 . 1 . , Visual Basic

,..

...

<F7>

<Shift+F7>

<Ctr!+Tab>

<F2>

< F4>

<Ctrl+G>

<Ctrl+L>


...

...

<Shift+F2>

<Ctrl+F>

(, , ) <F3>

<Shift+F3>

<Ctrl+H>

<Ctrl+Shift+F2>

<Ctrl+Z>

<Ctrl+J>

<Ctrl+Shift+J>

<Ctrl+l>

<Ctrl+Shift+l>

<!+>


...

...

<11+5+
>


...

...

<F5>

<Ctri+Break>

<Ctrl+F8>

12

I. VBA

/
,
Visual Basic.
, , , . ,
-
VBA-.

, -
Visual Basic :
, ,
.
, , ,
, .
, VBA-. ,
.
( ) , . .
, , , Visual Basic,
.


Visual Basic , . , , , . . 5.1. , View.
, , . , . ,
X,
.
, , , Window. .
Windows,
<Ctrl+Tab> <Ctrl+F6>. Visual Basic .
, , : " ?"
.

5. Visual Basic

93


, Visual Basic , ..
, . , . . 5.5 Visual Basic, .

Ste Edit View insert Format Qehug R1-"1 1!

ffijn*

- i * 4a p

VisioVBA <C:> -

EQVB* BuwninriTeiffioi E'aiTples UserFQrnJ^

,., j |
3ackCotar
BordecColL

<\

s

UOQF
E

e
UserFocml

E-iat
-r t
- m

fie-

320
True
Tararno
!

. 5.5. Visual Basic, ,


, ,
. , , Visual Basic , ,
.

!
, ,
. . .

(, ), ToolsOOptions, Docking. ,
* . 5.6, - , , - .
(
), ,
Dockable . ,
par-oiaei.
94

I.

VBA

. 56! 5 Visual Basic


, .

^ , ,

, , ( f
Microsoft , ).
].
Windows
t\
, ( , ) .
S

, , . , .

I S ,
1
, .
^
\

, , .

it

/
<Ctrl+Tab>. ; Window.


Visual Basic \ .
, Visual Basic , , .

5. Visual Basic

95

toftoetoncutu
VBA ,
, . Visual
Basic , , , ,

Project Explorer ( ).
, ( , , ),
Save () Standard () Visual Basic.
,
, VBA-.


, Visual Basic, , , , .
1 S <Ctrl+R>.
I S Project Explorer Standard.
/ View^Project Explorer.


- Windows
(, , ), \ . (. 5.7).
, .
, . ,
Project Properties ( )
( " ").
, , , ( , , 14). .
.


,
, .
, , .

96

I. VBA

' & Flowchart


Basic ( \ i\*
- -.1 Viso Ob]ec&
{) ThisOocu merit fishbone Diagram)
L Forms
" Modules
. <S ViskiVBA (:\ \>.
- f-< Visto Objects
1
ThisDocument (VIsnVBA)
& ButtorWlTe>!eojb!ampleE
3 CltkCOLTilngFarm
EB DateCaptJonedForm

3 MoieyP lamer
MovngFwm
9 QuotatonForm
9 3ioy.SelectianFor

. Modules
AboutAge
4L AnQutbflkAutomatnnExample
JlEj

. 5.7.
Visio
Windows, Visual Basic
( ).
, , , , "". ,
"".
.
1 /
I
%
I S
|
I
S
it
|
| V
I

,
, "",
() , .
,
, "",
, .
, , <Enter>.
, <Shift+Enter>.

, <Enter>, <Shift+Enter>, . .
\ S View C o d e ( ), ,
f
.
I
I S View Object ( ), ,
X
. ,
|
. , I
Visual Basic , I
.

5. Visual Basic

97

I / Toggle Folders ( ), ,
|
.
f
, |
. ,
|
, |
. , .


(. 5.8) .
, , .

. 5.8.


Project Properties ( ) , , , . , VBA-, .
, , , .
, T O O I S ^ H M H Properties Properties
. . 5.9 Project Properties
General ().

J - , N053-131033 1999 200

Hfifci Fda :
^ Fiie hJisio^1\^51 4LP

L. .|

Condtons! COmpilaBon Arguments:

[ -

' )

. 5.9. General
Project Properties
98

I. VBA


, , .
(, .)
VBA
Properties ( ). - , .
. General
Project Properties, - .


, -
- , .
, Project
Properties Protection (. 5.10).

Easswod
i

[**"

. 5.10. Protection Project Properties


, .
Lock project for viewing ( ). , , . ,
Project Properties, ,
.
Password (). ,
.
Confirm Password ( ). , .
, ,
. VBA ,
, .

5. Visual Basic

99


Object Browser ( ) , . , , VBA.
"", ,

, , ,
. , , , , VBA, , (
14).
, , , , .


<F2>.
Object Browser
Standard View^Object Browser . . 5.11.

Object Biowiei

-
-i

goBa-l
j
.-p ApphfatO
i JP
i erss
iter'i
2! Automato
in Ever'.
tS DLLEve-li
IS
p
i EvenT
lypes

Lastr-equesKate

Property terns As

* ! <
Retjms ihe Ions t

. 5.1 /.

100

I. VBA


, , .
.
Project/Library ( /)
, . ( , , .) <11
L i b r a r i e s > ( ), ,
.. , .
, .


(. . 5.11). , .
, . ; , " ".
. Classes ( ), , , , , .
Member Of ( ) . , , , .
, ? , .
Details () ,
( - ,
). , , .


Web-.
Web-, Details (
) .


, , , <Enter> Code
.
Show Definition ( ).

5. Visual Basic

101


, <F1> Help ()
, , ,
, .
. ,
iyr VBA.


, ,
.
, . .
1. / <11 L i b r a r i e s >
( ) .
, ,
, , , .
2. Search () , .

, Search.
3. <Enter> Search (), .
,
Search Results ( ) . , .
4. Search Results, Search.
(
, ), Search Results .



, ,
, .
-, ,
<Ctrl+C> Copy to Clipboard ( ), .
. , , - ,
( , ).

102

I. VBA

&&/&
Code ( ) Visual Basic: VBA-, .
VBA-, ,
. VBA
, , ,
. VESA , ,
.


,
, Visual Basic .
. .
I / <F7>.
I J View Code .
I J View Code
1
.
\ View^Code.
I S ( !) I
<Enter>.
, <F7>
View^Code .



. ( 6.)
( , ,
).


VBA , , , ,
VBA-.
VBA- , , , Windows ( <> , a <Ctrl+Home> ).
, <Shift> .
, .
, , , .

5. Visual Basic

103

I" S , .
\ / , , \
, <Ctrl>.
,
, Immediate ( ) Watches (
).
, , , ,
, . ( , "
".)

. , <Ctrl+Z> Undo
( ) Edit (), . Edit Redo ( ) Undo, Redo Visual Basic
.


, Visual Basic ( ) .
I /
|
1
i
1S
|
|
1

IS
|
I
1
|
S
.
|
f
|
I
I

, ( ,
ToolS^Options , Auto Indent ( )).
Visual Basic ,
VBA (, i f . . . t h e n . . . e l s e , I f . . . T h e n . . . E l s e ) .
, (
), .
, ,
Sub ( F u n c t i o n ) ,
, Visual Basic End Sub ( End F u n c t i o n ) . , -.
, , VBA-,
- , Visual Basic (. 5.12).
, ,
. , Visual Basic ,
, - .
& -J

. 5.12.
, Visual Basic
,

104

I. VBA


,
. ,
(. 5.13). ,
. .
| S
I
|
I
|
I
I

. ( G e n e r a l ) (),
. ,
. ( ). , .

| S
1
I
I
1
i

/.
( D e c l a r a t i o n s ) () , , .
, , , . , .

(Guner.l)

Dir.
Dim
Dare
Dire

Si?] | Ad dMy Button

objMyUI As v i s i o . U I O b j e c t
objMyTooJ.becSet As V i s i o . T o o i b a r s e t
objmyToolbatltemH As VisiO.Toolbarltetns
o

- AU(,yMBinu\Vi8ioVUA.vsd- StyHnM... H E I E 3

| (General)
Sub DisplayshowSelectionFosrm
~
Di^ ItemCount I n t e g e r , Message A:T
ob]Myl
Sen objir,'

ob j My New'
objMyNeW
o b jMytJew'

Message = ShowSelectionFotrm. iblCouni"


Items = Accive&Jindow.Selection .CouniMessage = c s t i ( I t e m s ) s " "
|show5electionFOEm.lblCountOfIteras.Ct
ShovJSelectionForm.Shovj
i
Manyconttola.Show
\
End Sub
;
Sub FoctfiVaciablecemoO

. 5.13.
.


, 3 . , ,
, . , ,
.

5. Visual Basic

105

, , , .
, , ,
(, ; . 5.13), Toggle Bookmark ( ).
Edit (), , , . (Edit^Bookmarks^
Toggle Bookmark), . ( , Microsoft
.) ,
.
, . Next
Bookmark ( ) Previous Bookmark ( ) , .
, Toggle Bookmark. ,
, Clear All
Bookmarks ( ).


(. 5.14). .
.
:

, C:\Mo nemyMaHiuWiaioVBA.vstl- AddAVisioBution (Code)


(General]
TJ AddMyuution

l-lnlxl

Sub AddMyButton!)
IJi:n
L'im
Dim
Dim
' b v f "/-

objMyUI AS V i s i o J U O b j e c t
objMyTooibarSet As V i a i o . T o o l b a r s e t
objmyTooibarltetns v i s i o . T o o l b a r l t e r i s
objMyfJewTooIbarltem As VISJ.0 .ToolbacTtera
: h ' '

f - - I ' i

:'

:,

;\

'. "

P r i v a t e Sub txtEerialHumber Keypress(EyVal KeyAscii


As MSForms.Returnlnteger)

If Ctir (KeyAsciiJ < "0" O-z


(Chr(KeyAscii) > "9" An; Chr(KeyAscii) < "A") Or
(Chr (KeyAscii) > "2" Arid Chr (KeyAscii) "a") Or
Chr ( K e y A s c i i ) > " z " Then
MsgBox " I n v a l i d c h a r a c t e r ! "

. 5.14.
, . , .
, .

106

!.


, - ? Visual Basic
VBA . , .

Visual Basic, Edit ().
I s List Properties and Methods ( ).
| S L'St Constants ( ).
\. / Complete Word ( ).


, , List Properties and Methods
( ) , , . . - VBA-,
. ,
, , ( ), . :
Activewindow.Selection.Group
( 12.)
,
. ,
(. 5.15).
, (
) - ,
, <>, .
nnKyMHHTHWwioVBA.vsd-Moduli (Codt>)
-| [ v j
{{General)
S'-ib
Dim Alleges As
Set Ailegra = t.'ev
Allegra.
IB SpeciaEffect
s? SianUpPosilion
End Scb eff Tag
UndoAction
VertiCalScrolfBarSide

. 5.15. List Properties and Methods



5. Visual Basic

107

,
. ,
Dim Oatmeal As C e r e a l O b j e c t
, As,
. , ,
, <>. ,
7.

VBA, ,
Variant. , ,
. ( 7.)
, .
, Options
(). , Tools^Options , Auto List Members (
). Edit,
<Ctrl+J>.


List Constants ( ) ,
, , , , , .
, =, ,
O a t m e a l . T e x t u r e = Gluey
, ,
List Properties and Methods ( ).
, ,
<Ctrl+Shift+J>. , , " " 7.


Complete Word ( ) Visual Basic VBA-.
<1+>. , . ,
, , , , , ,
. , I n t e g e r V a r i a n t .
, <1+>, - , ,
, . ,
,
, Visual Basic ,
<!+>, .

108

I. VBA


VBA- ,
. ( ) , . , .
Visual Basic
, , .
-
, , , , . Quick Info ( ), . 5.16.

J OaatilBim

newDiirection(2) = 1#
vportObj.Direction = newDicection
ThisDcaving.ActiveViewport vpoctobj
vportobj .zooieAll

boolX = clieckR'jles (
checkRules(wh4f Double, hetgN As CouOie, webTh As Double) As
End sub boolean

iffyi^r
. 5.16. Quick Info
ToolsoOptions Auto Quick Info ( ), Quick info ,
, , . ,
Quick Info <Ctrl+I>.
, . , , Quick Info .
, , .

Quick Info Parameter Info


Quick Info Parameter Info ( ).
, :
MsgBox(Str(AnIntegerVariable))
S t r , A n l n t e g e r V a r i a b l e ,
. Ms gBox ( , VBA ).
- Quick Info , Quick Info ,
. , Parameter Info,
, .. , .
Parameter Info <Ctri+Shift+l>.

5. Visual Basic

109


Properties ( ) . ,
(, , ), Visual Basic . . 57, ,
.
(, , ,
).

j Defn
i e Beam User-For

Dsfre6eam
&HBQO0D00FB
&HB0O00012S
0m
f BorderSytWJane
Dereneam
>aBuf'er
=naheld
c

eCo lot
Height

( \

0 - fmCyclsAlf ns
32000

Taboma
8000012
177

jScroiKarsVrelBlB 3 - frnScrollBirsBoih
.eft
0
louselcon
(Wona)
tousePairiEr
- fmMoussPoiE'Oe
icture
idureAI-gnmenl

- A\ 1 _J

Quit

(None)
2 fiTPictUTBAl^nnian
0-f

/. 5.17.
,
. ,
.
, 10. , .


, .
I S <F4>.
| S P r o p e r t i e s () S t a n d a r d
\
().
\ S View^Properties .
,
( ), .

110

. -

I. VBA

, .
1. .
2. .
, ,
" ". , , <F4>.
3- .
, (Name) .

ftyatftieoe
orfUaqieu
9, , .
Visual Basic .
I/
|
|
|

Immediate { ) VBA- " " (. 5.18). , .


p r i n t 10C1/2
500,5
2 .ID.99 2:12:33
=#1/1/2000#
x=iif(x < ," "," ")
pcint

. 5.18. VBA-
|V
I
IV
|
|
f
|S
|

Locals ( ) .
Watches ( )
. ,
.
Call Stack ( ) , , .

. (break mode) . , - .

5. Visual Basic

111


, . , View () . .

Immediate

Debug

<Ctrl+G>

Locals

Debug

Watches

Debug

Call Stack

Debug
Locals

<Ctrl+L>

112

I. VBA

II

YBA

' ,
?"

& &...

3
-,
-
, ^ ;

- '~ ^'<
. , , i
, .
-
, i- '

, ,
VBA,
, ]
- .- .- ' '
'
7
i
VBA , ,

.
"
;
8 *
!
, ,
,
. , "".
,
, 9 ,
,
- .
II , .
Visual Basic

. VBA
,
VBA-

VBA-.

\- ,
VBA.
10
.

VBA-
/3 & ...
> VBA-
^

>
>

VBA

> VBA, , ,

>

>
>
>

, VBA-, .
, .
VBA, .
, , . , , . , ( ,
, ).

*> //
VBA- . , , ,
, .


? , , ( )
. , , .

6. VBA-

115

VBA. VBA
, , . VBA-
, VBA
, ,
,
.
,
. VBA- 14.


VBA- , . ( , ,
). .
, , .
(.) , 1 1000. , , 500, .
Option Explicit
Const Maximum As Integer = 500
Const HowMany As Integer = 5
Dim ListOfNumbers() As Integer
Sub MAIN{)
Dim ItemsInList, n, x As Integer
ReDim ListOfNumbers(HowMany)
Randomize
For x = 0 To HowMany
ListOfNumbers(x) = I n t ( ( 1 0 0 0 * Rnd) + 1)
Next x
n = CountBigNumbers()
MsgBox ("There were " & n & " values g r e a t e r than " & _
Maximum)
End Sub
Function CountBigNumbers(}
Dim Counter As Integer, As Integer
Counter = 0
For = 0 To HowMany
If ListOfNumbers(y) > Maximum Then
Counter = Counter + 1
End If

116

II. VBA

Next
CountBigNumbers = C o u n t e r
End F u n c t i o n
. , . , O p t i o n E x p l i c i t , Sub, Sub
MAIN ;. F u n c t i o n , F u n c t i o n C o u n t B i g Numbers ( ) . ,
, MsgBox, .

VBA
, ,
VBA.
| S
|
|
|
'%
\ ^
I
|
{
:

:
1
I

, VBA-. ,
VBA - . " ", .
,
. , . VBA Sub F u n c t i o n .
, , (End Sub
End F u n c t i o n ) .

\ ^ , !
, , .
\
VBA ,

, .
VBA . , ,
. , . 14.
II

I S , ,
'\
, .

Otftta/qa Sefiiftnca ^, ?
. , , , , . VBA-
. , , ,
, , Visual Basic
. ( 5.)

6. VBA-

117


VBA . , ,
, .


, . , . , .
, .
, .
. , ,
. ,
, , . . ( . , .)
, - .
, VBA-. , .
, ,
,
, . . " " .

VBA-
Visual Basic, ,
. ,
, ( , , , ). .
\ / Insert Module ( ) (
-1
). ,
;
. , ,
, ,
i
.
:


, ,
Insert Module ( ) .
I n s e r t s Module .

Visual Basic . .
, ( 5).

118

II. VBA


D e c l a r a t i o n s (). , ,
.
.
| / , . I
( ). |
, , |
.
I S , |
VBA.
. , ,
, - . ( , , ,
" ".)
, , - .

.
, ,
. ,
.


VBA- .
VBA Visual Basic .
,
, , . , , .
, , , . , , .
, VBA. , ,
.

VBA- ,
, - .

6. VBA-

119

VB - S u b ()
F u n c t i o n (). ,
" ".

- \
Sub F u n c t i o n . , VBA, -
, , Sub. VBA
- P r o p e r t y { ).
;
.
s Sub () -
VBA. Sub , 4. , Sub
(.. ) .
S F u n c t i o n () VBA-.
Sub , , , F u n c t i o n .
s (event procedure) - Sub . , . ,
14.
s P r o p e r t y { ) . 14,
, ;
Add Procedure ( ).
, VBA- Sub, (.. ) . \
Sub, :
Visual Basic, VBA-. - ;
Sub , . '.

" ".


, (Sub F u n c t i o n ) :
P u b l i c Sub ()
MsgBox " ! "
End Sub
P u b l i c F u n c t i o n ( As Date)
= D a t e D i f f ( " y y y y " , , Date)
End F u n c t i o n
, ,
E n d .
" S u b " "
Function".

120

. VBA


,
, . , " VBA", ,
.
, . , .. End. .
| S
| S

InsertOProcedure .
.

?
,
, . , " "
, .
, ,
<Enter>. End S u b E n d F u n c t i o n Visual Basic .
, , VBA
. , Insert Procedure
( ). ( , ,
Insert Procedure .) Insert^Procedure .
Add Procedure ( ). Name (), ()
Scope ( ) , ,
, All Local variables as Statics ( ).
, ,
, " ".
Visual Basic
E n d , , , .


, ,
.
VBA, . , .
,
, , VBA .

Sub
" S u b " ,
. , , , . VBA, -

6. VBA-

121

Sub , Sub. .
. Sub , :
Public Sub ()
Dim As String
= " !"
MsgBox
( )
;nd Sub
, Sub, .
-, , , VBA , . -, , .
Sub End Sub, VBA , .

Sub
P u b l i c . P u b l i c , P r i v a t e .
P u b l i c , . , P u b l i c , . , " ".
Sub, ,
Sub. , , , .


. , ?
, .
, . , ,
.
" "'.
, VBA
Sub, . Sub,
, .

Sub
, , , . Sub,
, ,
. , ,
Sub:

122

II. VBA

= ""

= +1

Function
F u n c t i o n , , , Sub,
.
F u n c t i o n , ,
.
F u n c t i o n :
P u b l i c F u n c t i o n ( As I n t e g e r , y As I n t e g e r )
DLT! Z AS I n t e g e r
2 = x +
= x 2
End Function 1
, F u n c t i o n
Sub. , ( P u b l i c ) .
F u n c t i o n , , , ,
. " ",
, . Sub, F u n c t i o n End F u n c t i o n .
, , VBA
.
F u n c t i o n , VBA
. F u n c t i o n .
F u n c t i o n , !
, VBA, 11.

Function Sub
F u n c t i o n Sub :
F u n c t i o n -
, . ,
. = z. , ,
.

Function
F u n c t i o n , .
, . , :

6. VBA-

123

. = (3, 4)
ZCTeneHb ,
, F u n c t i o n
3 . , , .


, . , , ,
. Sub, F u n c t i o n .
:
P u b l i c F u n c t i o n ( I n t e g e r , As I n t e g e r )
Dim z As I n t e g e r
z = x +
= x z
End F u n c t i o n
. ,
.
,
( , " ").
,
. , , .
. z , . , , z. , , z .
, z .


, ? ,
, . .
. , :
P u b l i c Sub ()
= ["Rambler _
Classic",1962)
End Sub
Public Function ( As String,
As Integer)
Dim As Integer
Di.TL As Single

. VBA

= - 1900
If = "Rambler C l a s s i c " Then
= .001
Else If = "Dodge D a r t " Then
= .005
Else If . . .
{ )
End I f
= * _
* 1000
End F u n c t i o n
Funct i o n
. F u n c t i o n , , .
, . ,
, ,
.
Dim As String
Dim As Integer
Public Sub 2()
= "Dodge Dart"
= 1963
= 2()
End Sub
Public function 2()
Dim As Integer
Dim As Single
= - 1900
If = "Rambler Classic" Then
= .001
Else If = "Dodge Dart" Then
= .005
Else If ...
( )
End If
= * _
* 1000
End Function

?
, ,
.
| S ,
I:
.
/ [
\, ( ).
|
, |
.
I
/ VBA

. , , I
.

6. VBA-

125


,
. :
( As S t r i n g , I n t e g e r , _
As Date)
,
As , VBA
. , VBA V a r i a n t . ( , ,
, 7 12 .)


- ,
.
, .

. .
F u n c t i o n
.
Sub, , , F u n c t i o n , ,


VBA-, , . ,
? ,
.
, -
. , , , . ,
, , .
. ,
,
, . .
, ,
, (,
,
). , .

126

II. VBA

OJso/t o&mctnu
VBA- .
, , . , ,
"" .
.
f S VBA ( ) |
(public). ,
I
,
(, , 14).

, (private). ( ) . , ,
, .
"/ VBA-, , ,
, .

!
( " " 7).


, P u b l i c P r i v a t e . :
P u b l i c Sub IKr.eadYouO
. . . ( )
End Sub
P r i v a t e F u n c t i o n IKneedYou()
.. . ( )
End F u n c t i o n
, P u b l i c . , , , .

, O p t i o n P r i v a t e Module.
, "" " ".


.
, , ( , ).
, ? , VBA
, !

6. VBA-

127

. , . ,
VBA , .
.

//

. , ,
, , ,
. VBA : , , .

, VBA (, , ). , , .
, .
. ,
( 7).
, , , :
Dim As I n t e g e r
7 , ,
. ()
, :
C o n s t a n t = ""
, . ,
.
.

, ,
D a t e . :

As String
As Date
End Type
Sub ,
, .
End Sub . , , , :

128

//. VBA

Private Sub ()
( )
End Sub
"
".



. : , ,
, .
, .
12:
= + 12
, C o l o r () A G r a p h i c S h a p e Blu'3 () , B l u e , :
AGraph^oShape.Color = Blue
, ,
Sqr VBA :
= Sqr()
, , VBA ,
, . , , , ,
F u n c t i o n , (, + *). .

3,14

3,14

AGraphicShape

5 ( , A G r a p h i c S h a p e
)

{12

3qr(x))/5

" , " & "


"

2 ( , = 4 )


.
; S ,
I S .

6. VBA-

129

!' V , ,

( |
) .
S VBA .
. , R o t a t e A G r a p h i c S h a p e :
AGraphicShape.Rotate{90)

. I f . . . T h e n ,
, 3, , , , :
If > 3 Then

End I f



VBA. , , .

Option Base

-
0 1 ( 13)

Option Compare

, VBA
(). ""
Binary
, T e x t , ,
D a t a b a s e ( Access) -
,

Option Private Module


D e c l a r a t i o n s
,
,
( . "
")

Option Explicit

,
,
.
VBA
( 7)

130

//. VBA


, , . VBA-. , ,
, , , .
\ / , .
\
, , , ,
t S

\ V ,
VBA :
!

&

$ #

()

{ } ( ) [ ] = -

% / - < > : ;

.
255 (40 ).
, VBA.

. ,
. ,
( D e c l a r a t i o n s ) .
,
.
, , Visual Basic , . , (. 6.1). :
. .

iro A*
Mm As Str
im

Microsoft Visuul uu

mpile eiraf.

. 6.1. Visual Basic ,


.
VBA , . , ,
, :
Dim LOUCVARIABLE As

String

6. VBA-

131

255...

Go4It

45coreAr.d7

You_Did_It

WhoDunlt?

,
, . , :
loudvariabie = "-"
lOuDvArlaBlE =Ucase(10UDvARIA3LE)
MsgBox(LoudVariable)
, .
Visual Basic , , - , ,
.

VBA
, , . , .
,
. ,
- .
, .
, , , , ,
, . ,
, , i n t P a r t N o .
. 6.1 , VBA.
. .
1-5 ,
. ,
.
6 . 1 . , VBA

byt

Byte

bytDaysInMonth

bool

Boolean

booIClearedSta-cus

int

Integer

intWeeksOnChart

132

II. VBA

. 8.1

Ing

Long integer

lngPopulation

sng

Single

sngRadius

dbl

Double

dblParsecs

Currency

curUnitPrice

str

String

strLastNarae

date

DateAime

dateBirthdate

var

Variant

varSerialNumber

obj

Object

obj StampCo11ection

txt

Text box ( )

txtEnterName

Ibl

Label ()

iblAnswerMessage

cmd

Command button ()

cmdCalculatelnterestRate

mnu

Menu ()

mnuTools

cmb

Combo box ( )

cmbioyCacegory

fra

Frame ()

fraHabitat

opt

Option button ()

optGasolineGrade

chk

Check box ()

chkCaseSensiuive

bas

Module ()

basTextFormatFunctions

frm

UserForm ( )

frmOptionsDialog

> tooa
-, , .
, , , .


: . VBA , . , :
If i n t A = 27 Then
If t x t C h o o s e C o l o r . T e x t = "" Then

6. VBA-

133

intA = 33
intB = 0
End If
For Each objCbar In CommandBars
If objCbar.Name = " " Then
If objCbar.Visible = False Then
objCbar.Visible = True
iind If
End If
'lext objCbar
End If
If intA = 2 7 Then
If txtChooseColor.Text = "" Then
intA = 33
intB = 0
End If
For Each objCbar In CommandBars
If objCbar.Name = " " Then
If objCbar.Visible = False Then
objCbar.Visible = True
End If
End If
Next objCbar
End If
, E n d I f ,
I f . . . T h e n .
.


? , . , , - , .
, VBA I f . . . T h e n . . . E l s e ,
Do . . . L o o p F o r N e x t ,
. :

Do While intC <> 20


intA = intA + 1
If intA = intB Then
intA - 5
intB - 10
Else
intA = intB
intC = 20
End If
Loop
VBA i n t A = i n t A + 1 I f . . . T h e n . .. E l s e
D o . . . Loop. i n t A = i n t A + 1, , I f . . . T h e n . . . E l s e

134

II. VBA

( I f . . . T h e n , E l s e End I f ) . ,
If . . . T h e n , , i n t A = i n t E ,
, , E l s e .
, Do . . . L o o p I f . . . T h e n . . . E l s e
( ,
). Do. . . L o o p L o o p ,
I f . . . Then . . . E l s e End I f . ,
. , . ( 8.)



<>, . , <> , T o o l s ^ O p t i o n s . Options
() Editor () Tab Width ( ), , .
Visual Basic , , 3
, 1 32.
- ,
VBA
Indent ( ) Outdent ( ) . , ,
. ,
.


, Visual Basic , .
,
( '-<>). , Editor Tools^Options.

-

.
, (. 6.2).

!
300
(, , , 308),
.
, , , -.

6. VBA-

135


,
( ). , :

sngWackyNumber = Cos(12 * 57.5 / Sqr(intMyTinyNumber + _


intMyBigNuraber) +- CustomDataMassage (sngRawInfo, 12) +
(bytFirstTuesdaylnAugust * curLastPayCheck] + 1)
He ,
, ,
VBA " " (invalid character).
: ,
, (, ,
). , , ,
7.


, , . VBA , .
, , , . ,
, ,
. , :

sngTempl = Sqr(intMyTinyNumber + intMyBigNumber)


sngTemp2 = 12 * 57.5 / sngTempl
sngTemp3 = CustomDataMassage(sngRawInfo, 12)
sngTeinp4 = bytFirstTuesdaylnAugust * curLastPayCheck
sngWackyNumber = Cos(sngTemp2 + sngTemp3 + sngTemp4 + 1)
, . ,
,
, , .
( 9.)


, VBA ,
, . ,
, ,
.

136

It. VBA

VBA .
, , .
,
. , , .


. ,
, .
, . , , . 6.2.

B i t s - (Code)
^ji'JDIsplayGueetlon
Public sub Display^uestion()
Sel e c t N e x t Q u e s t i o n
vc

: ; . " I V , T : - >

' ^ - n ; M I ",;,.;' - " ^ r-,;,.-.


'

':'!''<':.-:

"~

r . - y i : . .!"-]

"or

'!.:

-:

!-.;-,;.*::. r-~ : . " j r ; > - . : i


'

.-:-'.'"[

If b o o l M e i g h t i n g F l a g = MTurnedOn Tiien ' ;:...;.


ReEntstQuestionLoop:
Do K h i l e Q u e s t i o n s (intQUumberCucreni:) .intQKeighv v i;;tRando.TiKeight
3elsctTJextQM=stlo!i ' ":= ;[ : "J pi; ::.: .- -:''. :: r.r.r.v. '. VECF.-;-.
Loop
End I f

If irnQN'jmberiurrefit =

AntOKumbecLasi: Then

GOTO ReEnterQuestionl-OOp
Find If
: " -."it ;
cLast = incCNumberCuirent '

flf/c. ff.2 ?
, , , (
). , . , :

= b _ '
f-
, ,
, , , :
1


z = - _

' -

6. VBA-

137


.

, , . , , , . , .
, :
. . - , ,
, , .
, , , .


. ,
, , , ; .


,
, . , VBA , .
, , ,
Visual Basic , .
<Enter> . ,
, , C o m m e n t
Block ( ). Visual Basic
.
"" ,
. U n c o m m e n t
Block ( }.
Comment Block Uncomment Block
. , , ,
.

138

II. VBA

. , Comment Block ..
, Uncomment Block . ,

, , ,
.

Rem
Rem,
BASIC, VBA , , . (Rem remark (), .) , , Rem .
, - . Rem , -
, Rem
(:):
Rem Rem
= b + : Rem Rem?

6. VBA-

139


3 & ...

>
>

> VBA
>

* VBA,
* . ,
. V - . , .
( ) ( 1 ).

. 12 , ,
13 .
, ,
"" . Internet www. d i a l e k t i k a . com.


,
.
. . -
, , . .
.
- .

140

II. VBA

, ? , .
, .
, ' , ,
. , , ,
.
,
, , , ,
. , ,
. .
, (.. ) . , , , ", ".
, , .


6, VBA-,
.
. :
Dim v a r A n y O l d V a r i a b l e
P r i v a t e i n t l n t e g e r V a r i a b l e As I n t e g e r
S t a t i c ^trNewYearsResolution As S t r i n g
Dim,
Dimension (), , Dim VBA , .
P r i v a t e , P u b l i c
S t a t i c : , (
, " ", 6).
. ,
( , " "). ( ) .


:
? S ( ),
; S

( ).

, , ..
.
, . ,

7.

141

.
P r i v a t e , P u b l i c S t a t i c . { 6 , " ",)
VBA , , , . :
P u b l i c Sub V a r i a b l e P r o c e d u r e s ( )
Dim s t r C h i l d s N a m e As S t r i n g
Dim intToyCount As I n t e g e r
Dim curAllcwance As C u r r e n c y
strChildsName = i n p u t B o x ( " : " )
CountToys 11 NewAllowance
1
F u n c t i o n ,
' -
intToyCount = C o u n t T o y s ( s t r C h i l d s N a m e )
curAllowance = NewAllowance(strChildsName, i n t T o y C o u n t )
End Sub
1


, , , ,
. .
, O p t i o n E x p l i c i t ,
, " ".
, VBA . - = 7, VBA 7. ,
, V a r i a n t ,
.


. VBA v a r i a b l e , , :
Dim v a r i a b l e
.
Dim sngMyOidSocks As S i n g l e
sngMyCidSocks S i n g l e , , , - , , , , , 6,02 X 1(\
VBA , D a t e () C u r r e n c y (). , -

142

//. VBA

. . 7.1 ,
, , " ".
7 . 1 . VBA

Boolean

(-1) (0)

Byte

0 255

Integer

He

-32768 32767

Long

-2147483648 2147483647

Si n g l e

-3.40282338 -1.401298-45
1,401298-45
3,40282338

Double

-1,79769313486232308
- 4,94065645841247-324
4,94065645841247-324
1,79769313486232308

Currency

,
19 ,

-922337203685477,5808
922337203685477,5807

Decimal

, 29
, 28


, ,
79228162514264337593543950335

7,922816251426433759354 28

Date

1 100 . 31 9999 .

Obj ec~

VBA

String
( )

String
(
)


0
65400

Variant

,
,

7.

143

Variant:

, (..
, V a r i a n t ) , , .
,
. , ,
, .
, .
, VBA
. ,
Dirr. d a t e A n n i v e r s a r y As Date
VBA , , :
dateAnniversary = " " '
, , , ,
.
, d a t e A n n i v e r s a r y
V a r i a n t , .. , . V a r i a n t , , :
Dim v a r T o o l S u i t e As V a r i a n t
V a r i a n t ,
, :
Dim T o o l S u i t e
, , , , . V a r i a n t
, , .
.
V a r i a n t , ,
, .
, ,
, V a r i a n t .

V a r i a n t
.

, ,
. ,
, VBA .
Option E x p l i c i t

144

II. VBA

VBA . .
VBA : - .
. O p t i o n
E x p l i c i t . - . VBA , ,
, .
, ,
- , 12 .
, , O p t i o n E x p l i c i t
. ,
? . Tools11*Options
Options () Editor () Require
Variable Declaration ( ).
Visual Basic O p t i o n E x p l i c i t
. , ,
.


(.. , ) :
1 S ( ,
I
; . " ");
I ^ , (Dim, P u b l i c , P r i I
v a t e S t a t i c ) .
Dim,
.
VBA . , Dim
, , .


P r i v a t e , Dim. ,
:
Privar.2 s t r L o u i e As S t r i n g
Dim s t r L o u i e As S t r i n g
, P r i v a t e Dim , , , P r i v a t e . P r i v a t e ,
, ,
.

7.

145


P u b l i c
. :
Public intUnclassifield As Integer
, P u b l i c , D e c l a r a t i o n s () . VBA
P u b l i c , , .


S t a t i c ,
, , . i n t L a s t i n g V a r i a b l e :
Sub T r a n s i e n t P r o c e d u r e ( )
Dim s t r T r a n s i e n t V a r i a b l e As S t r i n g
S t a t i c i n t L a s t i n g V a r i a b l e As Integer
s t r T r a n s i e n t V a r i a b l e - Format(Now(), "Medium Time")
intLastingVariable = intLastingVariable + 1
MsgBox " " & s t r T r a n s i e n t V a r i a b l e & ". "&____
" " & ___
intLastingVariable & " ()."
End Sub
i n t L a s t i n g V a r i a b l e = i n t L a s t i n g V a r i a b i e -t- 1
. i n t L a s t i n g V a r i a b l e Dim, S t a t i c , , .
S t a t i c ( )
. , ( ),
s t a t i c , (.. Sub F u n c t i o n ) , ;
P r i v a t e S t a t i c Sub D o I t A l l ( )
Static Function DontDoVeryMuch{intTimeToWaste As Integer)
, P r i v a t e ( P u b l i c ) , S t a t i c .


.
Dim , .
,
. :
Ldrr. i n t A As I n t e g e r , i n t D As I n t e g e r , ir.tL As I n t e g e r

f46

II. VBA

:
Dim curNetWorth As C u r r e n c y , datSecondTuesday As Date

.
V a r i a n t . ,
Dim s t r X , s t r Y , s t r Z As S t r i n g
s t r X s t r Y V a r i a n t , .


,
( ). ,
. , ,
, .


. , 3 i n t C ,
intC = 3
VBA , , ,
. 3.
. ( , "--".)
:
s t r Q u o t e = " , - " & _
" , . "

s t r Q u o t e . , , .
, .
& () VBA .
, .
, , VBA
, .
, . 2+2-4 , .
VBA
. .

7.

147



, .
c u r S a l e P r i c e = c u r C o s t * sngMargin
c u r S a l e P r i c e ,
c u r C o s t s n g M a r g i n . , . VBA , . (, 6
, c u r S a l e P r i c e c u r C o s t C u r r e n c y . )


F u n c t i o n , . :
ctrFavorite = InputBox(" ?")
VBA I n p u t B o x ,
. ,
( I n p u t B o x 11).

--
VBA. ( ,
). :
| S (, bytMonth b o o l W i n t e r ) ;
~- V (, 1234 );
S ( ;
'" ");
: S VBA (, I n p u t B o x () S q r ());
\ S F u n c t i o n .
,
( , , +), F u n c t i o n , F u n c t i o n .
, ,
.
, ?
VBA
; , "
0.
, , , ,
, ,

148

11, VBA

, , . , . , ,
, , , .
, .

s t r i n g ( )

(" ")

S t r i n g ( )

, , ASCII 0 ( )

Variant

( , ,
)

Object

( , ,
)

&
, ,
. ,
,
, , .

Const
Const
Const

C o n s t :
c s t r P e t s N a m e As S t r i n g = ""
c d a t e T a r g e t D a t e As Date = #5/26/2000#
cboolUp As Boolean = True

, , .
, , O b j e c t ,
D e c i m a l (. . 7.1).
, "" ( constant ). ,
, , . ,
, .
VBA Visual Basic , , , VBA-. VBA Visual Basic v b vbBlue
(, , 16711680)
vbKeyTab (, <>, 9). VBA , , x l B a r S t a c k e d (, Excel, 58).

7.

149

, , VBA VBA-, . (. 5).


, , . ,
, .
:
If bytPa3MepO6yBM > 45 Then

= 75000
End If
, "".
,
. ,
. , .
,
:
C o n s t As C u r r e n c y = 75000
If bytPasMepOOyBM > 45 T h e n
=
End I f

, . , . ,
: , " 75000?", , ,
46 .
.
, , ,
,
.


(, ..) (, , , ).
, '
, , . . , :
C o n s t +;!-: = 1, cbyt- = 2
C o n s t : = 3, cbytropbKi/ = 4

150

II. VBA

Do While intBKyc = 1:

intBKyc = {)
Loop

/
VBA ,
, (, ) .
+ ( ) 3 i n t A :
intA + 3
. ( ,
VB -,
i n t B = i n t A + 3. . ''--"'.)
VBA ,
, . .
+ , .. , .
"" &. VBA
" " & ""
" " .
, + , ,
. ,
i n t C = 123 + "456"
i n t C 579.
:
Tan(sriyAng2eA} <> 1.4
" "'. ,
, 5> True (),
F a l s e (). sngAr.gleA !.},
True, Fwlse.


, , VBA
, , , .
intA + i n t B * inrC
: * ( ) "" ( ). "intA. i n t B ir.iC".
~* , r
. VBA iri -B i n t C ,

7,

151

i n t a . , VBA , .
, , .
(intA + i n t B ) * i n t C
VBA , i n t C .
VBA ?
, VBA
.
1. .
2. .
3. .
VBA .
, , . 7.2. VBA , , .
, .
7.2. VBA (
, )

- /

Mod

+ -

&

<>

( )

<

>

<=

>=

152

II. VBA

. 7.2

Like

I s

Not

And

Or

Eqv

Imp


VBA (. . 7.2).
, +-, -, * /., 6/2 , , 3.
.
.
I V '* , ;
. ,
2

I
|

8 (2 ).

| / \ , |
. VBA , |
. (.
|
, .) ,
|
I

244

\ 7

34, , 244 .

S Mod ,
|
. ,

, 244 34 |
,
I
I
|
|
|
t

2 4 4 Mod 7
6. ,
Mod .
,
(244 Mod 7)

/ 7

, 6/7, 0,857.

7.

153


VBA
, L i k e ( ) Is ( ).
, YBA (=) .
(. " "),
, .
, , T r u e (), F a l s e (). , <= ( ):
. '_

<

-L i

i n t X 12, F a l s e , 12
, ! 1.
I f . . . Then. 8,
:
If ir.tX = 2COC Then

End If
- , . , True (), F a l s e (); : '" i n t X 2000, ".

"" = ""
F a l s e , .
, . , , VBA , '"", .
, VBA
. , .. , .
, , , , , (
, ). , ,
, " " > "" " " > " Z " , , T r u e .
VBA .
, . . , VBA , ..

154

II. VBA

, ,
O p t i o n Compare Text.
( ,
, ). True ():
" " = ""
" " < "Z"


, ? , ,
. :
" , " > " "
True ().
, , . ,
" " > " "
T r u e (), , , , .
Like
, L i k e ,
, , , ( ) . , , VBA
.


( B o o l e a n ) :
boolTheAnswerls = 5 > 4
5 , 4, T r u e (), VBA
b o o l T h e A n s w e r l s True.
T r u e F a l s e
VBA, -1 0.
.
,
:
If intP <= intQ Then
'
End If

7.

155


, & (), .
, ,
. ,
, , , :
;;trA = " " 5 InputBox(" : " ) & _
". " & s t r A n s w e r & " . "
s t r A , , "
. ", - .
.

2)

, VBA. , .
Ob j e c t , , , 12. .


VBA
. , VBA.
VBA
. 11 . , , , VBA . , , , , +,
, . ,
, VBA .
,
. , , - VBA .

Variant
V a r i a n t "" . VBA , , , . ,

156

II. VBA

. ,
( ):
Dim As Variant
= 3
= " ."
=#12/31/99 11:59:59 #
VBA , ,
V a r i a n t . ,

V a r i a n t / D a t e . , V a r i a n t , VBA TypeName. ,

strVar.i_antType = TypeName ()
s t r V a r i a n t T y p e D a t e , .
V a r i a n t .
, V a r i a n t , .
, "
V a r i a n t : ".
, V a r i a n t
. VBA 5 VBA 6 , VBA
( ).


, V a r i a n t ,
, , , ,
. ,
, ,
.
,
( 1 7)
( 1-31), B y t e , 0 255.
365 , I n t e g e r .
VBA
. 7.1.
.
: S (.. )
{
Boolean, Byte, I n t e g e r Long.
f S i n g l e Double, , 15 .
i
, "j
, , -

7.

157

. , , 4,72X10"""
S i n g l e Double, ( + - , VBA ):
s n g F l o a t i n g = 4 . 7 2-2 2
, C u r r e n c y
19 , D e c i m a l 29 ( ). VBA D e c i m a l
, D e c i m a l , D e c i m a l
V a r i a n t .
V a r i a n t
, D e c i m a l ,
CDec.
, ( , CDec ):
Dim d e c v a r P i As V a r i a n t
d e c v a r P i = CDec("3.1415926535897932384626433333" )

, , ,
.
:
Dim b y t B y t e i As Byte
Dirr. b y t B y t e 2 As Byte
Dim i n t l n ~ e g e r As I n t e g e r
b y u B y t e l = 255 ' Byte
bytByte2 = 1
i r . t l n t e g e r = b y t B y t e i + bytByte2 ' !
, , i n t l n t e a e r , , VBA . ,
B y t e
I n t e g e r .


B o o l e a n : T r u e ( 1) F a l s e (0). B o o l e a n , , . ,
1 ,
T r u e (), , , ,
F a l s e (), .
Bool e a n
T r u e Fa s o . , . ,
, :

158

//. VBA

Dim As Boolean
Const As Boolean = True
Const As Boolean = False
If = Then

End If
! B o o l e a n , Not. , Word

:
ActiveWindow. Docurr.entMap = Not ActiveWindow. DocumentMap


C u r r e n c y () . S i n g l e Double
,
, ,
, .


( VBA . ] S, J VBA
, , , Windows.
, , :
C o n s t c c u r M o n e y T a l k s As C u r r e n c y = 5 4 6 3 . 7 2
MsgBox f o r m a t ( c c u r M o n e y T a l k s , " C u r r e n c y " )
>
$ 5 , 4 6 3 / 7 2 , 5 4 6 3 , 72F.
F o r m a t 31.

, C u r r e n c y . F o r m a t
" C u r r e n c y " ,
- , .

Currency
, C u r r e n c y :

7.

159

S , ,
Long;
/ , , ,
I
.
C u r r e n c y 19 , 15 4
( ).


Date . -
VBA 35692,9201273148.
, , . ,

, .
, "'". ,

D a t e , :
Dim dateWeddingDay As Date, dateTimeOfCeremony As Date
dateWeddingDay = #4/20/99#
dateTimeOfCeremony = # 3 : 1 5 : 0 0 PM#
C u r r e n c y . VBA ,

.
,

Format (#10/24 / 8 9 # , "Long d a t e " ) "Tuesday. October 24,


1989", "24 1989 .".

,
. , :
#09/1/1998#
#Sep 25, 93#
#Janua 9 1905
Visual Basic , " ",
Windows. , VBA . ,
, , .


#:: ?-,
AM ( ). :
# 1 0 : 4 5 : 0 0 #
# 2 : 3 : 3 0 #

160

II. VBA

, #01:02:03 #, , VBA
, .
, VBA
. , , - #0:0:23#,
a VBA #12:00:23 #.


VBA , , ,
. , # 3 / 1 9 / 2 0 0 5 # - # 3 / 1 9 / 2 0 0 4 #
'"1 ", , # 1 2 / 3 0 / 1 9 0 0 # . VBA, . , ,
VBA , D a t e A d d ( ) D a t e D i f f ( ) ,
.
11.
, . , , :
Dim d a t e T h e n A s D a t e
d a t e T h e n = # 0 7 : 1 5 AM# + # 1 2 : 0 0 # ' = # 0 7 : 1 5 : 0 0 PM#
d a t e T h e n = # 0 7 : 1 5 : 0 0 AM# - # 0 : 1 5 AM# ' = # 0 7 : 0 0 : 0 0 AM#
d a t e T h e n = # 0 7 : 1 5 : 1 5 AM# + # 0 : 0 : 3 0 AM# ' = # 0 7 : 1 5 : 4 5 AM#
, . , VBA ,
# 0 : 0 : 3 0 # # 1 2 : 00 : 30 #. (
): ,
D a t e .


VBA , ,
. , , . V a r i a n t
,
.
MsgBox,
.

Dim date3TC\l],aTaAHeCTpoKa As Date


date3T<^aTaAHeCTpoKa = #17;23:16#
MsgBox date3ToflaraAHeCTpoKa
, . 7.1.

7.

161

. 7.1.

&.

:
Dim i n t S m a l l As I n t e g e r , s n g T a l l As S i n g l e
Dim varYouAll As V a r i a n t
intSmall = 3
s n g T a l l = 9.99E+33
varYouAll = i n t S m a l l & s n g T a l l

v a r Y o u A l l "39.99+33". (&)
VBA , .
, , .. . -
.
,
. . , ,
.


,
(.. , S t r i n g ) ,
. , VBA
. ,
Dim strGSting As String
strGSting = #July 22, 1904#
"22.07.1904", - . ,
, , ,
.
VBA As Chr ANSI-
. Chr
, (, ). As
.

162

II. VBA


VBA .
(, Dim s t r M e s s a g e As S t r i n g ) ,
. ,
.
.
, ,
, Web ("www . s e l d e n h o u s e . com/vba). , . :
Dim s t r F i x e d As S t r i n g * 5
5 .
, VBA . , VBA 5 , :
s t r F i x e d = " a b c " ' s t r F i x e d " a b c "
s t r F i x e d = " F o u r s c o r e and seven y e a r s " ' " F o u r s "

7.

163


...
>
>
> I f. . . T h e n S e l e c t C a s e

F o r E a c h . . . N e x t Do . . . Loop

>

For...Next,

> Go

, ,
^S
, , . VBA .
VBA
(, , ).

/ qmeux nftotjtaaui
/&aftfUfjafUfft
,
With.
; S , , |'
, ( T r u e F a l s e ) .
I
VBA I f . . . Then S e l e c t C a s e .

i
| /
|
, , T r u e
|
F a l s e . , , |
F o r . . . N e x t , I
, Do . . . Loop (
|
). |
, F o r E a c h . . . N e x t ,
I
12.
\ / W i t h |
(. 12).

164

It. VBA

, ...
. , .


, ,
, .
If . . . T h e n :
If a <
b
a
End If

b
=
=
'

Then ' ,
a ' ,
' .
- .

, , , . , , , .
, ,
, , , .


,
, , .
VBA , .
.
, .
Do W h i l e . . . L o o p
If . . . T h e n , If . . . T h e n Do W h i l e . . . L o o p :
If a < b Then ' I f . . . T h e n
Dc W h i l e b > ' Do W h i l e . . . L o o p
b = b - 1
If > d T h e n ' I f . . . T h e n
d = a

End If ' If...Then


Lcop ' Do While...Loop
End If ' If...Then

!
. , , . , , I f . . . T h e n
End I f . , ,
, ,
.

8.

165


, . , ""
, .
, ,
, : "
".
, , VBA : Do . . . Loop,
I f . . .Then S e l e c t Case. . , F o r . . . Next
For E a c h . . . Next, .


I f . . . T h e n , S e l e c t C a s e Do . . . L o o p : T r u e () F a l s e ()? VBA. ( , VBA 0 F a l s e , True.)
- , , .
, VBA,
7. , . ? .

< b

b =

colTBears ("")
o b j Cur r e n t Bear

Is

s q r ( l / x * 2 9 . 3 2 3 4 ) >=
CDbl ( s t r N u m b e r ) + 12

, c o l T B e a r s
" " , , ,
o b j C u r r e n t B e a r
, 1,
29,3234,
s t r N u m b e r 12


,
, . , .

166

. VBA

, , , , .
,
Internet. VBA, , .
. ,
True, F a l s e .
113 ' True, F a l s e
, , ,
If 118 Then

End I f
T r u e F a l s e , . :
1234 ' True
' F a l s e
True ' True
F a l s e ' F a l s e
intHowManyPets ' F a l s e , intHowManyPets = 0
ingA + lngB + lngC ' F a l s e , =
,
f rmHelpWindow. E n a b l e d .
,
, .


(. 7)
- True F a l s e , , , True F a l s e .
(, ,
) And, Or . , .

T r u e

And


T r u e

3 * 2 = 6
And 12 > 11

True

2 + 2 = 4
And 4 - 2 = 1

False

8.

167

Or

T r u e

T r u e

T r u e

10 > 20 Or 20

True

> 10

5 < 4 Or 6 < 5

False

5 + 5 < 9
5 + 5 = 10
5 + 5 > 9
5 + 5 = 10

True
False

, . , , :
( + b > 20 And = 10) Or (objDoor.Open)
:
" ,
+ , 20, 10,

Open obj Door True".


,
, .
, , . o b j Door. Open , Or .

oneftcunoftbi Jf..
I f . . . T h e n , I f . . . T h e n . . . E l s e
I f . . . E l s e l f .
I f . . .Then ,
.

If...Then
If . . . Then , , , True, ,
F a l s e . :
If Then
(, , = True)
End If
I f . . .Then
" " .
, I f . . . Then.
I S
I
" ".
I
True, VBA , I f . . . T h e n n E n d If.

168

. VBA

IS
I
I
|^
I

, Then If . Then ( ) VBA .


End If, VBA
, .

If...Then
I f . . .Then ,
True, .
End I f , .
If > 20 Then MsgBox " ! "

If > 20 Then
MsgBox " ! "
End I f

If...Then...Else
, , I f . . . T h e n . . . E l s e .
, T r u e , , F a l s e , . :
If Then
(, , = True)
Else
(, , ^ = False)
End If
True, VBA , , , End If. F a l s e , , E l s e .
,
. , :
If TypeOf ctlCurrentControl Is CommandButton Then
ctlCurrentControl.BackColor = &HFF& '
Else
ctlCurrentControl.BackColor = &HFFFF00& '
End If
, c t l C u r r e n t C o n t r o l
. TypeOf ,
, ,
CommandButton.

If...Then
, , , . .

8.

169

VBA, : "
, ,
. , 11.
, I f . . .Then
E l s e l f If . . .Then.

If... Elself
E l s e l f , ,
, T r u e . :
If ! Then
(, , ! = True)
E l s e l f 2 Then
(, , ! = F a l s e ,
2 = True)
E l s e l f Then
(, , !
2 = F a l s e , = True)
. .. ( E l s e l f )
E l s e '
(, , = F a l s e )
End I f
E l s e l f .
E l s e , ,
,
, ,
. , ,
. , .
. ,
,
, ( - ).
If objRollOfFilm.ExpDate < Date Then
MsgBox " ."
Elself objRollOfFilm.Type = "" Then
int- = 1!: + 1
Elself objRollOfFilm.Color Then
intUBeTHbieHeraTHBHHe = 11: + 1
Else
intHBHeraTMBHbie = 11 + 1
End If
ExpDate ( ) .
, , . (.. F a l s e ) , E l s e l f .
() , . " " , .
, E l s e l f ,
C o l o r ( ,

170

//. VBA

, T r u e F a l s e ) .
T r u e , ,
F a l s e , E l s e
-.
I f . . . E l s e l f ,
T r u e . E l s e l f E l s e .

if... Then
I f . . . T h e n I f . . . E l s e l f . ,
, T r u e . I f . . . T h e n : "
X Y , , ".
I f . . . T h e n
I f . . . T h e n . . . E l s e , I f . . . E l s e l f I f . . . T h e n . I f . . . T h e n :
If

! Then
If 2 Then
(, , ! 2 = True)
Elself Then
(, , ! = True,
2 = False)
(, , 1
2 = False, = True)
End If ' I f . . . T h e n
( , , ! = T r u e ,
, 2)
Else
(, , ! = F a l s e )
End I f
I f . . . Then :
If sngGPA > 3.5 Then
If sngUnits > 10 Then

MsgBox " !"


End If
End If


E l s e l f If . . . T h e n , , .
. I f . . . Then, :

8.

171

If sngGPA > 3.5 And sngUnits > 10 Then


MsgBox " !"
End If
, .
, , :
If sngGPA > 3.5 Then
If s n g U n i t s > 10 Then
MsgBox " ! "
Else
MsgBox " !"
End If
End I f
,
I f . . . Then, :
If sngGPA > 3.5 And s n g U n i t s > 10 Then
MsgBox " ! "
End I f
If sngGPA > 3.5 And sngUnits <= 10 Then
MsgBox " !"
End If

oneftcuitoftoe Select Case


I f . . . E l s e l f I f . . . T h e n
. ,
, S e l e c t
C a s e . :
S e l e c t Case
Case !
(, ,
!}
Case 2
(, ,
2)
. . . ' Case
Case Else '
(, ,

)
End Select

Select Case
S e l e c t Case ,
, (. " "). ,

172

//. VBA


. ,
+ b >
, ( + ), , , , (> ).

Select Case
, ,
S e l e c t Case.
S e l e c t Case o b j R o l l O f F i l m . T y p e
Case ""
intCJiafiflOBbie = 11: + 1
Case " "
11 = -^ + 1
Case " "
intHBHeraTHEHbie = -^ + 1
Case E l s e
MgaBox " . "
End S e l e c t
,
I f . . . E l s e l f (
). , ,
, , , C o l o r , .
,
, .
. S e l e c t Case , .
Case If
o b j R o l l O f F i l m . Type = " " Then, .. " " , ,
Case.
, , , . , S e l e c t Case
.

Case Else
Case, Case E l s e ,
S e l e c t Case.
" " . C a s e E l s e , , . , Case E l s e , , , .

8.

173

Case
, Case, , " - -?"
C a s e .
. , S e l e c t
Case :
S e l e c t Case 1^
i n tBo3pacrnauMeHTa, .
, Case, .
I S ,
1
Case 18 35
i
Messages("").Print
I / , , I
. , ,
1
.
1
I / , =,
Case Is > 65
Messages("").Print
I s .
Is , VBA
.
C a s e ,

Case 0 5, 15, Is > 55
Messages("").Print
He . , Case
, Or,
, Case .


"" , . , ,
.
VBA , .

1 74

. VBA

Do. . .Loop
F o r . . .Next
For Each. .. Next

, True

:
, .

Do
Do . . . L o o p
, .
, , Do . . . L o o p ,
I f . . . T h e n " ".
, D o . . . L o o p , . .
1 / , .
S , .
S .
</ .
/ .
| * ( I f . . . T h e n ) I
, |
( 13).

Do.. .Loop
VBA D o . . . L o o p , .

Do. . . L o o p
Do W h i l e . . . L o o p
Do. . . L o o p W h i l e
Do U n t i l . . . Loop
Do. . . L o o p U n t i l


,
End Do
,
T r u e
, ,
True
,
F a l s e
, ,
F a l s e

8.

175

Do While... Loop
Do W h i l e . . . Loop Do. :
Do While
(, , - True)
Loop
Do While, VBA . F a l s e , VBA
, Loop.
True, VBA .
Loop, VBA Do While, .
, , , F a l s e .
VBA , , , Loop.
True, .
,
F a l s e . , . , , , F a l s e .
Do While. . . Loop (, )
, Do
While . . . Loop, , . , , ,
VBA- , Do;
Sub R e v e r s e T h e D i g i t s ( )
Dim i n t O r i g i n a l N u m b e r As I n t e g e r
Dim i n t O n e D i g i t As I n t e g e r , strBackwardsNumber As S t r i n g
Do While i n t O r i g i n a l N u m b e r < 10
i n t O r i g i n a l N u m b e r = __
InputBox(" , 9.")
Loop
Do While intOriginalNumber
intOneDigit = intOriginalNumber Mod 10
strBackwardsNumber = strBackwardsNumber & intOneDigit
intOriginalNumber = Int(intOriginalNumber / 10)
Loop
KsgBox strBackwardsNumber

End Sub


Do , , ,
( , ?).
, i n t O r i g i n a l N u m b e r
0, .
10, True, VBA .
, , . , Loop -

176

//. VBA

uecc , . , , . (
, , , I n t e g e r . )
VBA
. ,
. True, Do While
i n t O r i c i n a l N u m b e r > 0 Do While i n t O r i g i n a l N u m b e r
.
, , .
, , , ,
, .
I / Mod, i n |
t O n e D i g i t 10. 10,
I
, () .
I^
|
I S
f
v
I
I
I
\S
|
|
|
;

, , .
10,
. .
, , , , I n t . I n t ,

.
i n t O r i g i n a l N u m b e r
0 , ( , 10, I n t ).
VBA F a l s e , ,
.

Do
Do W h i l e . . .Loop,
Do, , " Do While U n t i l " .
D o . . . Loop While
Do W h i l e . , .Loop Do. . .Loop While : Do W h i l e . . . Loop , D o . . . Loop While
.
Do While . . . Loop ,
True, . F a l s e , .
Do. . .Loop While, ,
, ,
, ,
True. Do . . . Loop While ,
.

8.

177

, D o . . . L o o p
W h i l e , ( ),
. , , , ,
.
Do Until...Loop
Do W h i l e . . .Loop Do U n t i l . . .Loop , .. , . Do
W h i l e . . .Loop Do U n t i l . . . Loop.
, . : " , " " , ". , .
Do While Do U n t i l .
Do While = ,
Do U n t i l .

Do Do
Do ,
VBA . , , , VBA
E x i t Do.
Do, E x i t Do
, .
, ,
:
Do While strA <= "2"
If Len(strA) > 1 Then
Exit Do
End If
strB = strB & strA
strA = GetNextCharacter
Loop
E x i t Do
I f . . . T h e n S e l e c t C a s e . , - . E x i t Do
, , ( ' )
.

178

II. VBA

Do While Until
Do W h i l e / U n t i l . . . L o o p
, . ,
?
D o . . . L o o p W h i l e U n t i l .
I f , S e l e c t C a s e .
E x i t Do, , .
Do . . . L o o p :
Do
(, )
If Then
Exit Do
End If
(, , )
Loop
, , , , .
,
.
D o . . . Loop ,
:
Sub GetAnAnswer()
Dim strAnswer As S t r i n g
strAnswer = InputBox("Enter your answer

(A-E)")

Do
If strAnswer = "" Then
strAnswer = InputBox("You d i d n ' t e n t e r a n y t h i n g . "
& " Please type a l e t t e r between A and E.")
E l s e l f Len(strAnswer] > 1 Then
strAnswer = InputBox("Your answer should be" _
& " only one l e t t e r long.

Please t r y a g a i n . " )

E l s e l f strAnswer < "A" Or strAnswer > "E" Then


strAnswer = InputBox("You typed an i n v a l i d " _
& " c h a r a c t e r . Type a l e t t e r from A to E.")
Else
Exit Do
End

If

Loop
End

Sub

E x i t Do , , I f n E l s e l f , .

8.

179

Do While T r u e , . T r u e () True, , , , VBA


. , ,
E x i t Do.

For...Next
, ,
F o r . . . Next. , , .
.
, .
F o r . . . Next ;
For =
(, )
Next
Immediate ( Visual Basic Immediate
<Ctrl+G>):
Sub CountToTen()
Dim j As I n t e g e r
For j = 1 To 10
Debug.Print " " & j
Next j
End Sub
.
, j 1; , . Next j j ( 1) . j 10, .

For.. .Next
, . 1 F o r . . . Next, .
, , (, , ).
,

. , 0 ( - ).
Next , F o r . . . Next, - Next
VBA . Next.
F o r . . . N e x t

180

//. VBA

,
N e x t . He F o r . . . N e x t .
, , . !
.

F o r . . . N e x t , .
, .

For... Next
F o r . . . N e x t , . 13, F o r . . . N e x t
.
F o r . . . N e x t
, :
Dim 1 ^ (14) As I n t e g e r
F o r a = 0 To 14
intMac-(a) = a * a
Next a
15
(15, 14, VBA ' 0). F o r . . . N e x t , , 0 14, . ,
, , .

For... Next
VBA, F o r . . . N e x t , - , .
:

Dim sng~ ' , " "


Randomize '
For = 1 5
sngC = Rnd()
For = 1 5
Debug.Print sngC * Rnd()
Next
Next A
, ,
VBA .
1.

.

VBA.

8.

181

2. For. . .Next.
VBA Rnd, sngC .
3. For. . .Next.
, Rnd
. Immediate.
4. .
. Next
A, VBA .
5. . 2 3 .
, , ,
, . , -,
- . , ,
- ,
.
F o r . . . N e x t
, , .

Exit For
E x i t For
, .
( I f . . . Then S e l e c t Case), For . . . N e x t .
E x i t For, , , .
, , -
- "" .
- ,
. , :
For = 1
If () - " ! " Then
MsgBox " ."
E x i t For
End I f
() = varlleHa(p) * sngCOLA
Next p


F o r . . . N e x t S t e p () , , , :
Sub ListOddNumbers()
Dim strOddNumbers As S t r i n g
For F = 1 To 33 Step 2
StrOddNumbers = strOddNumbers 5 F & " "

182

If. VBA

Next F
MsgBox " 1 33 :" &
Chr(13) & strOddNumbers
~
End Sub
, , . - 2 , S t e p . 1, F , .
, S t e p VBA,
. , S t e p , 1. S t e p , , . ,
.
.
S t e p ,
, .
S t e p , ,
, .

notnoicau Qo Jo
, , ,
. Go To

. , . ,
( VBA), .

Go To
Go To
S p e c i a l V a l u e , :
F u n c t i o n GoToExample (ItemNumber As I n t e g e r )
D_m i n t R As I n t e g e r
S e l e c t Case ItemNumber
Case 2412
Go To S p e c i a l V a l u e
Case Is < CutOffValue
DoSomething
Case >= CutOffValue
DcKardlyAr.ything
End S e l e c t
i , /,- )
GoToExample = i n t R
Exit Function
SpeciaLvalue:

8,

183

DoSomethingSpecial
GoToExample = -intR
End Function
S p e c i a l V a l u e
.
VBA .

Go To
Go To .
, "",
- . "" ,
Go To . , , ,
.
Go To , , . , , . Go To ,
.

184

//. VBA

"" :

/ftou ...
>

, ""

>
>

>

Visual Basic, S t e p Immediate,


Locals Watch

> Visual Basic Immediate Watch


> ,

VBA .
w s , , . ,
, VBA. ,
, .

See //
HftoucxoqMfl
, VBA ( ), .
/ . , .
/ . , , , , .
S
1

/ . , ,
( , " ").

, , , . , ;
, .

9. "" : 185

UcnfwueHue,
, Visual Basic
, , . - , Visual Basic ,
,
, , ( , Editor () Options
() Auto Syntax Check ( );
, ToolS^Options ). ,
If = 3 T h e n , C o m p i l e e r r o r : E x p e c t e d : T h e n or GoTo ( : Then GoTo).
VBA , . , VBA ,
GoTo

.
, , .
,
, , , VBA.

qj& nftozftaMMuanoe
, , . , 2 4- 2 = 22, , - .
. , , - , , . ,
. , , .
, , ,
, .
,
, , .

, ,
, , , , .
, "", , .
, , -

186

//. VBA

, '"", .
.
S ,
. ,
VBA- .
S , ,
(, ).
1 S , I
.
| S
|
, I
. , , I
, , ,
|
.
\ / ,
|
( 29 ) (
I
). ,
|
.
I
|
.
,
. , , .
,
. ,
, ,
. ( , , .) ,
VBA , .
, .
, ,
.


. 9.1 , . .
9.1.

()

<F8>

<Shift+F8>

<Ctrl+F8>

9. "" :

187

. 9.1

<F9>

<Ctrl+Shift+F9>

<Ctrl+F9>

<Shift+F9>

<Alt+F5>

<A!t+F8>

!
VBA.
.
''", . , ,
S t e p , , ,
, , .
S t e p
.
. 9.1 VBA- .
, , , Visual Basic ,
.

-:; VBA lode for Wwd - tupyBo


l cko
l f lie (Code)

I
. 9.1. VBA

188

. VBA

,
,
.
, (
, " ").


, - .
S t e p I n t o (.
" ").
.
, ,
, .
S t o p .
,
, S t o p .
Break (), Rurr^Break <Ctrl+Break> . , -
I
. , I
, ,
I
.
| V Break When Value Is True (
\
) Break When Value Changes ( ).
|
, 1 True .
- , . VBA (. 9.2). End () , Debug () . General () ToolS1^1 Options
, .

. 9.2. ,

9. "" : 189


, - , . . 9.3 ,
Visual Basic
.

"33

II (cKecktlniffs > 2) Ar.j


Quant I t . 3how

Er,d ;
dec 1
ti.3

It

4 t h
. Ada ilnieiOunncltv)
Objects - .fcunc
Enii U;-_h
I* txtQuantityHidden - "Cencsl" The
ehecktimes

. 9.3.
Visual Basic
,
,
, ,
( ) . VBA ,
, .
S t e p ,
.
,
. <F9>.
, .
, VBA ,
.
, VBA
, . ,
.


, . VBA .

190

. VBA

, <F9>.
, D e b u g ^ C l e a r All
Breakpoints <Ctrl+Shift+F9>.
Undo () .


, Visual Basic ,
.
. , ,
. 9.4 ( , , ).

orFd

i :

EEII - VbK

'.
:"
.,

(1

The
+ 1

Oua ntltV
u
: f Ouanc ltliHlddr n
chsc ttimes - 0
IK it
Er,a I I
c h e skeines - 0
Ex ^ Sufc

e e l " Thru

1;
S
,
...

fh = r d etsFtS
Ob] ta

.C O u [ , t

d .--L

111

. 9.4. He , ,
( , )
, , , . ,
?
.
Show
N e x t S t a t e m e n t ( ). D e b u g ^ S h o w
Next Statement, Visual Basic
. ,
, , "
".

: Stop
, :
. , ,
. , , Visual Basic
, .

9. "" : 191

S t o p . . .
S t o p .
, ? :
intflaHHbieCMapca = (1.5454)
Stop
MsgBox " " & intflaHHbieCMapca /Z
, , S t o p
, , . S t o p
, - . ,
S t o p , ,
.


( ), VBA . , , . Run ()
, - Continue (). -
<F5>. , , , .
, R e s e t
( ). Reset Reset
Run. <A!t+F4> Windows,
VBA-. <Alt+F4>, ,
VB -, Visual Basic,
VBA-, - Windows, .


, Visual Basic , .
, ,
.
, ,
. Visual
Basic, Auto Data Tips ( ),
Locals (
) Watches ( ),
. .

192

II. VBA

, , S t e p Visual Basic, : S t e p I n t o ,
S t e p Over S t e p Out. Debug,
Debug, .

Step Into

<F8>

Step Over

<Shift+F8>

Step Out

<Ctrl+Shift+F8>


S t e p I n t o ( ) , . , , VBA , . S t e p I n t o <F8>.
- ,
: Sub F u n c t i o n , S t e p I n t o ,
, .
S t e p I n t o . ,
S t e p I n t o . S t e p I n t o
, ,
.


S t e p Over ( ) <Shift+F8> S t e p I n t o , :
S S t e p Over
I
;
| S S t e p Over .
, , , S t e p Over , ,
.
, , , .
S t e p Out ( ) <Ctrl+Shift+F8>
S t e p Over. , .
, Step Over,
Step Into i ).

. , S t e p Out.
VBA , , , .

9. "" : 193



"" , . VBA
, .
,
.



.
. , ,
. , , .
(, ),
VBA .


, , , , . "",
, - .
Visual Basic '
. , . , , , .
, ,
. ,
. , Immediate, W a t c h e s Locals, .
.
I S ,
(
<Ctrl+F9> Debug<=>Set Next Statement .
>

S I
(. 9.5).

, . , . ,
, .
. -
"" "" . ,
"" Comment Block
( ) Edit (} (. 6).
194

//. VBA


GoTo. , VBA GoTo , : . ~
GoTo
A=+
'D = + / F

G+D
:
MsgBox '"
Format(Now, "dddd"!
, GoTo .
:

Salesperson. Locked

e'-

Keycodc - vbKsyRecurn The

eheclctime: cliecluMmes + 1
QuanlltyTcim.SftOB

checktuiws
Exit

SUB

Sith

it
Vt
'^

ordstsBS

.*dd

obj=et=

.CDUOC

. 9.5. , , ,

VBA
. , , (. 9.6).
, ,
Auto Data Tips ( )
Editor () Options ().
Visual Basic ,
. <Ctrl+I>,
Quick Info ( ) .

9. "" : 195

, , - . ,
, . 9.7,

Z1

]ll*erF rm
-1

KeyC ode - vbKeyfi:


Sal
on.Enabled ' Ti ue
Sal
on.LoeKed - Falre
Sal
on.secFocus
E. i
sell

"

. Quent
KcyCade -

Focn.
vbK

Tl

";':

]ehklir **

Tl

ei.

S a l e : pec son. Locteed - false


Sales per son. SecFoeus
fm Ivb
s e l l Ke y C o 0e vOKeyRe t + n The.,

> 2) And (cboHainlteroEntiy,Valu


Quant ir.yrorm.snov
If QuantlcyHlOden - "Cancel" TSieu
rhec-lctimes - 0

-.

s >- 2) And (cfcoHai Itemlntcy.Value - ;


Quan cicyFocn .Shoe
^

."S

eheclctin ea -
)H

~.
If
cheekt lines - 3

=UE

lid I f

EMS

chec ifClnes EJL:

Zr.'X

eyfli

Sub

End I :
V: Eh o r d e r s l 3
erQu antity)
. Add
.Co n.

It

th ocdecsPS
.Jidd (EncetQu
objects - .Co

A / G 5.6! 5

. 7 Quick Info


Jtntnediate
Immediate ( , . 9.8), <Ctrl+G> Viewolmmediate Window . Immediate :
, D e b u g . P r i n t ;
, : Immediate, <Enter>.
" ?" . .
Immediate .
Print

(27

398)

+ 1414

<Enter>, (. . 14.8). Immediate D e b u g .


Immediate
, , D e b u g . P r i n t .
(. 9.9), , , .

196

II. VBA

. 9.8. Immediate
| S Immediate
:
P r i n t |
.
. ,
*
Immediate ,
^
, , |
. ,
I
Immediate ,
|
.
. Immediate , ,
( , <Ctr!>, Immediate,
). <F1> Immediate ,
, ,
. .

204 3
2

Single
Osgw

53

nager

sngArayqeni
rtHmpCounto

"OanQQDrSWng

BuderColor
Boran Style
Cafc)r

-21"433 OLE_COLOfi
-JH? 483630 OLE_COL0fl
tmBanerSI yic ( r S l y
' A l of the Mo String

1
FIBU =1S. 2
*1S
'J 115
~4gu tM
qu

JFornV
IFwmv
: Formv
2 FonnV

. 9.9. ,
Debug. Print

See / noq nftucjioffifuxu


Locals
, Locals (
), , . Locals Debug
View ^ L o c a l s Window .
. 9.10, Locals , , .
Visual Basic ,
Locals .

9. "" : 197

For i = 0 To 15
ArcayOIPendoi
Anniversary = Now

FortcCount -= UsecForms.Count
^^ - AcrtiveUindci'. Select ion, Coyrit
Mt-' - CSterItems) " otejtclj au
CusromClb^ects. Add ;proFora. Contrals

!1= - ^ t r (Items)

* " fh

. 9.10. Locals


Visual Basic, Locals ,
. Visual Basic 5.
, ,
.
, ,
, .
. 9.10, , Locals , , . ,
.
Locals ,
. ,
(, , ,
. 9.11).
Locals ,
. , ,
Locals. , . Locals .

198

II. VBA


'j*i!ftft'1. *".*.-!*" '
*'/*
'^*"
^- ~1
*
'

"

- " ' '


* 4 f ' j i i "' *"' """" *"
:

yg.'f* * '
J - J . - " ,""
**
" ^ i
..."

'

'

Message = "
C u s t o m O b j e c t s . A d d (ProFocmc

ActiveWindow.SelectAll
Items = ActiveWindow.Select
Message =

"

!
t
.U)

. .

'

"

ITroe
EtyteMsker/StyleMa
Colleclion/Colleclion
Integer
Integer
" : i. String
ffll 12 99 12 3)'00#
Date

a
0

?
%

PnqWOKMJO
R SlyteMaker
X f l CustomObjects
1- FoimCourt
llemCount
Message
Anniversary
- JDefault
- SackColor
- BorderColor
- BordeiStyle
Caption
-1 Control;
- Cycle
i
-g

"??DDDODnDDDQOD? Slnna
21J7483633
OLE COLOR
-21478630
OLE_COLOR
ImBorrferStyleNone
fmS order Style
" ! of Slrpng
Controls/Conlrols
ImCycleAIIForms
fmCycle
DrawBuffer
32000
Long
Enabled
True
Boolean
Font
Fom/Fonl
FoieColor
2147460
OLE_COLOR
Height
103,6
Single
HelpComextlD
Long
0
InsifleHeight
85,
Single
InsidaWidlh
236,2
Single
KeepScrollBaisVisitifmSciallBaisBoth
fmScrollBars
IblCountOIHems
Label/Label

iU
-3
z,.

li

,
r

-1 j

;. / /. Locals , . 9.10, ,
,


, , ,
, , , .
| S , I
. , I
. ,
|
, .
| S ,
I
. S e t N e x t
I
S t a t e m e n t { ), \
, Locals.
1S
1
I
|

, ,
(,
Internet). Locals .


Locals, .
1. ( ) , .
.
2. .
, ,
, #.
3. , <Esc>, <Enter>.

9. "" : 199

Visual Basic ,
,
.
,
. , , ,
. , ( ,
). , , , (. . 14.11).

cfteqafWo otnuacjKu: Watches


Locals, W a t c h e s (
) . Watches , , , Locals, , , . W a t c h e s . 9.12.
j Watches
;i
:
Expression'%'; ' pf'~ ' l Value'-,.' ^lTVoe '~ "
-" I c i r l r e
Stf sngArrayltem
2904,3
Single
14 12
&rf FormCount
2
Integer
i o
52
Integer
14 12
&rf intTempCounter
FouriAnO)jmion/FormAnOpinicPiic14 1^ FormvariableDerti^B
!|1
"??DDDDaa[String
Pnc14 12.FormVariablaDem
- _Defaurl
BackColor
-21474B3633 OLE_COLOR
Pncl4 12.FormVariableDem
BorderColor
-21474B363Q OLE_COLOR
Pnc14 12 FormVariableDam
fmBorderStyle fmBordeiSlyle
14 J 2. F ormVari ab I e D e m
- BorderStyle
~ Caption
"InFormAtiue" String
=14 12 FormVariableDem , 1

. 9.12. Watches Visual Basic


Watches , .
Watches, , ,
.
W a t c h e s , . ( , , " W a t c h e s Locals".) , , .

Watches Locals
, Watches , Watches
Locals .
s Watches VBA
, . , (X
- Y) >, 15, " - " & s t r C o l o r 2 + 2.
Watches .
200

. VBA

s - Watches , Context ().


,
. ,
<Out of context> ( ). :.


,
Watches. , .
, , , . "" , , .
( , ,
),

.
, .
| S
|
Add Watch, Add

Watch.

; S Debug^Add Watch ,
|
Add Watch.
IV
|
{j
^

Quick Watch Debug DebugoQuick


Watch . Visual Basic
, - .

/ Watch.

- .
,
, Add Watch

Add Watch.

Add Watch
. 9.13 Add Watch ( ),
Debug^Add Watch
Add Watch ( ) . Add Watch , .
Expression () , .
, ,
.

9. "" : 201

Context () , Visual
Basic . Procedure
() Module () , .
, . ,
, .

|Ai)<i Wili.li
.--

11

If* Procedures]

j Project:

i
:

..,--_-_
_^j

Care!

#"P

.VBIQVBA

wattfiType
Break When vato is Jrue
V~ Break When value *

. 913 Add Watch


, ,
, , (
Procedures) ( ), .
(All Modules) ( ).
Watch Type ( )
Watch Expression ( ).
, "
",


,
Expression () Watches.
, ,
, .
.
Locals, Watches
. Context Watches . Edit
Watch ( ), Debug ( ) Watches. ,
, Add Watch
,

202

II. VBA



. , , ,
. ( ,
) ,
.
Watch Type ( ) Add Watch
Edit Watch .
IS

I
|
|

, ,
, (, VBA 0 F a l s e ,
True). , Break When Value Is True ( ).

I S , ,
|
, I
. , I
B r e a k When Value Changes ( ).
, "" Watches ,
. . . 9.14
, .

aseChanger MAIN

. 9.14. "
* caseRange /

, VBA , . , ,
. Watches , , , (. . 14.34).

9. "" : 203

* "quicux "

wor rr
- , , . VBA .
. 9.2.
End ( ), Debug ( ), Help ( , ).
,
, . , ,
, , VBA
. ,
.
,
. , , .


, ,
.
I / ,
i
, VBA. , , |,
,
i
. |
. , I
, VBA .
I S , |
. , , I
Internet
|
.
,
.


VBA , ,
, , . - , VBA , , , .


, :

204

//. VBA

S On
?
;

E r r o r , VBA,

| V E x i t Sub ( E x i t F u n c t i o n ) ,
;
^ ,
\
.
. , On E r r o r VBA ,
.

On Error
On E r r o r , VBA ,
. : On E r r o r GoTo , , .
On E r r o r :
Sub ErrorHandlerDeno{)
On E r r c r GoTo E r r o r H a n d l e r
MamaVariable = DoThisFunction (X,Y,Z)
P s p a V a r i a b l e = DoThatProcedure
EabyVariable = MamaVariable + P a p a V a r i a b l e
' ,
E x i t Sub
ErrorHandler:
( )
End Sufc
, On E r r o r .
| S On E r r o r GoTo 0 , \
, E r r . ,
|
On E r r o r GoTo ,
|
-
|
" " VBA.
| S
;

\
I
\.

E r r o r Resume N e x t VBA
, ,
. , , On E r r o r
Resume N e x t , , 14.

Exit
, , VBA
. ,
. , ,
.
, , E x i t ,
" On E r r o r " . Sub,
E x i t Sub, F u n c t i o n E x i t F u n c t i o n .

9. "" : 205


:
I S , , ;
I / , ;
| / , ;
I S ,
I
( ).


VBA (, , ). ,
On E r r o r . T h i s I s T h e E r r o r H a n d l e r :
ThisIsTheErrorHandler:
( )
End Sub
, End Sub.


. , ,
.
, , , E r r VBA.
E r r VBA-. , . E r r VBA .
E r r , Number ()
D e s c r i p t i o n ().
Number . , :
Sub ()
On Error GoTo
( )
Exit Sub
:
Select Case Err.Number
Case 7 '
( 7)
Case 11 '
( 11)
... ( ..)
206

. VBA

Case Else
( )
End Select
End Sub
S e l e c t Case Number
, . .
1 S , ,
I .
.

,
.
Number , . - . VBA-
Office 97 , ,
Trappable Errors ( ) VBA.
, t r a p p a b l e e r r o r s
. Office 2000 .
- , VBA , D e s c r i p t i o n () E r r .

, , D e s c r i p t i o n :
strMoeCoo6u;eHMe = " , " _
& " " __
& " . " _
& " (VBA), "
MsgBox strMoeCoobmeHHe & E r r . D e s c r i p t i o n
VBA- ,
. , Microsoft Access
E r r o r .

, , , , , , .
, 1> Resume. Resume .
1 / Resume , I
, . , |
, .

9. "" : 207

, , Resume Next. , , .
,
Resume .
, - , , , , . ,
.

208

II. VBA

10

VBA-
/3 & ,...
5>
>
> VBA
>
> , ,
> VBA-
> ,

[
VBA ,
W*1 , ,
. ( ) . -
- , .
: , Visual Basic,

. ,
, , .
.

//cpoftM
, . .


(..
) . .
1. .
, .
2. .
<F5> Run ().

10. VBA-

209

VBA- ( Visual Basic


). ,
. , , , .
. Visual Basic

Visual Basic. ,
<F5>. Macros (),
Cancel (), , .
, ,
, ,
. <Alt+Tab> Visual Basic,
R e s e t ( ) .
, Visual Basic .
VBA-,
, ,
, 4.


,
VBA. , .
; . , ( ) . . .
.
, Microsoft Access: VBA Access VBA, Access
,
VBA-, Access VBA. Access VBA, , Access,
, VBA-, .


VBA ,
. , , . ,
, .

210

II. VBA

1. .
, , ,
.
2. ,
.
,
, ,
?
.
3. ,
.
? . , .
4. , .
, ,
( , ).
5. , ( )
.
. ,
,
.
6. , .

? , , , , .
, .
, ,
, , .
,
.


VBA- ,
, .
,
- .
Visual Basic, .
1. ,
, ,
, .
2. File=>Print <Ctrl+P>.

10. VBA-

211

3. Form Image
( )4. Code ( ),
.
5. Current Module ( ), , Current Project ( ),
.
6. .


(. 10.1)
.
, ,
.
* *

. 10.1. (
)

, ,
. VBA, Visual Basic. , . ( 5.)


VBA Insert^UserForm Visual Basic
( ) . (, ) -

212

II. VBA

, (. . 10.1) , , .

Toolbox
, -
, . ( Toolbox).
,
. , , ,
. ,
, ViewOToolbox, .
, .
1.


, .
2. .
.
3.

,
, (. 10.2).
4. .
.

. 10.2.

, . ,
. ,
.

10. VBA-

213


VBA , , . , , .
, , . Visual Basic ,
,
. , ,
,
. 30.3. ,
View^Properties Window <F4>.

]VB***

[ 3 SH80OD0ODFS
SH80OX012S
- fmBcrderSC/leNone
userFofml
8HB00CCC12S
D - fmSpecuEffeciflat
0 - fmCycleAIForms

32GCO
()

0 fmMousePonterOefa

. 10.3. VBA
, - ,
. <F1>, VBA,


.
| / ,
|
( , ).
\ ^ \
(. 10.4).
,
, .
, ,
Alphabetic ( ) (),
Categorized ( ) .

214

. VBA

, .
, , .
SpuButton
UserForn
< 5plnButton
ComboBo
ToggleButton
i Te*ffioi
label

-lentatloo
-1 - FmOnentationAijto

Visible
True
Behavior
EnabteC
True
H Data
Coniro Source
H Mist
HelpContextlD
Mouselcon
^ore)
MousePomter Q - fmNVxeePomterDefault
TaMndex
1
Tabstop
True
Tag

. 10.4.


,


,
:
;
;
.
, .
, ,
. ,
, . ( ,
; . . 10.3.)


,
. ,
, , (

10. VBA-

215

). VBA
.
, , . .
, ,
, , ,
, ,
.
, . ,
()
, .
. , .
,
, .


. , , . , .
, .

, .

(Name)


.
,

Caption

Caption ()
, . ,


Font

216

II. VBA

ForeColor

BackColor


, VBA- ,
. .
, , .
Name ().
, . ,
. F o r m s
F o r E a c h ,
, .
, , Name , , -
.
VBA ( ; . 6). ,
.
. , .
, , . C a p t i o n () ,
.
, ,
. ( .)


: , ;
, . , , VBA ,
. ,
"", ,
, , .
VBA 6.3 ( , VBA 6)
.

S h o w M c d a l T r u e , , .

10. VBA-

217

, F a l s e .
, , , .

, .
, , .
, ,
, .
VBA 6,
VBA, , VBA 5
ShowModal .


,
, , ,
Visual Basic . .

. ,
, .
.
. H e i g h t () W i d t h ()
, , ,
. , ,
Windows ( Paint ), , . , , .
| ^ ,

.
/ , .

.
, .
, .
,
.



, H e i g h t W i d t h ,
( 1/72 ).

218

II. VBA


,
( Visual Basic). S t a r t U p P o s i t i o n .
1 - C e n t e r O w n e r . , VBA- (, , , ).
, , , VBA-, S t a r t U p P o s i t i o n 2C e n t e r S c r e e n . , O-Manual, L e f t ( ) ().


, , . , L e f t ( ) ().


, S p e c i a l E f f e e t , . , 0 ( ), , .



, . ,
" ",
. .

, ,
Windows, Visual Basic , , . . ,
Cut (), () Paste () Standard () Visual Basic. (
, Microsoft Office.)
P a s t e .
, VBA
, .
, .


, , <Delete> Edit^Delete. ,
<Backspace> .

10. VBA-

219


, , , . .
.
% / Toolbox , -:
, ,
t:
.
1
, .
S
|
|
I
IV
1
.
|

, , <Shift>,
. , , .
, <Ctrl>.
,
, .
, , ,
. , .



U n d o (), <Ctrl+Z>. , , .


, "" . .
S , [
. , ,
\
, .
I S
I
\
|
|

.
, ( ) "" . , .

. , , ,
.


, Too Is 1 ^ Options . Options () General (), , , (. 10.5).

220

II. VBA

P CeFiQal C
m Ocmsdi
(7 BK&rsuntf Con**
c** I
. /0.5. /Z/
General Options
, .
if S Show Grid ( ). , ,
i
. 1
.
1 ^ Width () Height (). (.. ) .
Align Controls to Grid ( ).
, "" .
,
. , ,
.


,
, . , , , ,
Make Same S
.
!:',:
? 5S4
mstoto
fit
, VBA
Grid
. , ,
VBA .

Format
, , Format ()
Visual Basic (. 10.6).

.

10. VBA-

. 10.6.
Format

Visual Basic

221

UserForm
Visual Basic UserForm. , , UserForm
. . 10.7 UserForm
.
-


. 10,7.
UserForm


UserForm Format.
, . ,
VBA , . ,
.


, , .
, ,
- ,
. . 10.8 .

| ~

~ " ;

. 10.8. . ,

222

II. VBA

.
1. , .
2. Group () UserForm
Format<=>Group .
. , , ,
Horizontal Spacing^Make Equal ( " ").


, , .
, V i s i b l e ,
, .
Visual Basic - .
, Order () Format, .
Order.
, , , FormatoOrder^Bring
to Front (1^ 1^ ), .
, ,
Format^Order^Send to Back (^^ ).
, .
, , Format^OrderoBring
Forward (^^ ) Format^OrderoBring
Backward (^^ ), , . .


Format .
, , .


, . , -

10. VBA-

223

, .
VB .
Format^Make Same Size ( ), , , VBA
(, ) . Align
()
, . Horizontal Spacing
( ) Vertical Spacing ( ) Format
, .
. 10.9 ,
. , .
.
1 -

. 10.9. ,
,

, : ,
,
<5:+> <1+> . , .


<51+>


<11+>

<1+>

224

. VBA


, ,
, .
, Align (), .
, .
, .
1. ,
, (. ).
2. Format^Align ,
.
, Align () UserForm ( ) .


Format Make S a m e Size ( )
. , .
UserForm.


Horizontal Spacing ( ) Vertical Spacing
( ) Format
, . , ,
. .
. S

k
|- S
f
;.

Make Equal ( ).
( ). , . , .
Increase () Decrease (). , . , .

"' S Remove (). ,


;
.
I
.

, tfequMe
xoftoiuo/

VBA, , (
).

10. VBA-

225

, , . .
, ,
.
Visual Basic, . ,
, . - . ,
" ", .

Enabled Locked
E n a b l e d L o c k e d ,
. ,
, . , ? .
, .
, . , - , Cut () , .
E n a b l e d , ,
, . Windows . , , Windows .
E n a b l e d T r u e ,
. E n a b l e d F a l s e , Windows
() , (. 10.10).
.. >','.! iHP'FWIJHMB
9

Mi i i

rauyuteitofBt
, , f Do (

|vout*nrtk*me|

tin '

j I)H *>'C) -.j i

. 10.10.

,

L o c k e d , . L o c k e d
T r u e , , ,
, (
" " ). E n a b l e d
T r u e , .


Windows , <> . -

226

//. VBA

<>. (
, <Shift+Tab>.)
,
. <> UserForm ,
.
, . .
, ?
, , . , View^Tab Order (^ ). , . 10.11. , ,
, ,
Move Up () Move Down ().

2*0**.

. 10.11.
Tab Order ,
,
VBA

T a b l n d e x . T a b l n d e x 0 , 1
.. ,
VBA .
, Tablndsx, F a l s e . . Tablndex. True, , .


, ,
.
, . ,
<Alt>, , .
, Accelerator
() Properties ().
.
. VBA
.

10. VBA-

227

,
C a p t i o n , , .
1. .
.
2. , .
3. .
, , . . 10.12 , .

10.12.
<A!t+N>, ,


, .
, ;
- . . .
, , .
. . 10.13.
, .

rrpofVco
J

_J

.zi
J

. 10.13.

228

//. VBA


. S p e c i a l E f f e c t 2 ( s u n k e n ) B a c k C o l o r .


, .
, Capt i o n Properties (). , . .
, <Shift+Enter>.


,
. Properties ().
. 10.14 . , .
[S
|
I
I

Wordwrap True (no ), ,


VBA
, .
Wordwrap F a l s e , .

f /
f
I
*
;;
I^
I

A u t o S i z e True, , . WordWrap T r u e , . Wordwrap F a l s e , , .


T e x t A l i g n , ,
, .
fforttwrapKTruBin
Wordwrap tt F*K* In
m label, slowing
Mart to onap from.
If* to toe.
ttMttoSba
ropartyiet
-oTrue.

. 10.14. , ,
WordWrap

AutoSize

, .
,
. , . ,
. , , .

10. VB-

229


, , , C a p t i o n . , .
, :
LbllnspirationalMessage.Caption = "Laugh and be happy!"
, , . ( 11.)


, .
, . . 10.15
.
ImewtHM * * wVe
The viewwas marvelous and
the staff friendly, but... the
room was too warm, the water
was too cold, the food was
overcooked.. and wfliat was
lhat beeping noise all night?
1

t '
* I

. 10.15.

,

, , .
I n p u t B o x ( 11).
I n p u t B o x
.
I S . I
I n p u t B o x .
iS
|
IS
|

, , ,
.
, .

, ,
, . , . I n p u t B o x ,
, .

230

//. VBA


.
, .
, , , ( ) ,
. . Value Properties (). {: .)
Value, T e x t .
, ,
, Value,
Text. Value Text ,
. Value , , , .
Value ( Text) , " , " "
" .

,
, ,
V a l u e Text.
, :
str'TextBoxText =txtMessageFromUser .Value
s t r T e x t B o x T e x t
, x t M e s s a g e F r o m U s e r .


,
Value,
,
Value T e x t VBA.
Value ,
. :
t x t R U L i s t e n i n g = "Do as I s a y ! "
,
:
strWhatHeard = txtSoundOffTextBox


A u t o S i z e , Wordwrap T e x t A l i g n ,
. .
" "
. Wordwrap , , 19, " VBA-".


- , ,
. .

10. VBA-

231

, , OK, C a n c e l Guess Again, F r i e n d . , . , Properties () Picture , . ,


. . 10.16 .

Get eFvour choice-

O
dtHereT
or
H
Vo
ciaekeHT
eoaurd

WuMnao
tM

. 10.16.
, . , ,

, , .
C l i c k , ,
VBA, , .


<Enter> .
,
<Enter>, , .
, D e f a u l t True. , .

Cancel
, . , , , Cancel
(). ,
'Never m i n d " " F o r g e t i t " . ,
, .
, <Esc> , , Cancel ()
( - ). C a n c e l T r u e , ,
<Esc> ,
Cancel ().

232

. VBA

C a n c e l T r u e , ,
. <Esc> C l i c k .


VBA . .
.
/ .
|
, .
|
(. 10.17).
| S , .
*\
---!
f

\ tjoTKoJ

j
i
t

\
1

- k e M e H H W B
j

. 10.17.


" ", ,
. .


,
. . , .
.
i S . I
. I
> ,
I
. .
| S
%
*

.
, .
, .

, , (. 10.18).

10. VB-

233

. 1018. ,
,


, . , .
, .
( ) .


, , . , , , ,
. , , . , ( !).
Windows
. , .
. . 10.19 .
liHiTlliifiiilH i f l 1i i M W ' l l

Mkig srsB*nsum pmta


" DOtAMgos
<"* Ettiiopb Houte

. 10.19.
, .
, .


, . , , . VBA
. .
: "
"? , VBA, :
. . ,
, ( , ,
Windows, 19).
, . .

234

II. VBA

, VBA , , , , ,
. . 10.20 , .

Toddy's roan

Morning | Mkktay btmwa'

Deivetn office ("MhwitmniM <" No daHvurr today

. 10.20.


, .
.
. , ,
. , ,
.
: , ?
V a l u e . , . I f . . . E l s e l f :

If OptionEuttonl.Value = True Then


ChosenOption = "Bill"
Elself OptionButton2.Value = True Then
ChosenOption = "Bob"
Elself OptionButton3.Value = True Then
ChosenOption = "Barney"
Else
ChosenOption = ""
End If


,
. , ,
.
, Yes ()
No (). On Off, True False Stay Leave.
, .
| S - , , I
Yes, On True. ( , .)

10. VBA-

235

I / .
1
, , , .
f.
. 10.21 .

. 10.21.


,
. . 10.22 .
, ,
, .
!
~
F

>

~
~ *

" (]

- ^

. 10.22.


, Value , . , Value True,
, Value F a l s e .
: Value True; .
Value F a l s e . :
If tglLightSwitch.Value = True Then
TurnLightOff
Else
TurnLightsOff
End If
236

//. VBA

,
N o t . ,
, , :

ChkYesOrNo.Value = Not chkYesOrNo.Value


,
,
10 -12 .
.
.

-
,
-


, Windows .
, (. 10.23).

preferred "tad dnstoe C


o
ji n miliard irfnagrct*
lemon
Thousand Illand

" "
Sltee(0rtM

in
omccllnit
lorMla

. 10.23.

, -
, .
, VBA .
, .


.
,
, . : , , . , , . 10.24, , .
,
. , .

10. VBA-

237

, ,
,
, , ,
. .
: VBA , ,
. , , , . ,
.
,
.
, S t y l e 2
( f m S t y l e D r o p D o w n L i s t ) . : ?


. Properties () , . A d d l t e m ( Excel Access).
,
A c t i v a t e . , :
P r i v a t e Sub U s e r F o r m _ A c t i v a t e ( )
cmbOptionPoll.Addltem " O v e r p o p u l a t i o n "
cmbOptionPoll.Addltem " G l o b a l warming"
cmbOptionPoll.Addltem "No time to s m e l l t h e r o s e s "
cmbOptionPoll.Addltem "No r o s e s to s m e l l "
cmbOptionPoll.Addltem "Taxes o n t h e r i c h too h i g h "
cmbOptionPoll.Addltem "Too many s o c i a l s e r v i c e s "
cmbOptionPoll.Addltem " I n a d e q u a t e s o c i a l s e r v i c e s "
cmbOptionPoll.Addltem "HMOs"
End Sub

, ,
.


,
, Value .
, :
strOption = cmbOptionFoll.Value

tpofui
, , ,
, .
.

238

II. VBA

, !
VBA- , . ,
, , ?
- , VBA- , . VBA Visual Basic, .
, , VBA-
.


VBA ;
I S ;
I / .
VBA, .


Show. , F o r m I C a , :
FormICa.Show
, Show U s e r F o r m , -. ,
Show , ,


,
L o a d . L o a d , (
Show):
Load

, ? ,
. ( , ), .
, , .
, , .


L o a d , , .
, , -

10. VBA-

239

Visual Basic ,
, :
, (
), .
, ,
. , ,
VBA . , , :
Sub DisplayDateCaptionedForm()
DateCaptionedForm.Caption = Now
DateCaptionedFcrm.Show
End Sub
, VBA-. . 10.24 ,
VBA, Visio:
Sub DisplayShowSelectionForm()
Dim ItemCount As i n t e g e r , Message As String
Items = ActiveWindow.Selection.Count
Message = " : " & CStr(Items) S " . "
ShowSelectionForm.lblCountOfItems.Caption = Message
ShowSelectionForm.Show
End Sub

. 10.24.
, "'" :
ShowSelectionForm.lblCountOfItems.Caption = Message

240

II. VBA


, , . VBA , , . .

Initialize Activate

.
, , .
I n i t i a l i z e () A c t i v a t e () .
, , , . .
!^
1
1
I

I n i t i a l i z e , .
A c t i v a t e , ( ).

, " ".


Hide (), VBA- .
:
FormErly.Hide
, Hide . , Hide , .
, ,
, .
, Hide , , VBA , , Hide
. , ,

Hide
.
Hide C l i c k
() , . ,
" " " ".
, Show .

10. VBA-

241


, , ,
. VBA- ,
, .
, ,
U n l o a d F o r m A t i o n .
, .
, , H i d e
U n l o a d . U n l o a d Me U n l o a d _ , Me
.
, H i d e , U n l o a d ,
. U n l o a d , (.. , ),
, .


Sub, , , . , - , .
, . ,
, .
, . , , .
, :
, , .
. .


VBA .
( VBA, , "" , ,
.)
, . . 10.1.

1 0 . 1 .

Activate

,
( )

addCont r o l

242

II. VBA

. 10.1

AfterUpdate

""
,

""
,


V a l u e

;hanae
Click

DblCl:.ck

D r o p B u t t o n C l i c k
(
<F4>)
Enter

Error

Exit

KeyUp, KeyDown,
Keypress

Layout

RemoveControl

Scroll

,
,
,

Zoom


( Zoom)


. 10.1, . - ? ,
. -
.

10, VBA-

243


VBA. , . , , Visual Basic .
, ,
, . ,
, .
1. .

. View^>Code ( )
, , <F7>,
UserForm.
2. , .
(. 10.25).
3. , .
, .
! \ | 5 . v s d - Monyconl/ols ( C o d (General)

(Genera)l
ChtckB oxl
CheckBsx?

ComboBoHl

Activated
*
= " 1*

Co m m an d ut(o n2
Framel
Frama2
Imagei
LabeM
Label?

Click ()

.
njyupOuawijjjH;_n;TH i.StantUpPosition = 3

_*1
. 10.25.

, VBA . , VBA ,
(. . 10.25). , VBA
.

, VBA ,

244

II. VBA

\ \Visi i V B A v n d - Mnnyconlml-j (Cud i r a n


| CommandBultoni
Private

*rhi click

Sub Conrn n d B u t t o n l _ C l i c k (

End Sub
=

Visual Basic
. 10.26, S u b . , , . , ( ), VBA. :
Private- Sub cmdCalcuiateSquareRoot_Click()
End Sub
Private Sub UserForm_Activate()
End Sub
Private Sub sclVolumeControl__Change ()
End Suk


,
, , , . VBA ,

.
, , C l i c k ()
, C o m m a n d B u t t o n l .
C o m m a n d B u t t o n l ^ C l i c k . ,
, . , 1 _ 1 1 , , , .
VBA
, , . -: ,
Cut () P a s t e () .

...
Windows ( C l i c k ) . , , , ,
. Windows,

10. VBA-

245

, .
,
( , , "
Click'').
.
, C l i c k ,
, - . :
P r i v a t e Sub cmdCountClicks__Click ( )
' i n t C o u n t
'
S t a t i c intCount As I n t e g e r
intCount - intCount + 1
c m d C o u n t C l i c k s . C a p t i o n = " " _
& intCount & " ( ) . "
2nd Sub
. , , i n t C o u n t 3. , C a p t i o n . ,
i n t C o u n t VBA . Dim, .
cmdMoveThisForm:
P r i v a t e Sub cmdMcveThisForm_Click()
Move(Left - 2 4 ) , (Top -24)
End Sub
,
24 24 .
, Move
, VBA , , .. .
, cmdMoveThisForm. Move,
. , ,
, L e f t .
, , , .

, VBA
.
, .
, :
Click

246

. VBA

; , , VBA , :
.
( P r i v a t e ) , .. VBA
: P r i v a t e . , .
P r i v a t e P u b l i c .

. : ( P u b l i c ) \ , ( ).

Click
VBA C l i c k . ,
,
C l i c k , , . ,
, .
, VBA .
. VBA . VBA ,
,
( / , /
..). , .
VBA , .
? ? ? ,
, .
V a l u e , , . V a l u e ,
C h a n g e . V a l u e ,
. , C h a n g e ,
" ".

Click
C l i c k . , , , . , - .

C l i c k :
P r i v a t e Sub U s e r F o r m _ C i i c k ( )
. . . ( , )
End Sub

10. VBA-

247

, U s e r Form, , . C l i c k ,
, VBA
, - .
, , .

Change
, C h a n g e .
, . VBA Windows ""
, .
, , , , . C h a n g e . C h a n g e ,
(.. V a l u e ) . , ,
-
V a l u e .
, ,
.
Af t e r U p d a t e , .
,
. :
, ;
, ,
, ;
, , .


K e y p r e s s , KeyDown KeyUp, . K e y p r e s s
"" (, , ), , .
<1+>,
<Backspace>. ,
K e y P r e s s .
KeyDown KeyUp, ,
, <Alt+Shift+CtrI+F9>. , K e y P r e s s , . , KeyDown, <Ctr]+<> <Ctrl-*>> , , 10.

248

II. VBA


, , ,
, , , ,
. ,
.


, , ,
. , ,
, ,
, , , .. .
, . ,
.
I /
|
|

,
,
, .

I S
I
|
I
|

,
.
, . ,
.


, ,
C l i c k .
, :
P r i v a t e Sub crndClose_Cick (}
Hide '
End Sut
P r i v a t e Sub cmdCar.cel_Click ()
Unload frmOptions
End Sut
H i d e U n l o a d . "
" " ".
, , , - , , ( ) .
P r i v a t e Sub c m d C l o s e _ C l i c k ( )
Message = " " _
& " " _
5 " ? "
If MsgBox(Message, vbYesNo) = vbYes Then
Hide ' ,
End If '
End Sut

10. VBA-

249


<Esc>.
<Esc> , . ,
K e y P r e s s -
C a n c e l T r u e .


. ,

, .
.

C l i c k . , ,
. Hide, Unload. txtCName
t x t C A d d r e s s . . t g l S e n d , , S e n d B i l l T o C u s t o m e r .
, :
P r i v a t e Sub cmdOK_Click()
strCustomerName = txtCName .Value
strCustomerAddress = txtCAddress.Value
'
If t g l S e n d . V a l u e = True Then
SendBillToCustomer
End I f
Hide
End Sub


, ,
.
.

, . . ,
.
- - , (. 10.27).
, .

250

II. VBA

- *

- ! ,

. 10.27.

Change

I f . . . T h e n S e l e c t C a s e . :
P r i v a t e Sub spnVolumeControl_Change(>
If s p n V o l u m e C o n t r o l . V a l u e = 13 Then
MsgBox "13 . "
End I f
End Sub



.
,
, .

K e y P r e s s
(
)

C h a n g e (
)


B e f o r e U p d a t e (

)

c l i c k

10. VBA-

251


, .
" V ?
S , ?
S ,
?

252

II. VBA

III

VBA
" ,

, ,
."

> &...

VBA ,
III 11
VBA;
,
. 12
. 13
,
,
.

11

VBA
...
>

>

F o r m a t

>

>

>
>

, ,
^ * . VBA
,
. . - .


, VBA , . ,
VBA - ( - 12). , , -, ,
, .
, , VBA , , , .
, , , VBA-, .
. (. 7), VBA
, . . ,
.
. , C h D i r ( ) , , :

ChDir{" ")

11. VBA

255

. ,
F u n c t i o n , 7, , . , Tan ():
! = Tan (!5)
, :
If Tan ()

< 45 Then

. P r i n t , Debug ( Immediate) Visual Basic.



Debug . P r i n t (^)
X / P r i n t ,
[
P r i n t .
I
14. , , VBA , ,
I
,
-
I
( 12).
. 11.1 VBA-, (, , ). .
11.1. ,

Randomize

Sqr ()

Format ()

Date

Date

Err . Raise

E r r

VBA- F o r m a t .
(19.12.99), (19--99)
(19 1999 .),
VBA (VBA ). , . . F o r m a t , , .

256

III. VBA

F o r m a t VBA 5, VBA 6, VBA 6


, .

Format
Format, VBA 5 VBA 6, .
, , .
Format ( , , VBA):
Format(,"")
, ,
, (.. ). ,
.
,
. , .
Format, Value
Capt i o n . ,
l b l D a t e M e s s a g e . C a p t i o n = " " & _
Format(Now, "Long d a t e " )
" 19 2001 ."
l b l D a t e M e s s a g e ( , 19 2001 ).

Format
. 11.2
VBA. Format. He
( ).
11.2. F o r m a t

(

)

G e n e r a l N u m b e r

2001,5599

Currency

2 001,56.

Fixed


,


(
)

11. VBA

3390,10

257

. 11.2
(

)

Standard

1 323,45

Percent

, 100,

{%)

12,54%

Scientific

1,23+02

Yes/No

, 0,

True/False

, 0,

On/Off

, 0,

/ , ,

15.03.9817:27:45

/
General Date

Long

Date

15 2001 .

Medium Date

15--01

Short Date

15.03.01

Long

(, ) ,

17:27:45

Medium Time

( ) 12-
/ ,

05:27

S h o r t Time

( ) 24-

17:27

Time


, , . , , F o r m a t
" > " " < " . , Format Function
( F o r m a t ) VBA, See Also (. )

258

III. VBA

User-defined Formats ( ).
, . ,
VBA l i f :
MsgBox " " & Format(Now, " h : n n " ) & _
". " & Ilf(Format{Now, " a / p " ) = " a " ,
" . " , " . " )
,
" 9:07. ". , . 11.1. (, MsgBox
" ".)

. 11.1.
. Format " h : n n " .
h ,
10:00. ,
10 .
F o r m a t I l f . ,
" / " , " " , " " , , 12
. /
, , I l f .

Ilf
I l f I f . . . Then ( 32). :
I l f (, , )
"" :
, ,
, .

Format(Now, " / " ) = " "
F o r m a t , .
, , .
VBA 11 f , . , I l f ,
, .

11. VBA

259


VBA 6 , Format, F o r m a t Number, F o r m a t D a t e T i m e , F o r m a t C u r r e n c y F o r m a t P e r c e n t
. Format,
-
. , ,
, ,
Format. .
I S FormatNumber(,,
|
,
|
,
). . , FormatNumber
,
Windows. , Windows ,
132 328,55. FormatNumber ,
, 1,
(, ).
F o r m a t D a t e T i m e ( , ). . , ,
Windows. ( ,
Format), , vbLongD a t e v b S h o r t T i m e .
F o r m a t C u r r e n c y ( ) . ,
, .
F o r m a t C u r r e n c y FormatNumber
.
F o r m a t P e r c e n t ( ) . 100
(, 0,05 5,00%). F o r m a t P e r c e n t , FormatNumber. , 0
(, F o r m a t P e r c e n t ( . 0 5 , 0)).

9, VBA " ". , , ,


. ,
.
, VBA
. ,

260

III. VBA

I S , VBA , ;
| S , VBA ;
| ^ .
VBA CBool, CByt e,
CCur .. , Ob j e c t . , boolMaybe = CBool (123) b o o l Maybe True ( , ,
True, , - ).
, , , ,
, VBA . ( Dec . 9.)
F i x I n t ,
. , C I n t CLng, , I n t (4 . 98 9) 4, 5.
.
I n t , a F i x
.


Hex Oct ,
.
VBA ,
Visual Basic .
:
&, & ( , 0). , i n t B a s e s = 10+&12 + & i n t B a s e s 30, ( ).


VBA .
.
I S C S t r . ( O b j e c t ) , ,
\
. ,
I
Windows. ,
I
C S t r ( 2 0 0 . 0 2 ) " 2 0 0 , 02". ,
|
,
1
,
|
.
I / S t r . ,
|
.
I V . I
, , -

11. VBA

261

.
. , CDbl ( " 2 0 0 , 0 2 . ")
2 0 0 , 02; , CDbl ( " $ 2 0 0 . 0 2 " ) .
V a l . , ,
, . , ( , ). , .
, Val ("28 1 9 0 . 4 3 12 by 14 ") 28190,4312.
Chr. ANSI- .
, , .
Asc. Chr -
.

cMftOKoutu
VBA
, . . 11.3
, .
11.3.
, ( ,
) .

( )
( )

Asc ()

Chr ( _ )

,
_

Filter
(_,
, ,
)

(
VBA6)

, , .
_
.
F a l s e ,
,

Format {)

. " "

Hex ( )

InStr (,
1, 2)

, 2 1;
,

I n S t r R e v ( 1,
2, )

(
VBA6)

, 2 1,
1;
,

262

III. VBA

. 11.3

J o i n (_,
)
(
BVBA6)

( )
( )
,
_ ,
. J o i n
, ,
(
)

,
, ,

Len ()

LCase ()

LSet _
=

,
,

LTrim ()

Left (,
)

Mid(,

, )
Mid(

, ,
) =


, ,

Oct()

Replace(,
, ,
,
_,
)

(
VBA6)

,
.
, _ _
. _ ,
, ,
(
}

Right (,
)

,
, ,

RSet _
=

,
,

RTr im {)

Space ()

11. VBA

263

. 11.3
( )
( )

Split(,
,
,
)

( ,
VBA 6)
.
,
, (
,
).

StrComp(!,
2)

, ; -1, 1 ,
2 ; 1, ! ,
2 ( 9)

StrConv(,
)

String(,
)

StrReverse
()

{ ,
BVBA6)

Trim()

UCase()

, , ,
.
.

Len Left
, , :
", , . "
", , . "
", , . "
( ; 13).
, - . , .
, str-,
:
strHoeaH = L e f t ( s t r C T a p a f l , Len(strCTapan) - 7)
VBA , , .

264

III. VBA

1. Len,
.
2. , . 1, 7,
( ).
, .
3. L e f t , .
L e f t , ,
, ,
.
4. , , L e f t ,


, , . :
strMMfl = Mid (strHoBafl, I n S t r (str-, " , " ) + 2)
Mid ,
. ,
. ,
I n S t r 2.
I n S t r ( ) ( ).
, . ,
I n S t r , , , , .
, I n S t r , 2,
, .
Mid , ( ,
).

qatfuutu (
, . VBA ,
, ,
.
. 11.4 .
VBA.
, , .
, , VBA , (. 7).

11. VBA

265

11.4.

#, .

( )
( )

Date

DateAdd (,
, )

DateDiff (,
1, 2)

, 1 2

DatePart
)

DateSerial (,
, )

, ,
, ,

DateValue
( )

Day

, ,

0 23 ,
,

0 59 , ,

0 12 ,
,

MonthName (,
)

( VBA 6)

, ,
,
1 12.

T r u e ,

NOW

Second ()

0 59 , ,

Time

Time ( )
Timer

Date

()

()

Hour

()

Minute
Month

266

(,

()
()

///. VBA

. 11.4

( )
( )

T i m e S e r i a l (,
, )

, ,

TimeValue
(__)

,
( )

Weekday

, ,

WeekdayName
(__ ,
,
)

(
VBA 6)

, ,
_ ,
1 7.
T r u e ,

.
,
_ _
vbMonday, vbTuesday ..

Year()

, ,

()


VBA
, .. ,
.
Now
Data, , , datJIoBnMoMeHT = Now.
, VBA . ,
, .
, (.. ) VBA,
Time, (.. ) ,
Time. , Time :
dat3ToMoeBpeMH = Time ' Time
'
Time (113:15 #) ' Time
D a t e D a t e .


, .
, . ,

11. VBA

267

. , , , . VBA .


DateAdd, ,
.
, ,
( 7, 60, ..), .
DateAdd : DateAdd ( , ,
_).
,
( ) . , , . . 11.5 , ( ).
11.5. D a t e A d d D a t D i f f
( )

ww

,
datBbino = DateAdd ( " s " , -90, Now)
datBbino , ,
90 .
, , Format (, Format (datBbmo, "Medium
t i n e ")), " ".
DateAdd :
31 , 30 .
DateAdd , , .
DateAdd , , , ,
, .

268

III. VBA

VBA ,
, D a t e S e r i a l T i m e S e r i a l . ;
D a t e S e r i a l (, , ]
T i m e S e r i a l (, , )
, ( Date). , , , :
i n t r o f l = 1999
intMecHU = 12
intfleHb = 3 1
^ = D a t e S e r i a l ( i n t r o f l , intMecHu, intfleHb + 1)
, , .


D a t e D i f f , ,
(, , ..) . :
1 = DateDiff ("m", #2/12/90#, #10/12/01*}
1
Long , .
D a t e D i f f ,
" " , , , - .


F u n c t i o n D a t e D i f f
D a t e S e r i a l .
,
, , (
I s D a t e ) , , .
F u n c t i o n WhatAge(dateDateOfBirth As Date)
Dim intAge As I n t e g e r
> 255

' Long,

intAge = D a t e D i f f ( " y y y y " ,

dateDateOfBirth,

Date)

, :
If D a t e S e r i a l ( Y e a r ( D a t e ) , M o n t h ( d a t e D a t e O f B i r t h ) , __
D a y ( d a t e D a t e O f B i r t h ) ) > Date Then
intAge = intAge - 1
End If
WhatAge = intAge
End F u n c t i o n

11. VBA

Jlftoafibte
-, , , VBA
( , ..)- VBA- , .
, , , , VBA-.
VBA
( 10), , , .
VBA-, MsgBox I n p u t B o x , .
MsgBox , , ( ) , .
I n p u t B o x , .


MsgBox :
MsgBox([,

[,

[,

_, ] )

, , , , , .


MsgBox .
, . :

MsgBox " MsgBox."


VBA , . 11.2 ( ).

.
.

. 11.2.
, ,
.
. , VBA
. ,
, . 11.2 ( ):

270

. VBA

intWishCount = 3
datWhen = Format(Now, "Short date")
strlnfol = "Ha "
s t r l n f o 2 = " : "
MsgBox s t r l n f o l & datWhen & s t r l n f o 2 & intWishCount
,
( ASCII- 13) Chr
( . 11.2).
MsgBox " ." & Chr(13) & " .'
, ( ASCII- 9).


, , .
.
"", . 11.2. . 11.3 ( ).

^?

. 113.
,
, , , , . . 11.3 .

,
, .
, VBA-, . . 11.6 .
11.6. VBA

vbOKCnly

vbOKCancel

vbAbcrtRetrylgnore

11. VBA

271

. 11.6

vbYesNoCancel

vbYesNo

vbRetryCancel

vbCritical

16

vbQuestion

32

vbExclamation

48

vblnformation

64

vbDefaultButtonl

vbDefaultButton2

256

vbDefaultButton3

512

vbDefaultButton4

768

, ,
, . 11.3, 531.
? :
i n t A = MsgBox(" ", vbYesNoCancel
+ v b D e f a u l t B u t t o n 3 , "VBA ' ' " )

vbCritical

, ,
v b D e f a u l t B u t t o n 3 , ( ) , . .
. 11.3, , , , ,
<Enter>.
?
. . ,
.
, MsgBox , , . ,
, VBA .
.
272

III. VBA

vbOK
vbCancel
vbAbort
vbRetry
vblgnore
vbYes
vbNo

, , ,
If . . . Then. :
If Msg3ox ("?", vbYesNo) = vbYes Then

Else

End I f
, I f . . . T h e n . . . E l s e If.


VBA, (. . 11.2). , MsgBox (. . 11.3).


,
, I n p u t B o x . , , :
Input3ox([, ] [, ])
. 11.4, ,
, , .
, I n p u t B o x :
s t r B = InputBox(" ?", "TWA", " ")

. 11.4.

11. VBA

273

,
, , .
, MsgBox. ,
. ,
, ,
<Enter>.


B o o l e a n
Not. .
,
boolBlinking = Not(boclBlinking)
b o o l B l i n k i n g T r u e ,
F a l s e , , , F a l s e , T r u e .


VBA . (, )
. VBA .
, ,
, . ,
VBA
. , ,
VBA , .


. 11.7 VBA, .
, " "
.
11.7. VBA

Abs ()

A t n ()

Cos ( )

{)
F i x ()

274

,
(. I n t )

///. VBA

. 11.7

i n t ()

. i n t F i x -
: I n t ,
a F i x

Log ()

Rnd ( )

Sgn ( )

1, , 0, , - 1 ,

S i n ()
s qr ( )
Tan ( )


VBA , , F u n c t i o n , .
, , , , , . , VBA
, .
, derived math functions ( ) . , , N Log (X) /Log (N).
, - , ,

Function,

. ,
.


, . , VBA ,
.
VBA 6 Round ( VBA 5 ), . ,
= R o u n d ( 2 . 5 0 5 , 2)
2, 5, 2 , 5 1 , . Round, , , .

11. VBA

275

VBA,
Format: , ,
( , .
" ");
= Format(, " # , # # 0 . 0 0 " )
VBA 6 F o r matNumber:
sngOKpy = FormatNumber(, 2)
. F o r m a t , . , " # , # # 0 . 0 "
. FormatNumber , , .
, , ,
S t r i n g , S i n g l e , Double, D e c i m a l , C u r r e n c y
V a r i a n t , I n t e g e r Long, .
, ,
(, ). , , . :
sngOKpyr = Format( / 100, " # , # # 0 . " ) * 100
,
. ,
F o r m a t 10, 10. 10 ,
. 2, .. .


, .
, , .
VBA .
\ S Randomize. , %
VBA. , Randomize ,
|
. , ;
.
"-, / Rnd, \
. Rnd
I
Rnd .
:
( S i n g l e ) .

276

III. VBA

. , , ,
, . Rnd ,
:
_ = Int( +(Rnd() * ))
,
.


VBA - . , Pmt .


Pmt , (
) . Pmt:
Prat(, , [, [ , ] ] )
, , . . 8%- , , 8% .
. 08/1 (8% 12 ).
, , . 5
5 * 12, 60. .
. , , , ,
Pmt
. ( . ,
.) ,
( 0 )
( 1).
Pmt , Double. :
dblPay = Pmt (.08/12, 360, 300000) '
dblSav = Pmt (07/12, 120, 12500, -75000} ' = 75000.


VBA . 11.8.
, . , VBA, .

11. VBA

277

11.8. VBA

DDB(, , )

FV(, , , )

Impt(, , , )

IRR(

MIRR((), _,
_)

Nper(, , )

NPV(, ())

,
,
( )
( )

Pmt(, , )

PPmt ()

(, , )

Rate(, , )

SLN(, , )

SYD(, , ,
)

278

///. VBA

2)fttfiue ^
,
VBA . , , .
11.9. VBA
,
, .


( )
( )

ChDir .)

(),

ChDrive( )

CurDir

( )

Dir()

,
,

FileCopy ,


s ( ,
, )

FileDateTime()

D a t e
,

FileLen()

( )

GetAttr()

,
,

Kill


(
)

MkDir

Name As

/
,

RmDir

11. VBA

279

. 11.9


( )
( )


.
,
. VBA

SetAttr ,

AppActivate ,


.
T r u e , VBA
,

Choose(,
!, 2,
... )

DoEvents

Windows
, .
VBA ,

-

,
,

RGB(,
, )

, RGB-
, ,
, (

)

Randomize

Rnd('

Environ

()

Environ

()

280

!, 2,... ,

( )

III. VBA

. 11.9


( )
( )



,
.
T r u e ,
,

,
.
,
,

SendKeys ,

Windows
Shell(

DeleteSetting

__, ,

Get A l l S e t t i n g s
(_,
)

V a r i a n t ,

Windows
{ )

GetSetting


Windows

(__,
, )
SaveSetting
_,
, ,

Spc{

P r i n t # Debug . P r i n t
Tab()

,
P r i n t #
D e b u g .

11. VBA

281

. 11.9


( )
( )

IsDate ()
IsNumeric()
IsObject()
IsArray()
IsNull()
IsEmpty()

T r u e ,

; F a l s e

Len ()

,
,

TypeName ()

VarType ()

282

III. VBA

12

...
>
> , VBA-
>
>
>
>
> ,

>
>
>

For Each . . . Next With

VBA -
*J .
VBA,
VBA-. ,
. VBA.
VBA, . VBA- .

tnajcoe oJbetafl
, , VBA, , ,
,
.

VBA-
VBA . (shape) Visio -

12. -

283

, (connect),
(. 12.1). ,
, , .
, , , .

. 12. l VBA
Excel ,
, , , , . Microsoft Office
VBA- ,
.
VBA ,
. ,
" ", .


,
,
. ,
.
, .
, , VBA- .
V Microsoft Excel CustomView ( Excel
:
).
^ Microsoft Word F i l e S e a r c h ,
, "
( )". ,
\
, .

284

III. VBA

Visio S t y l e , .
VBA , VBA-. , , C o l l e c t i o n
, , ,
.


, VBA
- . ,
.
- : ,
, .. , ;
, .. , ,
;
, .. ,
.

, , - , , . , , .
, .
? : , , .
, .


, .
, . , ,
. , , ,
.
, . ,
, .
, , ,
. - , .
, ,
.

12. -

285


VBA- . ,
, . ,
. , S h a p e s Visio Shape,
Pages Page.
,
. , VBA C o l l e c t i o n ,
.
, VBA .
, " ", ,
, 13 , ( , ) VBA- C o l l e c t i o n ,

&
, VBA . , ,

. ..
VBA-
. ,
.
. 12.2.

The Microsoft11 Visio'


object model

. 12.2. Visio - \Tisio 2000


. 12.2, VBA-
Application (), ,
. - -,
,
. ( VBA- 7.)

286

III. VBA


VBA , , VBA-
. , . 12.2, ,
. , , , C o n n e c t C o n n e c t s , M a s t e r ,
... .


, VBA,
VBA-. VBA- .
"", Component Object Model (COM) Microsoft
(Component Object Model ).
,
,
. ,
, . , VBA
Visual Basic,
(, C++), .
VBA, . ( )
, ,
Visio, Word, Excel ..
, ,

-.
, , , ( 14). , VBA .

Vtfd /
VBA .
, , , (. 12.3).
, , VBA , .. , (, , ), .
VBA U s e r F o r m .

12. -

287

-L S-S.9

/25. VBA
VBA,
. UserForra VBA-, ,
UserForms, , . ,
UserForms C o n t r o l s , , .
, , , , .
VBA .
, , ,
, ,
. ( , ) 10 19.

6 //
, , VBA. , , , ,
, .


-, , . ,
, . , ,
, , , .
, My Shape . L i n e S t y l e L i n e S t y l e
MyShape Visio. MyWorksheet. C a l c u l a t e
C a l c u l a t e MyWorksheet Excel.
, : " ,
?" ! , '' ", , . , .
288

III. VBA


" ", VBA ( ),
, .
,
.
VBA ,
(. 12.4). ,
, Microsoft Excel Objects.
, , . , . ,
, , , , . (,
VBA , VBA. VBA . 4.)

. 12.4. Excel
,
Visual Basic.
. 6.


, .
, ,
, , .
, D o c u m e n t P a g e s ,
. S h a p e F i l l , -

12. -

289

. CornmandButton ( )
C a p t i o n , , .
, VBA- ,
, , - . , , .
, , ( , ).
,
. , Shape Visio .

AreaUl

Fi 11S t 1

Visio,

Text

.
, ,
. , ,
E n a b l e d , , .
, AreaUZ Shape Visio,
.
. >
, True Fal . , .
,
VBA.


, , , .
, .
. , ,
, .
, .


,
, ,
,
VBA. , , .
, , . , ,
(True F a l s e .

290

III. VBA

, ), B o o l e a n . , , .. . ( VBA
. 7.)


, , , , F u n c t i o n . ,
. ,
, , , .
, , . , , ;
Dim 1_ As I n t e g e r
int = ].
,
, .
? , . ( , ,
" 8 ,
".) ,
.
, ,
. :
If ]. > 8 Then
11;0 = ^ + _
^ *2)
End I f
, , . - ,
VBA , .


, . , ,
, .
^. = 999
, , ^ 999:
]. = F a l s e
^. = " . ."

12. -

291

,
, .
,
. , , o b jMyaMea . :
^ = " . ."
, , ,
. , , ,
.


" ", . - ,
. ,
Toolbar.ToolbarItems
T o o l b a r l t e m s - T o o l b a r , T o o l b a r l t e m s .
( ,
" ").


, ,
, . , Visio P a g e s , )' ,
Pages.Parent
. , , , ,
A p p l i c a t i o n :
Pages.Application

qeuanuu
, .
, , ,
, .
, , , R e s i z e , , R o t a t e , .
, , C a l c u l a t e , , C l e a r , . , , P r i n t () Save ().

292

III. VBA

, , ,
. , , ,
. ,
^ , -. ,
. :
obj .

VBA, 7.
, , . ,
, :
^. 3
, (
, , , a intCKopocTb ,
):
^. 2, , intCKopocTb
, , F u n c t i o n .
, , :
^ =
__].(2, ,intCKopocTb)
,
. , ,
, , Add.
, .
, ^
,

, 11: = ^. . ,
.

, , . .
8

/ , , , . (, -,
, , , VBA.)

12. -

293

, . ,
, ,
, ( Word
A p p l i c a t i o n D o c u m e n t ) . Visio P a g e s
, E e f o r e S h a p e D e l eze (
') T e x t C h a n g e d ( ). 4.
, , ,
VBA-. , , .
: .
10.
, , , , , ,
. .

VBA, . VBA- ,
. , VBA
, ( , ).
- . , , .
.


,
"" . , .
VBA, . -
,
, . . , " ". : " ?"' -

294

III. VBA

, 3
, , . (,
, , "?'") ,
3 , ,
" " . , ,
VBA .

,
. , (, ) , (, - ).
: ,
. , ,
.
, ,
Word;
ThisDocument.Sections(2).Range
, . , Range
S e c t i o n , , , ThisDocument. , , , , .


VBA A p p l i c a t i o n ()
. VBA , , , .
A p p l i c a t i o n D o c u m e n t s ,
. ,
- Documents. ,
Documents (5) Document 5 Documents.
, , T h i s D o c u m e n t .
VBA- ThisDocument
Document, .
Word Document S e c t i o n s , S e c t i o n s , , .
T h i s D o c u m e n t . S e c t i o n s
S e c t i o n s , ThisDocument. S e c t i o n s , .
, S e c t i o n s (2 ) .
. ,
.Range S e c t i o n ,
Range. , Range.
, , VBA
" ".

12. -

295



ThisDocument.Sections(2).Range
S e c t i o n s . VBA
, .
VBA- .
, S e c t i o n s Word S e c t i o n ,
P a g e s Visio Page. (, VBA C o l l e c t i o n , . ,
, 13.)
, . .

1 </ . 1
{2 )
1
S e c t i o n S e c t i o n s .
1 / . .
|
, , I
. , Visio Page , I
:
I
ThisDocument.Pages (" ")


,
, T h i s D o c u m e n t . S e c t i o n s (2) .Range.
,
, . , ,
.
, , ; , . -,
, VBA ,
. -,
. , , ,
.
.
1. ,
.
2. , .
.

296

. VBA


, .
Dim ( , , P u b l i c ,
P r i v a t e S t a t i c Dim). :
Dim ^ As Object '
Dim objShapeObject As Shape ' Shape
.
, , . .
, , . VBA .
: , , , .
, , , . ,
| S . ,
i
VBA .
, ,
, ,
.
, .
|
|

. ,
, , .

| V . , |
, .
,
.
\ /
* . , I , . , i , .
I / % .

VBA- (
| 10 19), .
, V a r i a n t ,
. ,
, , , .

12. -

297


,
. S e t . :
Set objShapeObject = T h i s D o c u r c e n t . P a g e s ( I ) . S h a p e s ( 4 ]
, , (. 9). ,
, .
, Set.


, . , ,
, .
. S e t
N o t h i n g , :
Set o b j P r i c e l s N o O b j e c t = Nothing


, , ,
. VBA- Add,
, VBA-, .. ,
. ( , , - AddShape AddDocumer.t , .)

Add
, Add . , . ( ) .
, , L a y e r Visio. L a y e r
Visio. .. ,
. L a y e r L a y e r s , ( ) . , L a y e r s . Add.
, L a y e r s ,
VBA L a y e r . L a y e r s Page Document.
Laye r :
T h i s Document;. Pages (2 } . Layers . Add ( " ")
L a y e r Add,
. Add L a y e r L a y e r
, Layer.

298

III. VBA

, PowerPoint,
[ :
; v e P r e s e n t a t i o n . S l i d e s . A d d 1, p p L a y o u t T e x t A n d C l i p a r t
, - S._ides.


,
, Add,
.
. :
Dim ^ As Glide
Set ~ A c t i v e P r e s e n t a t i o n . S l i d e s .Add 1,
ppLayoutTextAndC]ipart

New CreateObject
. .
I ^ , .
?. / ActiveX (COM).
<: S , .
, New Sc j t, C r e a t e O b i e c t . ,

10 19.
Mev,- G r e a t eObj t\ .

Is
, , , ,
.
I s . True, , F a l s e ,
. ,
I s :
Dim o b j O b j e c t l As O b j e c t , o b j O b ] e c t 2 As Object
cbjG.jjezzl Is objOo^ecr2 Then
" ! "
Else
" ."
End I f
If

12. -

299

Is , , :
I f o b j O b i e c t 3 I s T h i s D o c u m e n t . P a g e s ( 2 ) . S h a p e s ( 3 ) Then
, Is ( ) .

ftaJo/fia &

nftoifuuuie
VBA , With Each . . . N e x t .

With
, , . , , .
, With .
,
, . :
With ^
.Name = " " ' Name
.DisplayName ' DisplayName
= .Area ' Area
intrioflTacoBKa = . R o t a t e { 6 0 ) ' R o t a t e ()
1

End With
, With. . .End With , , . ,
.
, With, . , ,
. . B l o c k AutoCAD. With
, Block.
.
With Block.Item{"")
' Block "" :
' Block Name
.Name = ""

300

///. VBA

'
Count
1^0 = .Count
1
AddCircle
' = 0 = 5
AddCircle (0#, 5#)
With .Item (1)
' With
1
Block
'
Move
.Move (15, 20)
' Color

.Color = 221
End With
End With
, ,
, . ,
With :
1^ = InputBox (" , " _
& " " & .Name)
If .Count > 12000
MsgBox " !"
End If

For Each...Next
-, VBA ( ,
) 8,
, . F o r . .Loop, VBA For E a c h . . .Next , . , , , For
E a c h . . . Next . :
For Each In
( )
Next
For Each. . .Next F o r . . .Next ,

VBA. For Each
, , .

For Each...Next
AutoCAD, B l o c k s
B l o c k B l o c k
Cone () 3DFace ( ).
B l o c k
B l o c k s :

12. -

301

Dim objB As Block


For Each objB In Blocks
D e b u g . P r i n t objB.Name
Next objB
, ,
. For E a c h . . .Next , , .

For Each...Next
, .

, For E a c h . . . N e x t I f . . .Then.
I f . . . Then , , E x i t For.
B l o c k s AutoCAD, :
Dim obi As Block
For Each objK In Blocks
If objK.Name = ""
objK.Delete
E x i t For
End If
Next objK
I f . . . Then , Block "" B l o c k B l o c k s .
Block ,
I f . . . Then. "" , ,
, Block. . , E x i t For , .

302

III. VBA

13

:

/3 & ...
>
>
>

C o l l e c t i o n

> ,

^ ,
9 V* , ,
, . ,
VBA . . .
C o l l e c t i o n
(), VBA, , .
. ,
, , . , .


, , , , - . ,
, -
. , , . :

214236
54 527 3
3714 53
891982
00000
9412 41

13.

303

, , , . ,
- , : " ". VBA
.


VBA , .. .
, , . ,
i n t L o t t o A r r a y { 3 ) ( , ) i n t L o t t o A r r a y . , , , i n t
,
. , , i n t L o t t o A r r a y ( 3 ) ,
.


.
| V . VBA
I
, , .
I V .
% D a t e ,
|
S t r i n g .
, . ,
V a r i a n t VBA ,
V a r i a n t . , , V a r i a n t , , ; .
.
, . . , , ( ), ().
, ,
" ").


, ,
, . , .
.
, .

304

///. VBA


12
2
21
4
9
3
11

. . 13.1 .

. 13.1.
,
VBA VBA
60.


, .
. :
' D a t e :
Dim datTimeOfImpact (cdatMaxObservations) As Date
' Currency :
P u b l i c c u r P r i c e Q u o t e s () As Currency
1
I n t e g e r :
Dim i n t A r r a y O f l n t e g e r s (34, 13, 29, 4) As I n t e g e r
, ,
, , , .
( , .)
,
As Type , . , VBA V a r i a n t , , . V a r i a n t , , ,
.

13. :

305

, , . -
. i n t A r r a y O f I n t e g e r s
35x14x30x5,
73500 .
, ,
(35 34 ..).
.


, (.. )
0; , 0. -
, ,
, . , 10 , 9.
, .
i n t M a c c M B (1) .
0, 1. VBA 1. ( )
Option Base I
,
;1.
. ;
Edn s n g N u m b e r s (55 7 5 , 7 16, 99)
' , 2 I , 55 75; 10
7, 100 0.


,

. ,
. ,
Edra ^ ( 9 , 19) As S t r i n g
, 30 20 (, ,
).
, , .
]1(. , 1 ^
, >
.

III. VBA


.
I S .
1 S , .
1 S
1
( ,
1
).
, . ,
Dim ^ () As Date
VBA D a t e ,
.


,
, . ReDim, :
ReDim ^ (1^ - 1)
, , , ,
. 1,
VBA 0, , , , ,
" ".
ReDim .
, ,
. , . , , ,
.


, ReDim P r e s e r v e . , , , :
Dim d b l G a l a c t i c M a s s e s () As Double
ReDim P r e s e r v e d b l G a l a c t i c M a s s e s

(1 To 30,

I To 50)

ReDim P r e s e r v e d b l G a l a c t i c M a s s e s

(1 To 30,

1 To 100)

P r e s e r v e

( ReDim P r e s e r v e
). , , , .

13. :

307


, ,
. . , s t r S a y .Lngs {4, 6 ) , , 4 6 .
, ,
, , . Dim s t r S a y i n g s ( 1 0 , 2 0 ) As S t r i n g , s t r S a y i n g s ( 4 , 6) . Dim s t r S a y i n g s ( 4 To 10, 50) As S t r i n g ,
.


. :
f" S ;
X
C u r r e n c y :
$
I
curBigDough(5,8,19) = 27.99
I */ , , , :
I
datThatDate = datTheseDates(25,10)
j S , :
|
i n t A = 35 * ( i n t B + i n t C o u n t s ( 3 , 2 ) )


, .
,
. :
Sub FortuneTeller()
Dim strTodaysFortune(1 To 10)
Dim intUserChoice As Integer
strTodaysFortune{1) = _
" 15 ."
strTodaysFortune(2) = _
" ."
strTodaysFortune(3] = " !"
strTodaysFortune(4) = _
" , ."
strTodaysFortune(5) = __
" ."
strTodaysFortune(6) = " ."
strTodaysFortune(7) = " ."
strTodaysFortune(8) = _
" ."

308

HI. VBA

strTodaysFortune(9) = _
" . "
strTodaysFortune(lO) = _
" ."
i n t U s e r C h o i c e = InputBox(" ," _
& " 1 10")
MsgBox ( s t r T o d a y s F o r t u n e ( i n t U s e r C h o i c e ) )
, , , I n p u t Box. 10.


, , Ubound, , . UBound :
UBound '_,)
_, , , ,
, VBA, . , UBound .

...
.
. ,
For . . . Next, . , ,
.
, 1.
:
Const S i z e As I n t e g e r = 3
(1 To S i z e , l To S i z e , l To Size) As Double
D i m dblMatrix
Sub A r r a y F i l l e r ( )
Dim I As I n t e g e r , J As I n t e g e r , As I n t e g e r , X As I n t e g e r
X = 1
For I = 1 To S i z e
For J = 1 To S i z e
For = 1 To S i z e
d b l M a t r i x f l , J, K) = X
X = X + 1
Next
Next J
Next I
End Sub

13. :

309

. , ,
.

, . . , , .
. 13.2.

. 13.2. ;

, ,
" i b i M a t r i x ( I , J, ) = X, I, J ,
. . 13.2
, .
, -'jr. . . N^xL. I, J , , .
, VBA , . . 13.3 .
, VBA I . ,
J. J
. I , . . I (. 13.4).
, , . ,
1, . ( , ):

310

///. VBA

. 13.3. , -
For. . .Next:
For = 1 Size
For J = 1 To Size
For I = 1 To Size
dblMatrix(I, u, K) = X
X = X + 1
Next I
Next J
Next
, . 13.5.

13. :

311

. 13.4. ,
Fo . . . Next

4
5
6

7
8
9

\
\

\\

. 13.5.
, ,
.

312

III. VBA


VBA 6
strMaccHBKonMH = str-
(, ) ,
VBA . , .
VBA 5 , :
'
'
intPa3MepMaccMBa = UBound(strMcxon.HbinMaccMB)
Re Dim !: (intPa3MepMaccHBa)
'
For I = 0 To intPa3MepMaccMBa
^ (I) = 1: (I)
Next I

ttaJofwMU
Collection
,
C o l l e c t i o n (). 12, VBA
C o l l e c t i o n , . C o l l e c t i o n . , VBA .
,
. ,
.


VBA,
C o l l e c t i o n
, . Add Remove
, , ReDim . ( , .)
, ,
.
, , , , 100,

13. : 313

ii'j
V a r i a n t , V a r i a n t , . .
C o l l e c t i o n , .. (. " "). , . ,
VBA , , ,
Microsoft Jet (DAO) ODBC, ,
.

Collection
; l e c t i o n , , As . ,
\ .
1 S e t ,
:
. S e t
New, . :
Dim colMixedBag As C o l l e c t i o n
Set colMixedBag = New C o l l e c t i o n
'
colMixedBag.Add "Howard, E t h e l "
1

VBA . New ;
Dim colSetOfStuff As New Collection
'
1

colSetOfStuff.Add intStuffing


Add ()
, VBA- (, 12). Add .
Add :

Add ([, ] [, before ] [, a f t e r ])


, ,
, , , , , , VBA. Add , .

314

III. VBA


ii -, , .
R :
c ^ L F n : .-.ncia I s - Aoj 14 32 3 . 44,

" "

c c l F i n a n c i a l s 1 4 3 2 3 . 4 4 . . S t r i n g .
, , , , . Add Remove
. 69 29-,
,
.
Add.
), ,
R e n o v e Add .


.
, -- ' Add , ,
b e f o r e (.) a f t e r ().
, 35- .
JTOTO :
] :1! j n d i s . A d d

r,i i S p e c i e s ,

b e f L-re

35

35- , ,
( - , I.) VBA,
, . , :
10;. Add = tstiCopT, af*:.ej. ""
VBA
.
, , Add, b e f o r e ,
a f t e r , .



F o r . . . N e x t , , . :

Duu X ? s i n t e g e r , Y As I n t e g e r
- 12
For X - I To 30
cclHcuseOfValues.Add Y * X
Next X

13. :

315


, , Remove . , , :
1.Remove 2123
1.Remove ""
, , VBA, , " "
, , 1.


, Add Remove. C o l l e c t i o n Count
(), , , . , , :
^ = 12 0.Count
, :
If colPrices.Count > 1000
MsgBox " !"
End If
, Count For . . . Next,
, :
Dim Z As I n t e g e r
For Z = 1 To c o l P r i c e s . C o u n t
MsgBox " " & c o l P r i c e s ( Z )
Next Z
F o r E a c h . . . Next (. 8).


, ( ). , :
- 1(" -")
, VBA . ,
, .
. :
c o l l n v e n t o r y ( 1 4 6 5 ) = 119
, .
D i c t i o n a r y (), 14.

316

///. VBA


, C o l l e c t i o n VBA ,
.
, .
F o r . . . N e x t
. , , . :
Dim I As I n t e g e r , strName As S t r i n g
Dim s t r P h o n e As S t r i n g , s t r A d d r e s s As S t r i n g
For I = 1 To T o t a l ' I
'
strName = InputBox(" ")
s t r P h o n e = InputBox(" _
")
s t r A d d r e s s = InputBox(" ")
1
" "
c o l D a t a b a s e . A d d strName, "" & I
c o l D a t a b a s e . A d d s t r P h o n e , " " & I
c o l D a t a b a s e . A d d s t r A d d r e s s , "" & I
Next I
, , :
MsgBox c o l D a t a b a s e ( " 2 " )
,
.
(
14). , , .
, . , . ( , , 14.)

, , ,
.
. VBA-
.

13. :

317


(, 13.6}, -
, , , - .

Name: Says, Fran

Name: Best, France's

Name: Smith, Francis

Name: Jones, Frances

Phone: (209) 555-3366

Phone:(612)555-9090

Position: Middle manager

Position: Database analyst

Frame of mind: Distracted

Frame of mind: Ebullient

. 13.6. ,
VBA
, . ,
. (, , - ). , .


VBA , ,
.
, , .

.
, .


, :

intHov.ep As Integer '
str As S t r i n g
strMMH As S t r i n g
strAflpec As S t r i n g
1 As Long
^ As Date
End Type
.
, , ,
. ( ,

318

III. VBA

.)
,
, .
, i y p e
, .
!, .
: ( ). .


,
, .
- .
P.i ' j l i , P r i v a t e S t a t i c .
Dim:
D .. i.i s ? v.
, .
D LIT. , .
,
:
P r i v a t e ( I 2) As

,

"" . : , .
,
. :
. s ^ . . ^ - "."
W i t h
. :
"With

. str^^i^^y.nv^ = ""
. ?, t = " "
. . ^ ^ - #1?/9/48#
End Wi-n
. :
:; ( int.4or.iep; . str- - ""

13. :

319


,
, , . , ,
(2) =
, . ,
:
Sub WriteDataf)
Open ":\Database\CoTpyflHMKM.dat" For Binary As #1
For i = 1 To intPa3MepEa3biflaHHbix '
Put # 1 , , (i)
Next i
Close #1 ' -
End Sub
18.

320

///. VBA

IV


cVBA

"
.
."

atfou &...
sT^f'B' 14no 16 VBA \ ^\
Microsoft Office. 14 !
, ", \
" -f ; r # % T Office, . ~
' |
+' *
- Office, , ' ^
~- ' ', . *
15 16 I
1
." . Word u.Excel '
i~l VBA-.
*
*'?' - -
\
_\" '
VBA.
]

,
-, , ]
.
\
17 \
VBA. 18 , \
-
!
, - , \
20, , 19
'
-.

14

VBA Office
/3 & ..,
>

VBA

>

Office

>

1 , . , Office.

/6>/

Office ,
, ,
VBA . , , VBA , , , , .
Office : . VBA C o m m a n d B a r s . , CommandBar.
C o m m a n d B a r s . , VBA4Dummies:
Dim tbar As Toolbar
Set tbar = CommandBars("VBA4Dummies"}


V i s i b l e . VBA4 Dummies.
CommandBars{"VBA4Dummies").Visible = True
V i s i b l e
F a l s e .

14. Office

323


, , Not (. 11).
CommandBars("ReBar").Visible = _
Not ( C o m m a n d B a r s ( " R e B a r " ) . V i s i b l e )
, , P o s i t i o n , Rowlndex L e f t . P r o t e c t i o n , .
.

Position

,
, ,
,

m s o B a r F l o a t i n g (
),
msoBarTop, msoBarLeft,
msoBarRight

Rowlndex

0;
m s o B a r R o w F i s t
m s o B a r R o w L a s t (

)

Left

Protection

msoBarNoProtection,
msoBarNoCustoraize,
msoBarNoResize,
msoBarNoMove,
rasoBarNoChangeVisible,
msoBarNoChangeDock,
msoBarNoVerticalDock,
msoBarNoHorizonalDock


VBA ,
, drag-and-drop,
Office, .
,
VBA , .
, , , Word
. .
, VBA-, , , ,- E n a b l e d T r u e
F a l s e .

324

IV. VBA


, VBA-.
.
| S Controls |
, .
I
; ,
1
( ) , 1. :
CommandBars("BarNone").Controls(3)
, Office
, ,
, .
S C a p t i o n . , Office. ^,
.
, .
, C a p t i o n " " .

CommandBars{"BarNone").Controls(" ")
, .
^ F i n d C o n t r o l CommandBars
. . Tag. , ,
& , , .
CommandBars("BarNone").Controls(,,"TagOl")
, , F i n d C o n t r o l , ,
:
CommandBars.FindControl(, , "TagOl")
F i n d C o n t r o l ,
( , ) Tag.


, VBA,


( )

E n a b l e d

,

, ,

T r u e F a l s e

14. VBA Office

325


( )

V i s i b l e

True F a l s e

S t a t e

, ""

msoButtonUp
( );
msoButtoriDown (

);
m s o B u t t o n M i x e d (
"")

CopyFace


. ,
,
, ,
.
.

"" ,
C o p y F a c e
.
P a s t e F a c e

P a s t e F a c e



,
- .
,
. , ^, , .
. ,
VBA, , T o o l t i p T e x t . Excel:
Sub ___ ! )
CommandBars("Mr. G o o d B a r " ) . C o n t r o l s ( 2 ) . T o o l T i p T e x t = _
" : , , "
End Sub
. Office
, ,
.

326

IV. VBA

JIftazfiaMUUifto6aHdie Office
Microsoft Office Office

. , VBA A s s i s t a n t .


A s s i s t a n t (. 14.1) ,
Office . .
, , On T r u e , .
14.1. Assistant

On

T r u e F a l s e
no Office.


Visible

F i l eKame

,

: , ..


. a c s ,

Animation


.
,

,
,
m s o A n i m a t i o n G e t A r s t y . M s o A n i m a t i o n T y p e Office

T r u e F a l s e


,
. Office , VBA ,
, ..


. , NewBaloon ,
A s s i s t a n t . , S e t
, , , .

14. Office

327

Dim blnUpUpAndAway As Baloon


Set blnUpUpAndAway = A s s i s t a n t . N e w B a l o o n
b a l l o o n . 14.2
. Show
.
14.2. Balloon

Mode

,
,

m s o M o d e M o d e l e s s (
,
}; m s o M o d e M o d a l
( );
rasoModeAutoDown
(
)

BalloonType

,
8

msoBalloonTypeButtons;
msoBalloonTypeNumbers;
msoBalloonTypeBullets

Icon

msoIconNone; msoIconTip;
msoIconAlert

Heading

Text

"" ,

,
,

Labels

CheckBoxes
Button


MsoButtonSetType,


,
.
I c o n , , . 14.2. ,
H e a d i n g Text , , :
blnUpUpAndAway.Text = "VBA! {bmp c:\dummies.bmp)"
,
328

IV. VB

.
bmp . bmp wmf
.wmf. .


,
. (
Mode msoModeModal, ),
. , C l o s e .


VBA, .
: , ( ) ( , ).

. ,
,
. T e x t
, :
With blnUpUpAndAway
.Labels(2) =
" ."
.Checkboxes(3).Text = " ."
End With
, ,
, B a l l o o n T y p e
m s o B a l l o o n T y p e B u t t o n s .
(. . 14.2).

, B u t t o n
, m s o B u t t o n S e t T y p e . , , : Abort (), Retry
() Ignore (), :
blnUpUpAndAway.Button = m s o B u t t o n S e t A b o r t R e t r y l g n o r e

,
. : ,
, ,
Show: , . , :
i n t B u t t o n = blnUpUpAndAway.Show
S e l e c t Case blnUpUpAndAway.Show
14. VBA Office

329

Case msoBalloonButtonOK
(, )
Case msoBalloonButtonCancel
(, Cancel)
Case 2
(, ]
End S e l e c t
Case, O f f i c e
, ""
(. . , ). , .
, Case.

Checked . If ... Then, , :
With blnUpUpAway
If .Checkboxes(1).Checked Then
(, , 1 )
Else
(, , 1 )
End If
If .Checkboxes(2}.Checked Then
(, , 2 )
Else
(, , 2 )
End If
End With
C a l l b a c k B a l l o o n
, .
b a l l o o n ,
, .

//
quace
, . ,
: -. , ,
Excel . Excel
, -
Excel.
Office Word
( V a r i a b l e ) . .

330

IV. VBA

Excel,
PowerPoint Project

Office. VBA
, , . , ,
.
Excel, PowerPoint Project ( Word
) Add C u s t o m D o c u r a e n t P r o p e r t i e s .
Excel, A c t i v e W o r k b o o k
( Word PowerPoint A c t i v e D o c u m e n t Act i v e P r e s e n t a t i o n ):
ActiveWorkbook.CustomDocumentProperties.Add_
Name:= " B u t t o n C o u n t " , L i n k T o C o n t e n t : = F a l s e , _
Type:= msoPropertyTypeNumber, Value:=0
L i n k T o C o n t e n t F a l s e , (
True, , Excel Word). ; , m s o P r o p e r tyTypeNum.be, m s o P r o p e r t y T y p e B o o l e a n , m s o P r o p e r t y T y p e D a t e , msoP r o p e r t y T y p e F l o a t m s o P r o p e r t y T y p e S t r i n g .
, , Value P r o p e r t y . ,
, B u t t o n C o u n t ,
.
:
With ActiveWorkbook.__
CustomDocumentProperties("ButtonCount")
.Value = .Value + 1
End With
.
, , ,
. , c i n t C u t O f fValue , .
With
ActiveWorkbook.CustomDocumentProperties("ButtonCount")
'
intCurrentCount = .Value
'
If intCurrentCount > cintCutOffValue Then
MsgBox " " _
& " " & cintCutOffValue _
& " ."

14. VBA Office

331

End If
^
.Value == intCurrentCount + 1
End With
intCurrentCount = ActiveWorkbook
_.CustomDocumentProperties("ButtonCount").Value


Access Excel , VBA, (Access) (Excel). , , ,
, .

, VBA. Window
( ), S a v e S e t t i n g G e t S e t t i n g ,
. 20.

332

IV. VBA

15

VBA Word
s/ftou ...
> Word
"> Word: , , F i n d
> :
>
> VBA Word

j [ Office Word
? . Word, Range
F i n d , , , VBA-, Word. .
Word , ,
. , , ,
. Word
VBA. , .

& dfifilication
VBA-, Word
A p p l i c a t i o n . ,
Word. A p p l i c a t i o n
VBA Word, . , , , . ,
L i s t Commands A p p l i c a t i o n :
A p p l i c a t i o n . L i s t C o m m a n d s (True)
, L i s t C o m m a n d s
, Word.
L i s t C o m m a n d s T r u e ,
Word. False, .

15. VBA Word

333

qotcifMeHJiuui Word


VBA- ,
, , S e l e c t i o n , . .


VBA- Word
, .
A c t i v e D o c u m e n t . , :
ActiveDocument.Close
, , : A c t i v e D o c u m e n t .


, ,
Documents, ,
Word.
VBA, , ,
( , ). ;
Documents("Toy S t o r e N e w s l e t t e r . d o c " )
, , ,
. , , : Documents ( s t r D o c N a m e ) .
. , , Documents:
Documents(3)
, ,
, .
. ,
:
strDocName = Documents(2).Name

, ,
Add
Documents. - , Add , Normal ().
, :

334

IV. VBA

Documents.Add template:= _
"C:\Windows\Application DataXMicrosoft\Hidden templates"
Open
Documents. , , :
Documents.Open FileName:= __
":\Toys\Toys for i n f a n t s . d o c "
A c t i v a t e
Documents. , , VBA- ,
, . , , , :
Sub DocActivateOrOpen()
Dim docFileName As S t r i n g , docPath as S t r i n g
docFileName = " .doc"
docPath - " C : \ T o y s \ "
For Each t a r g e t D o c In Documents
If targetDoc.Name = docFileName Then
targetDocIsOpen = True
End I f
Next targetDoc
If targetDocIsOpen = True Then
Documents(docFileName).Activate
Else
Documents.Open FileName := docPath & docFileName
End If
End Sub


Word , , Word VBA S e c t i o n s S e c t i o n
. .
S e c t i o n ( Heade r F o o t e r ) . , Add S e c t i o n s I n s e r t B r e a k Range S e l e c t i o n .

, Word , . . Word App l i c a t i o n Windows, . , Document


Windows.
Window Word , S e l e c t i o n .
S e l e c t i o n ;
, ,
, .

15. VBA Word

335


, .
ActiveWindow.

Windows. Windows
A p p l i c a t i o n . Windows , . . , ,
, , , .
Window.

Windows ( "Document 4" )

,
Document 4

Windows ( " K i t e s and s k i p r o p e s . d o c : 3")


Documents f "Window
d i s p l a y . d o c " ) .Windows (2)


Windows


^,
. Word , . Word
, , .
, . Panes
. ,
( , ).


Window ,
, .
T r u e F a l s e . ,
, :
ActiveWindow.DocumentMap = True
,
D i s p l a y S c r e e n T i p s D i s p l a y V e r t i c a l S c r o l l B a r . He
, Not
. :
ActiveWindow.DisplayRules = _
Not A c t i v e W i n d o w . D i s p l a y R u l e s
L e f t , Top, H e i g h t Width , .

336

IV, VBA

View
View . View .
View

Fullscreen

, ,
, . .
:
wdMasterView,wdNormalView,wdOutlineView,
wdPrintView, wdWebView wdPrintPreview. ,
ActiveWindow. View. Type = wdPrintView

TableGridlines

S h o w A l l , Show

S h o w A l l ,

.
, ,
, ,
Show, ShowBookmar ks
S h o w H i g h l i g h t



, Zoom,
P e r c e n t a g e . :
ActiveWindow.View.Zoom.Percentage = 135
,
, View
, :
ActiveWindow.View.Zoom.Percentage = 7 5
,
.
, P a g e F i t Zoom . Word ,
. ,
:
ActiveWindow.View.Zoom.PageFit = w d P a g e F i t B e s t F i t
, :

15. VBA Word

337

Windows("Documentl").View.Zoom.PageFit = _
wdPageFitFullPage
P a g e F i t w d P a g e F i t N o n e .

Selection
Word VBA S e l e c t i o n .
-, S e l e c t i o n , . , , . ( S e l e c t i o n , , ,
).
VBA-, S e l e c t i o n , Range.
" Word
VBA" .
, , , - , . : , S e l e c t i o n .
,
, . S e l e c t i o n . , , :
S e l e c t i o n . T e x t = " "
,
. :
documents("Songs.doc").Windows(2).Selection.Text = _
" "
S e l e c t i o n , , ,
- . .
S e l e c t i o n . , , , :
With S e l e c t i o n
If -Type = wdSelectionNormal Then
.Cut
End I f
, .

338

IV. VBA

wdNoEelection

wdS 1 ct i o n 3 1

wdSelectionColurr.n

wdSe I e ct i on Fr arae

wdSelectionlnlineShape

wdS e I t i n IP

wdSelectionNormal

wdSelectionRow

wdSelectionShape


,
, ,
. Word Range . Range . Range
, . Range ,
VBA, Word,
, S e l e c t i o n .
Range :
\ / Range;
% S Range Document.

Range
Word Range,
. , , ,
( ) . , , Range . , Range, ,
.
ActiveDocumep.t. P a r a g r a p h s (1) . Range
Word, , - . ,
. ,
, Range:
ActiveDocument.Tables[2)
.Range.Copy

15. VBA Word

339

,
With... , .
, ,
:
With A c t i v e D o c u m e n t . S e c t i o n ( 3 ) . R a n g e
. S o r t S o r t O r d e r := wdSortOrderAscending
. S e n t e n c e s ( 1 ) . R a n g e . B o l d = True
E'.nd With
, Range
, .
,
, .
, ,
, ; ,
Range.
,
, .
, .
S e l e c t i o n Range. , Range .
, , :
Set deRange = S e l e c t i o n . R a n g e
S e l e c t i o n . M o v e Unit := wdParagraph, Count := 3
deRange-Case = wdLowerCase

Range
, ,
Range.
Range, . Range ,
. , :
A c t i v e D o c u m e n t . R a n g e ( S t a r t : = 10, End:=2 0)
, 11- 20- . . , , 0, , 10 10- 31- . Word
, .
,
, , ,
. Range Range - C o n t e n t .

340

IV. VBA

Range ( , ).
, . .
, ,
.
, , S t a r t End S e l e c t i o n , Range Bookmark, . , F o r g e t MeNot, ;
With ActiveDocument
Set myEkMark = .Bookmarks{"ForgretMeNot")
Set horr.eOnTheRange = _
. F a n g e ( S t a r t := myBkMark, End := myBkMark + 10)
End With
, Range
, , , . End , :
With ActiveDocument
Set fistWord = . P a r a g r a p h s ( 1 6 0 ) . R a n g e . W o r d s ( 3 )
Set RangeTop = . R a n g e ( S t a r t := f i r s t W o r d . S t a r t )
End With
" VBA Word", ,
, F i n d
, .
F i n d , S t a r t
End
.

Word
Range S e l e c t i o n
, Word VBA.
,
, .
Range S e l e c t i o n , . , .
. , . , S e l e c t i o n : , , Range .

15. VBA Word

341

S e l e c t i o n ,
, , ,
, , , .
Range.
: Word ,
. , , .



, S e l e c t i o n Range . , . ,
.
.
| S S e l e c t . ,
*
RangeR RangeR. S e l e c t .
i S , , f
, Range.
: , , , ,
S e l e c t i o n . Range . .


Word VBA .
; .


Expand , . , , . ; ,
.
, , :
Selection.Expand(wdWord)
;
w d C h a r c t e r , wdWord, w d S e n t e n c e , w d P a r a g r a p h , w d S e c t i o n , w d S t o r y ,
wdCell, wdColumn, wdRow, wdTable ( S e l e c t i o n ) wdLine.
wdWord.

342

IV. VBA

: S e l e c t i o n
( ) Expand.
Word, . , E x t e n d , , : , , , .
, , S e l e c t i o n . Expand ( " " ). .


Word VBA
. , , Move, : , .
Move , ,
, . . Move "" . Expand
Mover,nd .

. , U n i t (
" " ). Count
,
(.. ), , . , ( )
:
oTheRange .Move Unit : = waParagraph, Count, : -= -2
MoveStart. MoveEnd , Move,
.
:
Select-on.KoveStart Unit : = wdWord, Count := 3
, ,
Word .
, S t a r t O f EndOf,
. S t a r t O f , EndOt .
E x t e n d Word. ,
, . wdMove wd~:-:i - .
:
S e l e c t . - o n . S t a r t O f Unit :=- wdSentence, Extend := wcMove

15. VBA Word

343


- ,
. , . ,
,
, . (. , , "" ).
C o l l a p s e .
, D i r e c t i o n . :
Selection.Collapse
:
Selection.Collapse(Direction:=wdCollapseEnd)
, ,
( wdCollapseEnd), Word
( , ). - ,
MoveEnd, , :
.MoveEnd Unit := wdCharacter, Count := -1

,
: D e l e t e . Cut,
. , , .
, ,
, P a s t e . , ,
Word.

, .
T e x t F o r r a a t t e d T e x t . , ,
, . , .
, ( ).
; :
With ActiveDocument.Bookmarks("TheBookmark")
Set RangeY = _
ActiveDocument.Range(Start:=.Start, End=.Start)
Fnd With
RangeY. Text == Selection .Text
T e x t
F o r m a t t e d T e x t .

344

IV. VBA



T e x t , .
, :
Range2.Text = ", ! - ! "
, T e x t . (
), .
I n s e r t B e f o r e I n s e r t A f t e r Range S e l e c t i o n ,
.
. Word .
,
. ,
D a i r y E n t r y ( VBA, vbCr, ). , . , , , .
Dim s t r l n s e r t T e x t As S t r i n g
S e l e c t i o n . I n s e r t B e f o r e " D a i r y E n t r y " & vbCr
s t r l n s e r t T e x t = "Today" & Chr(146) & "s d a t e i s "
strlnsertText =
strlnsertTex't & Format (Now, "Long date") & ". "
Selection.InsertAfter strlnsertText & vbCr
,
VBA, , VBA. VBA 11.

( vbCr) T e x t
I n s e r t B e f o r e I n s e r t A f t e r . Add,
P a r a g r a p h s , , .
,
, .



. Word .

Font

...
,
Name, S i s e B o l d . Range,
F o n t ,

15. VBA Word

345

...
Paragrap

,
LeftIndent LineSpacing

hFormat
Style

3orders
Tabs t o p s


.
P a r a g r a p h ,

tftetcana
, F i n d Word VBA.
F i n d . F i n d .
1. F i n d
. , C o n t e n t Document
, :
ActiveDocument. Content:. Find
2. F i n d ,
.
3. E x e c u t e F i n d .
:
With OpenRange.Find
. ClearFormatting
.Text = "pogo s t i c k s "
.Execute
End With
, , F i n d
, ,
Word.
C l e a r Format t i n g

.


E x e c u t e
. , , . Found F i n d
If...Then, :

346

IV. VBA

If .Found = True Then


( }
Else
( )
End If
E x e c u t e ,
,
. , ,
. , ,
. P a r e n t . I t a l i c = T r u e F i n d , .. O p e n R a n g e .
O p e n R a n g e ,
:

With OpenRange.Find
. learFormatting
.Text = "pogo s t i c k s "
If .Found = True Then
. P a r e n t . I t a l i c = True
Else
MsgBox "No pogo s t i c k s found."
End If
End with


R e o l a c e m e n t ( , )
F i n d .
R e p l a c e m e n t .
p o g o s t i c k s
s k a t e b o a r d s . , F i n d S e l e c t i o n :

With ActiveDocument.Content.Find
.ClearFormatting
.Text = "pogo s t i c k s "
With .Replacement
.ClearFormatting
.Text == "skateboards"
End With
.Execute Replace := wdReplaceAll
End With
, E x e c u t e R e p l a c e ,
, , .

15, VBA Word

347



Find, . ,
,
" ", . R e p l a c e m e n t , .

F i n d , T e x t ,
. ,
T e x t R e p l a c e m e n t .
,
Drab, F r i l l y :
With Selection.Find
.ClearFormatting
.Style = "Drab"
.Text = ""
With .Replacement
ClearFormatting
.Style = "Drilly"
.Text = ""
End With
.Execute Replace := wdReplaceAll
.ClearFormatting
.Replacement.ClearFormatting
End With
" " E x e c u t e . ,
, .

Office, Word
, . .
V a r i a b l e s
. ,
. Henry
F r i e n d O f A n a i s ;
FriendOfAnais = _
ActiveDocument.Variable("Henry").Value
Add V a r i a b l e s , :
Documents("Documentl").Variables.Add _
Name := "TimeThisMacroHasRun", Value := 0

348

IV. VBA

, , , . , ; , . :
For Each DocVar In A c t i v e D o c u m e n t . V a r i a b l e s
If DocVar.Name = " L a s t C a p t i o n " _
Then Doclndex = DocVar.Index
Next DocVar
If DocEndex = 0 Then
A c t i v e D o c u m e n t . V a r i a b l e s . A d d __
Name := " L a s t C a p t i o n " , Value := 1
CaptionCounter = 1
Else
CaptionCounter = _
ActiveDocument.Variables(Doclndex).Value
End I f
, Office
,
.
14.

15. VBA Word

349

16

VBA- Excel
...
> Excel
>
>
> Excel VBA-
> Excel

^ , - ,
w ; VBA, - . , Visual Basic ,
VBA-, , , ; VBA , .
, VBA
, , , . Excel, ,
VBA, .

Excel
VBA- Excel, Excel . .
A p p l i c a t i o n (), :ia Excel.
VBA-
. Excel ,
VBA-. . Excel S c r e e n U p d a t i n g ( ) A p p l i c a t i o n :
A p p l i c a t i o n . ScreenUpriatirig -- F a l s e
He True S c r e e n U p d a t i n g , .> .
S c r e e r . U p c a t i n g A p p l i c a t i o n . A p p l i c a t i o n
. , Acr : v e S h e e t A p p l i c a t i o n -

350

IV. VBA

, (, , ). A p p l i c a t i o n . A c t i v e S h e e t A c t i v e S h e e t . Workb o o k s A p p l i c a t i o n , .
, . :
Workbooks(" . x l s " ) - A c t i v a t e
, . W o r k s h e e t s , . W o r k s h e e t s ( " " ) . , C h a r t , , C h a r t s .
C h a r t s ( " P a r t s C h a r t " ) . , , ,
.
Window (). Window, , Windows A p p l i c a t i o n .
Windows , :
Windows(" . x l s " )
,
, : Windows ("
.xls:2").

tcoqe Range

qua
, Excel C e l l (). : VBA- Range. Excel Range
.
Range Excel Word,
. Word, VBA-
Range. Excel, Word, , -
.

Range
Excel
, . .
. 1- ,
, Range.
Range, :

16. VBA- Excel

351

ActiveSheet.Range("B3")
Worksheets("Sheet2").Range("M5:S20")
. , VBA Range , :
Worksheets(" " ) . R a n g e ( " ")

Name ():
Range("A3:4")-Name = "-"
. Range, Excel
Range 1 - . , :
ActiveSheet["Al:Z26"]
[" "]
Cells Worksheets. , , .
. "
Cells " .
Selection (). , , S e l e c t i o n .
" " .
ActiveCell ( ). A c t i v e C e l l , .
( A p p l i c a t i o n ) , A c t i v e C e l l :
ValueStorageBin = ActiveCell.Value
Rows () Columns () Worksheet. , ,
Rows Columns (
).
, , .. :
Workbooks("IOU.xls").Worksheets("Sheet Shootout").Column(5)
. , , .
, .
RanGer, ,
, RanGer . V a l u e :
Dim RanGer As Range
Set RanGer = W o r k s h e e t s ( " 1 " ) . R a n g e ( " B 1 2 : H 1 3 " )

Cells
C e l l s W o r k s h e e t s
, . , C e l l s

352

IV. VBA

A p p l i c a t i o n ( A p p l i c a t i o n . C e l l s ) , ( C e l l s ,
A p p l i c a t i o n ) .
,
C e l l s ( ). , :
Worksheets{" " ) . C e l l s ( 3 , 5 )
, , , 1-.
, .. . ,
, . , .
( ) , , ..
:
intMon-h = Month (Now )
aGoal ;= Worksheets("Monthly P r o j e c t i o n s " ) . C e l l s ( i n t M o n t h ,

8)

,
:
Range(Cells(3,5), Cells(4,6))
2x2
, ( 3, 5) F4
.

Range C e i l s .
, , :
Worksheets("2"). _ Range(Worksheets("2").Cells(3,5),
Worksheets("2">.Cells(4,6))
With .
:
With Worksheets("2")
. R a n g e ( . C e l l s ( 3 , 5 ) , . C e l l s ( 4 , 6 ) ) . F o n t . B o l d = True
End With
, C e l l s . C e l l s ,
Range.


,
. :
W o r k s h e e t s ( " 1 " ) . R a n g e ( " B 1 2 : H 1 3 " ) . F o n t . S i z e = 14
, With, , :

16. VBA- Excel

353

with someRange '


.Value = 20 ' 20
.Font.Name = "Garamond"
. Font.Italic = True
.Locked = True
someRange = .Name '
With


, , Excel ,
.
someRange . V a l u e = someRange . Value + 10 . For
E a c h . . , Next. ,
. :
" o r Each a C e l l I n S e l e c t i o n
a C e l l . V a l u e = a C e l l . V a l u e + 10
Next
,
, . , ,
. For Each . . . Next:
For Each aCell In Worksheets("2").Range("A5:BIO")
If IsNumeric(aCell) Then
Select Case aCell
Case 5 to 10
aCell.Font.Underline = xlUnderlineStyleSingle
Case 10 to 20
aCell.Font.Italic = True
Case Is > 20
aCell.Font.Bold = True
End Select}
End If
Next


,
. VBA ,
, , R a n g e . VBA , ; , ,
, - .


, Excel. -

354

IV. VBA

S e l e c t i o n () A p p l i c a t i o n Window. S e l e c t i o n A p p l i c a t i o n , , . :
A p p l i c a t i o n . S e l e c t i o n . V a l u e = 20
Selection.Value = 2 0
, ,
, , S e l e c t i o n . ;
, , :
Dim Se.LRange As Range
Set Se..Range = Windows(" .xls">.Selection
With SelRange
.CheckSpelling
.AutoFit
.Copy
End With


VBA- , , S e l e c t ()
, - . S e l e c t
Excel ,
C h a r t () (
VBA-). Shape () , , Range.
,
, S e l e c t Range,
:
With Worksheets(" ")
.Activate
.Range(" " ) . S e l e c t
End With
, S e l e c t W o r k s h e e t , , ,
, . , A c t i v a t e . A c t i v a t e S e l e c t ,
. ;
C h a r t s ( " " ) . S e l e c t
,
S e l e c t .


, A c t i v a t e () Range. , . :

16. VBA- Excel

355

Worksheets("").Activate
Range("A1:E7").Select
Range("C4").Activate
,
.


, , ,
. , -
, , ,
. VBA- TypeName ,
. I f . . .Then C a s e . . . S e l e c t ,
.
TypeName ( S e l e c t i o n ) S e . l e c t ( ) , . Case,
( 2 001), ( ),
, .
, :
With Selection
Select Case TypeName(Selection)
Case "Range"
.Value = 2001
Case "ChartArea"
.Interior.Colorlndex = 3 * 3 = bright red
Case "Nothing"
MsgBox " "
Case Else
MsgBox " !"
Znd Select
End With

, Excel , VBA . VBA , , .


, , , . ,
, ,
, .. , -

356

IV. VBA

.
( ) , .


Excel VBA-. 6. ,
End F u n c t i o n . ,
, . , , :
FunctiDn MemoryAvailable ()
M-smoryAvailable = A p p l i c a t i o n .MemoryFree
End Fu.icticn
,
Excel. , ,
, .
, :
F u n c t i o n CheckForvalue(aRange,Value)
For Each o b j C e l l In aRange
CheckForValue = F a l s e '
False
If o b j C e l l . V a l u e = Value Then
CheckForValue = True
E x i t For
End If
Next o b j C e l l
EndFur.ction
. - , T r u e ,
F a l s e .
.


, , ,
VBA, .. Sub.
6.
, , : .
. , , :
=MemoryAvailable()
,
, :
=MemoryAvailable() & " " "
= I F ( C h e c k F o r V a l u e ( 8 : 1 8 , 8 ) ) , " " , "
")

16. VBA- Excel

357


,
. ,
- , ,
,
,
.
^^ (
). .


,
( . ,
, ,
:
=.xls!(4:D6,9)
, , , VBA. Tools^References.
, Browse
is . , .
, ,
.


,
, , VBA- .
,
! , .
, .
, ,
Sub. VBA . , Range, :
Sub F x T e s t e r O
ReturnVal = C h e c k F o r V a l u e ( R a n g e ( " B 8 : B 1 3 " ) , R a n g e ( " C 8 " ) )
MsgBox ReturnVal
3nd Sub
, , Visual Basic . Excel . VBA , , , .

358

IV. VBA


, , . , ? C h e c k F o r V a l u e ( " ")
T r u e F a l s e :
F u n c t i o n CheckForVaiue2(aRange, Value)
For Each o b j C e l l In aRange
CheckForValue2 = " " 6 Value & __ " "
I;: o b j C e l l . Value = v a l u e Then
CheckForValue2 = " " & Value & _ "
" & o b j C e l l . A d d r e s s
E x i t For
End If
Next o b j C e l l
End F u n c t i o n
-
, 3, 57
SFS83;
3,57 .

scoqe [

, Sub,
Excel
. :
W o r k s h e e t F u n c t i o n s . , , .
, :
OnAverage = W o r k s h e e t F u n c t i o n . A v e r a g e ( R a n g e ( " : B 1 3 " ) )
Excel VBA. , VBA-, 11.

JlfwiftcuutuftoeciHue coJbttnuu excel


, Excel , Office.
, ,
. ,
, Excel, VBA-.

16. VBA- Excel

359


, . Excel : , , Excel . , , , . , ,
.
Excel , W o r k s h e e t Workbook App l i c a t i o n . , , ,
C h a n g e () W o r k s h e e t , S h e e t C h a n g e ( ) Workbook A p p l i c a t i o n .
, , , . .
IS
j;
I S

, .

, .

i S , I
.


Excel

VBA. 10 ,
.
1. .
2. ,
.
3. , , , .
4. .
,
(. 1 ). W o r k s h e e t , Workbook C h a r t , ,
View Code. ,
Options.
, , Excel
. , . , , ,
.

360

IV. VBA


, , , , Change, C a l c u l a t e Sel e c t ionChange
( Worksheet)


SheetCnange, S h e e t C a l c u l a t e S h e e t S e l e c t i o n C h a n g e ( Workbook A p p l i c a t i o n ) . , ,
Activate Deactivate.

Change SheetChange
C h a n g e S h e e t C h a n g e ,
. . ,
. , T a r g e t , ; , .
P r i v a t e Sub Worksheet_Change(ByVal T a r g e t As Range)
Fcr Each o C e l l In T a r g e t
11 Then
If oCell > 4 And oCell

With oCe 11.Font


.Bold - True
.
.Size
.Color - PGE 0, 255, 0)
End With
End If
V.r
xt oCell,
End Sub
=

, T a r g e t , ,
, , . Change
For Ea^h ... Next; ,
, .
, Change S h e e t C h a n g e , . ,
(
<F2>),
, ( <Enter>,
). , <Esc>
.

Calculate SheetCalculate
C a l c u l a t e W o r k s h e e t , C h a r t ,
Excel .

16. VBA- Excel

361

S h e e t C a l c u I a t e Workbook A p p l i c a t i o n
. , , , .. C h a n g e . , C a l c u l a t e , <F9>.
C a l c u l a t e S h e e t C a l c u l a
. ,
, ,
W o r k s h e e t _ C a I c u l a t e . ,
, ,
.

SelectionChange SheetSelectionChange
, Excel S e l e c t i o n C h a n g e w o r k s h e e t .
S h e e t S e l e c t i o n C h a n g e Workbook A p p l i c a t i o n .
. S e l e c t i o n Change
,
. , Sh :
P r i v a t e Sub Workbook J 3 n e e t S e l e c t icr/ihange (ByVal Sh As O b j e c t , _ ByVal T a r q e t As E x c e l . R a n g e ;
Sh.Range{"Ai") = A c t ; v e C e I I . A d d r e s s
A p p l i c a t i o n . S t a t u s B a r - Sh . Nriire & " : " j; T a r g e t - A d d r e s s
i.'.nd Sub
. , - : ,
:
P r i v a t e Sub Worksheet_SelectionChar.ge (ByVal T a r g e t As r'.xcel . P.ance)
If T a r g e t . A d d r e s s = "$3$2" Then
MsgBox " ! "
End If
n.nd Sub


Excel ,
,
ActiveX, . A c t i v a t e , Dea c t i v a t e C a l c u l a t e . , .
! S DragOver D r a g P l o t . ,
I
.

362

IV. VBA

MouseDown. MouseUp MouseMove. .


S e l e c t . - .
.
S e r i e s C h a n g e . ,
( , ).

16. VBA- Excel

363

17


/ftou ...

ActiveX
DAO
V SQL

^ , Access ,
" Office, VBA. , ,
VBA, Word CorelDraw, , ,
Internet.
VBA,
VBA-.

J[ftotftauuuiftoaHMe <$
//

, .
.


, , , . , , , , .
, Access . Access "" (front end),
, , , , .
(back end) .
Access Microsoft Jet.
Access ( . mdb) -

364

IV. VBA

Jet. Jet, , Visual


Basic, .
Jet . , , , .
Microsoft SQL Server, ,
Oracle

Informix.

SQL Server

SQL Server 2000 Desktop Engine, Access 2002. SQL Server , "" SQL Server, .
, ,
. Access VB -,
,
. Access , Word, CorelDraw VBA , .
SQLMVBA
SQL (Structured Query Language ) ( ,
, ). , Jet Oracle, SQL. S Q L , . SQL , .
SQL VBA ? VBA
SQL .
VBA : SQL .


VBA
. VBA
.
, , (, , ..) ,
, VBA.
, ,
( 12). ,
. , .
; Microsoft. ADO (ActiveX Data Objects ActiveX), , DAO (Data Access Objects ), , . . ,
dBase, CodeBase ( Sequiter Software).

17.

365

Microsofi
ADO. , , .
, ,
, . ADO SQL-, , Outlook, SQL .


ADO Office XP Office 2000, DAO Office, Office 97, VBA. , Web- Microsoft, , , .
,
VBA-, .
, T o o l s ^ References,
References (. 12), .


, ,
AvtiveX, , ,
.
, .

Access VBA-. , , Word, , ,
ActiveX. Microsoft Office Developer , Office, , VBA. ,
.


Office ,
. ODBC (Open DataBase Connectivity )
Microsoft, SQL. ODBC , C/C++, VBA.
, ODBC , ODBC-
.
OLE DB (OLE ) Microsoft , ODBC. , OLE DB

366

IV. VBA

,
.
, SQL , .. OLE DB,
ODBC.
OLE DB Provider, "" ADO. ADO OLE DB Provider,
ODBC- ADO.

///<5:
qoc/fa/
, Access , Microsoft Office, Access .
.
Access
. Access ,
, . , , . , , ,
.
: , ,
Access, ,
, , , VBA. Access, VBA- , ADO,
Access.


Access , ? ,
, .
| /
1
I
.,
I.
|
|

Access. VBA-, Access, , .


Access , , VBA . Access ,
, . .

3.
| S Office, Word Excel. |
-

17.

367

I
|
|
I
1
|

1
|
|
I

|
I

. , ,
, , Word Excel
.
ADO VBA- ,
.
, ,
Access , , Word ( Word Access).
VBA ,
.
, Office Developer
, . VBA-, Access.

f
IS
|

|
1
I
I
|
1
?
|

Visual Basic ( VBA). Visual Basic . Visual Basic ADO


, VBA, Visual Basic
, , Access. , Visual Basic, , VBA-, Word
Excel. -, , , Visual Basic, , , , VBA. -,
: VBA-,
.

Access
Access, Access VBA-. , Access, VBA-.
/ Access

VBA Visual Basic. , ?,


VBA-, .
| S
I
t
\
i

Access
VBA,
DoCmd. DoCmd , Access. , , , , ...
, , ,
.

Access, , DoCmd VBA.


: DoCmd Access.
VBA, Visual Basic.

368

IV. VBA

"" VBA,
DoCmd Access. , ,
Access VBA, VBA Access Show,
OpenForm DoCmd.

tcoqa

SQL- ,
ADO .
: C o n n e c t i o n , R e c o r d s e t Coirjnand; .


- , .
.
VBA 9.

ADO
ADO VBA-,
ADO.
Visual Basic Tools^References,
Microsoft ActiveX Data Objects 2.x Library (
2.5).



( ) .

Connect; i o n .

Jet Access
! Access ADO- Jet, C o n n e c t i o n
Jet, Access, Access . C o n n e c t i o n C u r r e n t P r o j e c t Access. , , :
Dim conADOConnection As C o n n e c t i o n
Set conADOConnection = C u r r e n t P r o j e c t . C o n n e c t i o n

17.

369

, SQL Server
Access, VBA . B a s e C o n n e c t i o n S t r i n g C u r r e n t P r o j e c t ,
:
Dim conADO New Connection
conADO.Cor.nectionString = __
CurrentProject.BaseConnectionString

Connection
C o n n e c t i o n .
C o n n e c t i o n , . Open
, ,
OLE DB Provider , .
, C o n n e c t i o n , , Open. , C o n n e c t i o n Jet:
1

1
Dim. conADOConnection As New Connection, strConnect As String
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=C:\Data\Toys"
conADOConnection.Open strConnect

2
Dim conADOConnection As New Connection
With conADOConnection
.Provider^ "Microsoft.Jet.OLEDB.4.0;"
.Properties{"Data Source") - "=C:\Data\Toys"
. Open
End With

, Open,
OLE DB Provider; , Web- Microsoft Office Developer . SQL Server:
Dim conADOConnection As New C o n n e c t i o n
Dim s t r C o n n e c t As S t r i n g
s t r C o n n e c t = "Provider=SQLOLEDB; Data S o u r c e * H e c a t e ; " __
& " I n i t i a l C a t a l o g = t o y s ; User ID = s a ; Password = ; "
conADOConnection.Open s t r C o n n e c t
Access, ADO . Access
SQL Server, OLE DB Provider. , Access VBA SQL Server
, . Access
v i d - r MSDataShape, DataProvider
SQL0LILD3.

3 70

IV. VBA


ADO ,
C o n n e c t i o n : ,
. R e c o r d s e t Command. C o n n e c t i o n .

Recordset
R e c o r d s e t : . R e c o r d s e t , , . ,
R e c o r d s e t .

Recordset
R e c o r d s e t , . ,
"". &
:
| / Open R e c o r d s e t ;
s S E x e c u t e Command;
I S E x e c u t e C o n n e c t i o n .
Recordset Open
R e c o r d s e t Open
. Open ,
S e l e c t . R e c o r d s e t Open:
Dim coriman As New C o n n e c t i o n
Dim rstMan As R e c o r d s e t
Dim strSQL As S t r i n g
...( ,
)
strSQL = "SELECT * FROM Toys" ' Toys
Set r s t M a n . A c t i v e C o n n e c t i o n = conman
rstMan.Open s t r S Q L , , adOpenForwardOnly, adLockReadOnly, _
adCmdText
,
R e c o r d s e t A c t i v e C o n n e c t i o n . , , , ,
Open.
Recordset Command
SELECT SQL . / R e c o r d -

17.

371

s e t (). , Command
R e c o r d s e t .
Command, A c t i v e C o n n e c t i o n
, .
R e c o r d s e t .
. R e c o r d s e t , E x e c u t e Command.
:
Dim conTest As New Connection
l')im cmdTest As New Command
Dim r s t T e s t As R e c o r d s e t
Dim strSQL As S t r i n g
...(, c o n t e s t
... strSQL)
' Command:
With cmdTest
Set . A c t i v e C o n n e c t i o n = c o n t e s t
.CommandText = strSQL
.CommandType = adcmdText
End With
r s t T e s t . C u r s o r T y p e = adOpenForwardOnly
r s t T e s t . l o c k T y p e = adLockReadOnly
Set r s t T e s t = c m d T e s t . E x e c u t e ( )

Recordset Connection
R e c o r d s e t
E x e c u t e C o n n e c t i o n . ,
C o n n e c t i o n , , . ,
,
SQL. :
Dim conVert As New Connection
Dim r s t V e r t As R e c o r d s e t
Dim strSQL As S t r i n g
...(, conVert
... strSQL)
r s t V e r t . C u r s o r T y p e = adOpenForwardOnly
r s t V e r t . l o c k T y p e = adLockReadOnly
Set r s t V e r t - c o n V e r t . E x e c u t e ( ]


,
R e c o r d s e t , .

Open R e c o r d s e t , :
Dim r s t l n P e a c e As New R e c o r d s e t
Dim strSQL As S t r i n g , s t r C o n n e c t As S t r i n g
strSQL = "SELECT * FROM B i c y c l e s " '
1
, s t r C o n n e c t
r s t l n P e a c e . O p e n strSQL, s t r C o n n e c t , adOpenForwardOnly

372

IV. VBA

Recordset
R e c o r d s e t , , .. , ,
R e c o r d s e t Open R e c o r d s e t ,
.

, . , R e c o r d s e t , , , , . C u r s o r T y p e Open
. . 17.1. Forward-only.
17.1. , R e c o r d s e t

-
CursorType

Forward-only

adOpenForwardOnly


,
, .
, ,
,
, .
,
,

Static

adOpenStatic

,
,
, ,
, .
,
, Keyset
Dynamic

Keyset

adOpenKeyset

,

.
,

Dynamic

adOpenDynamic

Keyset,
, ,

17.

373

C u r s o r L o c a t i o n R e c o r d s e t ,
( ) .
a d U s e C I i e n t a d U s e S e r v e r . ,
SQL Server
, Jet.

C u r s o r T y p e C o n n e c t i o n .

LockType R e c o r d s e t ,
, . a d L o c k P e s s i m i s t i c , .
, LockType ,
. 17.2.
17.2. R e c o r d s e t

-
LockType

No lock
(
)

adLockOptimistic

,
,
,
(
U p d a t e ) .
,
,
,
,

Batch update

adLockBatchOptimistii

,
,
,

Read only (all records)


(
{ ))

adLockReadOnly

,
;
,

Read only
(edited records)
(
( })

adLockPessimistic

,
.
,

( )

374

IV. VB


R e c o r d s e t VBA , , - . , ,
.
BOF (Beginning Of File ) EOF (End Of File ).
T r u e , .
And.
T r u e , T r u e ,
:
If r s t Y . 3 0 F and rstY.EOF Then
" !"
End I f


ADO . ,
M o v e F i r s t , M o v e L a s t , -1 MoveNext M o v e P r e v i o u s
. Move
. , r s t Z . Move -3
.
, , . Boo -crr.ark R e c o r d s e t , :
va r "Bookmark! = r s t A . Bookmark
, ':
r s t / i . Bookmark - varBookinarkl
Seek, F i n d ( F i n d F i s t , F i n d L a t , F i n d N e x t
F i n d P r e v i o u s ) , . Seek , ,
, Find, , , .


AddNew
. R e c o r d s e t r s t l n g , ,
, r s t . I n g .
( '"
" ). , ,
. -AddXew
, :
With r s t l n g
Array ( "", " " , ""; ,

17.

375

End With

Array("", 42, "")

, AddNew , , .
A r r a y VBA ,
. , , .
,
.
D e l e t e .



, V a l u e .
, .
, Value , :
I f r s t Y G a t e . F i e l d s ( " S e r v i c e v i s i t s " ) . V a l u e >10
MsgBox " T h i s u n i t needs a major o v e r h a u l ! "
End If
strCurrentFieldData = rstYGate.Fields(3)
F i e l d s R e c o r d s e t ,
. ,
, :
r s t Y G a t e l D a t e = #5/15/2001#
With rstYGate
intltems =![Oil cans]
End With


() ADO . , , , , ( ,
r s t B u c k e t ):
Kith rstBucket
. F i e l d s ( 0 ) . V a l u e = "Love"
.MoveNext
End With
,
, U p d a t e . , F i e l d s Value, , :
With rstBucket
IValue =8.93

376

IV. VBA

.Update
End With
AddNew, U p d a t e
, :
With rstBucket
.Update Array("", "", " "),
Array("", " ", "")
End With


Do ,
;
'
With r s t l n P e a c e
Do U n t i l .EOF
Debug.Print.Fields(0)
.MoveNext
Loop
End With
SetrstlnPeace = Nothing
End Sub

Command
ADO Command , SQL , .
Commands R e c o r d s e t ,
, . (
, , ; ,
, .)
, Command . OLE DB Provider
Command, , .


, -
( SQL) .
Access. , Access , , , SQL. .mdb, . , SQL Server, .
,
: . , , SQL. SQL , , .

17.

377

Command
Command . , ,
SQL ,
. E x e c u t e Command . SQL.
CommandType,
adCmdText SQL :
Jim consecrate As Connection
Dim cmdVBA As Command
Dim prmDate
( Connection...)
Set cmdVBA = New Command
With cmdVBA
.ActiveConnection = consecrate
.CommandText = _
"UPDATE Bicycles SET OnSale = True" _
& "WHERE Category = 4;"
.CommandType = adCmdText
.Execute
End With


(,
, ),
P a r a m e t e r , P a r a m e t e r s Command. , , , ,
Command ,
SQL:
Dim c o n s e c r a t e As C o n n e c t i o n
Dim cmdVBA As Command
Dim prmDate
( Connection...)
Set cmdVBA = New Command
With cmdV3A
.ActiveConnection = consecrate
.CommandText = "qryDeleteOldRecords"
.CommandType = adCmdStoredProc ' in Jet, __
adCmdTable
End With
Set prmDate - New Parameter
With prmDate
.Name = "Date"
.Value = InputBox "Enter the cut-off date."
.Type = adDate
.Direction = adParamlnput
End With
With cmdVBA

375

IV. VBA

.i a r a m e t e r s . A p p e n d prrnDate '

. Lxecute

End With
, Jet/Access,
a d d n d T a b l e CommandType Command, a d C r n d S t o r e d P r o c , SQL Server
.

SQL
ADO, . , SQL, ,
SQL.

SQL
SQL VBA - ,
, . , SQL , Visual Basic, VBA-
, . ,
, SQL, , ,
, .
Access . Access, Command VBA .
Command SQL-, , VBA. , Access ;
View^SQL View (^ SQL), . , VBA,
, , VBA, C c n m a n d T e x t Command. SQL "
Command" .
Access ,
. , .. . ,
Jet ( ).
Access SQL- ,
, SQL,
.

17.

379

SQL
SQL
,
SQL. Jet, , SQL,
, SQL. Jet- SQL, Office. ,
SQL.

SQL VBA-
, , , SQL , VBA. , VBA SQL , . , O p e n E x e c u t e ADO,
VBA, .
SQL .
, C o m m a n d T e x t Command O p e n R e c o r d s e t ,
. , . " : "
.

SELECT
R e c o r d s e t ADO . SQL,
, . , SELECT , .
SELECT . T o y s :
-ELECT * FROM T o y s
, T o y s . , , ,
1 .
SELECT ,
.
,
SQL, UPDATE DELETE.

SELECT
SELECT . siire ULC , , :
:';: W : ' FROM T o y s , C l e r k s

380

IV. VBA

, , . ,
, . , .
, SELECT . , , , ,
. , , :
SELECT Toy, Rep From Toys INNER JOIN Reps On Toys.ID =
Reps.ToylD
INNER JOIN
FROM. ON, , . ON , ,
, , (
).


, :
SELECT Toy, InStock, OnOrder FROM Toylnventory
,
, :
SELECT Toy, [ L i s t P r i c e ] , [Sale P r i c e ] FROM T o y l n v e n t o r y
Name F i e l d , SELECT,
. () ,
AS , :
SELECT Toy AS ToyName, InStock AS OnHand, OnOrder FROM Toylnventory
, . :
SELECT Toylnventory.Name, Clerks.Name FROM Toylnventory, Clerks


, ,
, . SELECT ,
VBA. , , ,
10%:
SELECT Toy, ( P r i c e * .9) AS S a l e P r i c e FROM T o y l n v e n t o r y
,
() . , , .
, , , , : ( P r i c e * I n S t o c k ) AS I n v e n t o r y V a l u e .

17.

381

, -
, , .
:
SELECT UCase(Name) AS [ C l e r k ' s name] FROM C l e r k s
SQL SELECT
,
, , , :
SELECT C o u n t ( R e c y c l a b l e ) AS

[Can R e c y c l e ]

:
SELECT A v g ( P r i c e ) AS [Average P r i c e ]

From Toys

FROM Toys


VBA :
intRecyclableCount = rstRecyclableToys![Can recycle]
: Count, Avg, Sum, Min, Max,
.

DISTINCT, DISTINCTROWH TOP


DISTINCT, DISTINCTROW SELECT . SELECT, . 17.3.

17.3. SQL

DISTINCT

SELECT
bers


,

.

,

;

,

SELECT DISTINCTROW
FROM Members

DISTINCT
ROW

382

DISTINCT Address

FROM Mem-

,
,
M e m b e r s

Name,

Address

,
N a n e A d d r e s s ,
, ,

IV.

. 17.3


ORDER BY

SELECT TOP 10 ToyName


ORDER BY U n i t s S o l d

FROM

Toys

, 10
. 10

ASC (ascending - no )
U n i t s S o l d

:
, , SELECT WHERE,
:
SELECT
SELECT
SELECT
SELECT

* FROM Toys WHERE P r i c e <= 20


Customer, Date FROM S a l e s WHERE Date = #10/24/2000#
Name, Rank, CerealNumber FROM Kids WHERE Rank = ' Q u e e n '
Name, Age, [Shoe S i z e ] FROM Kids WHERE Age Between 3 And 6

, WHERE FROM , , , . , VBA. , , . -,


B e t w e e n ... And, VBA
. SQL L i k e , VBA.
, (And, Or
..), ;
SELECT * FROM Toys WHERE P r i c e > 20 And Category = ' A c t i o n F i g u r e s '
VBA
SQL, VBA, a
, . ,
Command :
strSQL = "SELECT Name FROM Kids WHERE Hates =
'Brocolli'"
cmdEr.CommandText = strSQL
, WHERE, ,
SQL ; , , , .
. ,
, :
S t r S Q L = "SELECT Name FROM K i d s WHERE H a t e s = '
& frmlnputForm.TextBoxl & " ' "

" _

, , #,
. , , - .

17.

383


GROUP BY , , .
, ,
. , ,
. :

SELECT Category, Countf[Category]) AS [Number of Items] FROM Toys


GROUP BY Category;
.

42

37

29

23

17

31

SQL, Avg,
.

HAVING
HAVING GROUP BY . ,
WHERE; WHERE
, .
HAVING ,
, , WHERE:

SELECT Category, Count(Category) As [Number cf Items] FROM Toys


WHERE Price > 100 GROUP BY Category
HAVING Count(Category) > 4
ORDER BY
ORDER BY , SELECT, .
ORDER BY , :
SELECT Toy,

Price,

I n S t o c k FROM T o y l n v e n t o r y ORDER BY Toy

.
, ,
. , , :

SELECT Toy, Price FROM Toylnventory ORDER BY Price DESC, Toy


354

IV. VBA

. DESC (descending ) ASC


(ascending ), .
, .

SQL
UPDATE DELETE .
; , , . , 10% :
UPDATE T o y s SET P r i c e = P r i c e * 1.1 WHERE C a t e g o r y = ' T r a i n s '
, , UPDATE. SET, . , WHERE
, , .
WHERE , SELECT.
DELETE , UPDATE:
.
, :

DELETE FROM Toys WHERE InStock = 0 And OnOrder = 0


, ,
UPDATE SET,
N u l l .
UPDATE DELETE
; . ,
,
.

17.

385

18


/3 ...

>
> -

- ,
12, VBA . - . ,
, , ,
. - , , VBA
.

Haueft /fio ,
VBA, , . , , , . ,
. , "
", . . Open
(). :
Open " p a t h n a m e " For mode As f i l e n u m b e r
Open:
Open ":\ . d a t " For B i n a r y As #1
.
p a t h n a m e (). ( ),
, . , . , ,
VBA.
mode (). VBA, , ; " ".

386

IV. VBA

i S f i l e n u m b e r ( ). f i l e n u m b e r
I
1 511, . ,
I
, #. |
, ( I
F r e e F i l e , 1
f i l e n u m b e r ) .
Open .
,
L e n = r e c l e n g t h , .


Open
mode. VBA , .
mode ,
. , .


Open


,
,
,

V B A
,

Random

.
,
ASCII,
-

I n p u t (
); O u t p u t (
); A p p e n d (
)

Binary


VBA , ,
- , .
. , , . ,
. :

18.

387

Const PetsFile = 1, BdayFile = 2


Open "C:\MiscData\PetsLog.txt" For Input As PetsFile
Open "C:\MiscData\Birthdays.txt" For Random As BdayFile
Input #PetsFile, strPetlnfo ^
Close PetsFile
Put BdayFile, 45, usrBDay '
CloseBdayFile
: , F r e e F i l e ,
. , , , .
: Dim DiaryFile As Integer DiaryFile = FreeFile () Open
C:\MySecrets\Diary.txt For Input As DiaryFile


, .
, , , , .
, , C l o s e . C l o s e #2 ( C l o s e P e t s F i l e ,
). C l o s e
.


VBA- .
.

P u t

P u t # 1 , 1 8 0 0 , S t r Q u o t e (
S t r Q u o t e ,
1800);
Put # 1 , 15, u s r C u s t o m D a t a
( u s r C u s t ornDa t a
15}

Get


,

P u t

G e t t # 1 , 1 8 0 0 , S t r Q u o t e (

1800 S t r Q u o t e ) ; G e t # 1 ,
1 5 , u s r C u s t o m D a t a (
,
15- u s r C u s t o m D a t a )

I n p u t


,
P u t

StrBigText = Input (400,#1)


( 4000
StrBigText)

388

IV. VB

Oneparopwrite # ,

W r i t e #3, s t r S h o r t S t r i n g ,
strLongString,
i n t L i t t l e N u m b e r (
)

i n p u t #

,

onepaTopaWrite #

I n p u t #3, s t r S h o r t S t r i n g ,
strLongString,
i n t L i t t l e N u m b e r (
)

18.

389

19

VBA-
/3 * ..,
>
>

jm , 10,
^/vb VBA-.

6uqe /

, , , . Properties ( Visual Basic 10).


F o r e C o I o r B a c k C o l o r .
F o r e C o I o r ,
. ,
. F o r e C o I o r
, , (
).
.
, , , , , (. 19.1).
System () (. 19.1, ) Windows, .. , Windows Windows. Desktop ( ) Button Face
( ), VBA Windows.

550

IV. VBA

106 UserForm

01 Checkbox

Capton

ContolTipTgid
IHffjMuMMfl

SpeoaEffect 2 - frreuttan
False
visible
True
Behavio

False

Locked
False
TeiKIgn 1 - fmTeiiia
Tripl^staie False
wordwrap True

I] Button Face
Button Shadow
Disabled Te*l
Inactive Title Bar
I] Button Highlight _
utton Dark Shad

Data
ContrclSource
a Font
Font
HaettBnichwBile

El Appearance
BsckColor
eordercotor
EorOerStyte
Cap! ic
Spec a Effect
Behavior
Cycle
Enabled
a Font
Font
Mlsc
(Ma me)
DrawBuffer
HelpCcntEiUD
Mouse Icon

t J SHS0O000OFS
8H80000012B
0 - frrecrderStyleNone

O-frr
0 - frr

200
0
(

. 19.1. VBA
Palette () (. 19.1, ) . VBA . , .


, , ,
VBA , ,
. F o n t (), , , .
F o n t . , ,
Windows. F o n t , , .
: -
, F o n t .
F o n t .
, F o n t
, , . (. 19.2).
,
. ; ,

, - Windows
, , , ? : , , ,
.

19. -

391

*-"-


Comci Sens MS
1
""
1 Bookman Od
l Syle
i

*1
1
C
e
n
t
u
y
i
G
o
h
l
c
i
r
T Cenu
lyi Schoolbook i
)
T
Cuood
uud
*C
E
ireireirr New
1

<>

^ 1 .

. 19.2. ,


VBA , , , - .
, !



, . , , VBA-. ,
- . .
1. ,
.
2. M o u s e P o i n t e r
.
3. .
, , , Windows (,
). ,
Windows.
, , ( ,
).


, , 392

IV. VB

, , , Windows.
, , ,
(. 19.3).

. 19.3. VBA

. .
1. .
Windows ,
. i c o .
2. , "
", M o u s e P o i n t e r
99.
3. Mouselcon.
4. , ( ).
VBA Load Picture ( ),
.


,
- .
.
, ,
.
,
T o o l T i p . , , . 19.4.


, ,
. ,
, , , . . 19.5.

19. VBA-

393

* . (ft *?'
JElil

*'-1 & [

. 19.4. ?

I m a g e ()
Toolbox, ,
, . I m a g e
, , . ( L a b e l ) , C a p t i o n (), .

. 19.5.
394

IV. VB

, , VBA . ,
.
1. , .
2. P i c t u r e .
3. ,
P i c t u r e .
4. Load Picture ( )
, .
( ) .
5. P i c t u r e A l i g n m e n t , P i c t u r e S i z e M o d e P i c t u r e T i l i n g ( , ),
, .
.
P i c t u r e A l i g n m e n t , , .
P i c t u r e S i z e M o d e ,
,
( f m P i c t u r e S i z e M o d e S t r e t c h ) , ,
(fmPictureSizeModeZccm). ,
( f m P i c t u r e S i z e M o d e C l i p ) .
P i c t u r e T i l i n g True
(.. VBA )
False.


, , ,
"" .
, P i c t u r e ,
<Delete>. <Ctrl+X>,
<Backspace>.


, Format
, . ,
.
| S Center in Form ( ) 1 . ,
1
,
|
( ,
|
).

19. VBA-

395

Arrange Buttons ( ) . Arrange Buttons , , .


Size to Fit ( ) VBA
() . A u t o S i z e T r u e ,
.
Size to Grid ( ) ()
.
Visual Basic , .

{ Format).
1. ,
.
2. AlignoTops
(<=> ) .
3. ,
Horizontal Spaclng^Make Equal ( ).
4. (..
Group ()).
5. Center in Form Horizontally (
).

. ,
. 2, 3 5 , .

ftaSopfie

10 . , , 10.


,
, .

396

IV. VBA


, , . ,
. , ,
Locked T r u e . ,
,
. , E n a b l e d
F a l s e , .
,
BackCo^or , , S p e c i a l E f f e c t 0, "".

?
,
, , - , .
, VBA, , .
Visual Basic P a s s w o r d C h a r , ,
(, !) ,
. ,
(. 19.6).

. 19.6.

, .
,
, .


,
, M u l t i L i n e
True. , WrapText T r u e ,
, .
, VBA ,
.
"" ,
<Ctrl+Enter>, E n t e r K e y B e h a v i o r T r u e
<Enter>. ,
VBA.

19. VBA-

397

( ), ,
, ,
.
I S , ( I
!), . I
<Shift+Enter> .
V a l u e , , .
. 19.7 Wordwrap MultiLirte.

I can t sec y<-

) Every thing
i you type I
j clear
"

-_

' V _

WordWrap fabe.Mutll.he TrueWordwrap ralw,MultKine fabe

Imp 1

I inp 2 (not
Lme3

, 19.7.

Wordwrap tfMul tiLine


M u l t i L i n e , Wordwrap E n t e r K e y B a h a v i o r , . , ,
S c r o l l B a r s f r a S c r o l l B a r s B o t h . VBA , , .


, , .
.
"" ,
Windows-,
. 19.8. , , . , ,
; . ""
.

398

IV. VBA


*- " * ? 1 ^ .

|x]

Sproue
td staeltcuats
u /
1
mied
tJ sea teeds
I Lsmor marnate
>

H
Q
i iuS5JtiK
j'Timetoeet 1

FBft

*i"

4 *
*-t
-

L ^
ft

-%c. /<?. ,
,
: Toolbox; ,
. .


, , ,
, . ,
, .
,
. , , .
.
.


,
. , , .


.
1. .
2. New P a g e .
, ,
: Visual Basic ,
, Undo
.
Delete P a g e .


. ,
Caption ,

19. VBA-

399

Rename . .
Rename (): , ( ). , Rename
. .


Move , . , . 19.9, ,
, Move Up () Move Down
() .

. 19.9.


,
.
,
. , .

.
1. .
2. T r a n s i t i o n E f f e e t .
3. ( ) T r a n s i t i o n P e r i o d .
T r a n s i t i o n P e r i o d 0, . 500 (), , .


(, , ) . , GroupName,
.
GroupName .

400

IV. VB



, , . .
, - . , ,
,
G r o u p N a m e .
.

,
. .
. 19.10.

. 19.10.

, Windows
, .
: ,
.
.
- , , . ; .
, . , , .
, .

19. VBA-

401


, :
Toolbox, . .
, ,
. VBA , , . , . ,
, . VBA .
,
, O r i e n t a t i o n .


,
, .
I S ,
I
.
I / ,
1
, .


Min . .
, , Min , ;
Min. Min
. ( .)


,
, . . 19.11 , , , .
, .
, , , , . . , s c l W a r p F a c t o r l b l S c r o l l B a r R e a d o u t , :
P r i v a t e Sub s c l W a r p F a c t o r _ C l i c k
lblScrollBarReadout-Caption = sclWarpFactor.Value
Snd Sub
, , , , C l i c k , .

402

IV. VBA

Ac. /5.//. ,
. /9JO,

2)0nflOZftU4MUfU>6aHUU
.
, , , .
, , .


,
, . ,
, ,
, .
, ,
10. . ,
, U s e r Form:
Sub FormVariableDemo()
Dim frml As FormAnOpinion
Set frml = FormAnOpinion
' :
With frml
. C a p t i o n = " "
. Show
End Wi-h
End Sub
, S e t , , ,
. , . New. :

19. VBA-

403


Dim frmOne As MultiForm
Dim frmTwo As MultiForm
1

Set frmOne = MultiForm
Set frmTwo = New MultiForm ' New
'
i'rmOne . Show
frmTwo.Show


K e y P r e s s , KeyDown KeyUp, . K e y P r e s s
"" (, , ), , .
<1+>,
<Backspace>. ,
K e y P r e s s .
KeyDown KeyUp, ,
, <Alt+Shift+Ctrl+F9>. , K e y P r e s s ,
. , KeyDown, <Ctrl+<> <Ctrl+>>
, , 10.


10 , , . .



. K e y P r e s s , .
:
P r i v a t e Sub t x t S e r i a l N u m b e r _ K e y P r e s s ( B y V a l KeyAscii
As M S F o r m s . R e t u r n l n t e g e r )
' :
If Chr(KeyAscii) < " 0 " Or _
(Chr(KeyAscii) > " 9 " And Chr(KeyAscii) < "A") Or __
(Chr(KeyAscii) > " Z " And Chr(KeyAscii) < " a " ) Or __
Chr(KeyAscii) > " z " Then
MsgBox " ! "
KeyAscii = 0 '
End If
End Sub
, , ,
. If . . . Then ,

404

IV. VBA

. K e y A s c i i = 0; K e y A s c i i
K e y P r e s s , . , . , 0, .
K e y A s c i i
. , , :
P r i v a t e Sub t x t S e r i a l N u m b e r _ K e y P r e s s ( B y V a l KeyAscii _
As M S F o r m s . R e t u r n l n t e g e r )
KeyAscii = Asc(UCase(Chr(KeyAscii)>)
End Sub
, , ,
. K e y A s c i i , Chr,
UCase , , Asc.

BeforeUpdate
,
. ,
. ,
, .
, .
-
Internet, , , , .
,
, B e f o r e U p d a t e .
, ,
<> , . VBA
B e f o r e U p d a t e ,
, ,
. , C a n c e l :
P r i v a t e Sub t x t S e r i a l N u m b e r _ C h a n g e { ]
If L e n ( t x t S e r i a l N u m b e r . V a l u e ) > 5 Then
MsgBox " . . "
Cancel
End I f
End Sub


,
, . -

19. VBA-

405

, . ,
C l i c k . , ,
. ,
, . , ,
, . , .

406

IV. VBA

& ...
, ,
.
, .
20 ,
" " VBA-,

Windows ,
,
,
ActiveX
Visual Basic. 21 VBA,
, , }~,
.

20

( )
VBA
J3 & #...
> Windows
>
> VBA
>
> D i c t i o n a r y
>
>

ActiveX

( , ),
* %*> VBA. , , VBA,
. ,
.

/ / 0 fteeafifte
Window
, , Windows . VBA , .
S a v e S e t t i n g s , , .
:
S a v e S e t t i n g s , , ,
,
. VBA- ,
( ). , .
" " , , ,
, :

20. ( ) VBA 409

S a v e S e t t i n g s " ", "", _


" ", " 6 "
, " 6 " , ,
. . 20.1 .
!>
; *_ gun .* ; " -.
4- l j Adobe
4 CJ Design Science
+ CJ JA5C
+ L j MI erase"
+

.+
'+
+
+
'-

_^j
.Sj'Ho -111
* ^

?
( )

: 1 MiK
CJ Netscape
CJ NicoMakComputing
CJ ODBC
CJ sec
CJ VBancVBAPrcgiamSett.rrgs .
'+ ^ J AFWiBwer
* 2J Merlol
2 j VISIO Add-on?
- wJ

' 121 ^^^^


+ D HKEY_LOCAL_MACHINE
+ CJ HKEY_USERS
;+ Qj HKEY_CURRENT_CONFI3
_
'. = -.
'+ CJ HKEY DYN DATA
KDMnbOTBpVHKY_CURRENT_USER^oftwoie\VB ond V9A Program SE

" - -

'']

/. ?. /. Windows
G e t S e t t i n g . , , ,
__ :
G e t S e t t i n g , , , _
G e t S e t t i n g , ,
.
G e t S e t t i n g ( S t r i n g V a r i a n t ) , , :
= G e t S e t t i n g ( " ", "", _
" ")
, _, ,
G e t S e t t i n g , .
VBA , .
| </ G e t A l l S e t t i n g s
|
.
&S D e l e t e S e t t i n g ,
(
, .

2)oafUfH q/tiftux /
VBA- ,
VBA. ,
, .

410

V.

,
Visio, , Microsoft Outlook.
, Visio,
, , "" Outlook.
Outlook VBA ( , Visual Basic), Outlook
. Outlook VBA
Outlook ,
"" .


; , . , , , .
1. Visual Basic
.
2. , .
3. ateObject.
.



Visual Basic ,
. Visual Basic Tools^References,
References ().
Outlook , Outlook
Available References ( )
References. ,
. , , Browse ().


VBA . , ,
Visual Basic, . Outlook,
:
Dim
Dim
Dim
Dim
Dim
Dim
Dim

objOutlook As Outlook.Application
objOLNamespace As Outlook.NameSpace
colFolders As Outlook.Folders '
objPeopleFolder As Outlook.MAPIFolder
colPeople As Outlook.Items '
objPerson As Object '
strName As String

20. ( ) VBA 411


.
S e t .
a t e O b j e c t . ,
. , . , GetOb j e c t .
Outlook+Visio, :
Sub PeopleDiagram()
Set objOutlook = CreateObject("Outlook.Application")
1
CreateObject("Outlook.Application.8")
1
Outlook
Set objOLNamespace = objOutlook.GetNamespace("MAPI")
Set colFolders = objOLNamespace.Folders '
Set objPeopleFolder = colFolders.Item("Personal Folders")
1

Set colFolders = objPeopleFolder.Folders
Set objPeopleFolder = colFolders.Item("Contacts")
Set colPeople = objPeopleFolder.Items
' Sub, Outlook
ChartAName
End Sub
, a t e O b j ecu . ,
. , ( F o l d e r s , P e r s o n a l F o l d e r s C o n t a c t s , a
C o n t a c t s ) .
, ,
. ,
,
.
. , ,
V i s i b l e True, D i s p l a y , :
objPeopleFolder.Display


, , , , VBA-.
, Visio , C o n t a c t s , Outlook:
Sub ChartAName()
For Each objPerson In colPeople

412

V.

strName - objPerson.FullName
...( Visio
)

Next
End Sub

Web- ,
, Visio. P e o p l e D i a gram Outlook.
(, Outlook,
.)

/#

VJ3J

VBA ,
"" Microsoft Jet ( ,
Access), SQL Server dBase, , , .
. , .
, . ,
.
VBA 17.

:
VBA
- ,
.
, " " ",
". VBA,
. 11.9 (. " ").
, , VBA- . ,
, , ,
. , , .
, , VBA
. , Windows, VBA .
,
"" VBA-. , VBA- , ,
( Open Save).

20. ( ) VBA 413

VBA
11, VBA ,
VBA,
. ,
.
VBA , . , . VBA ,
. . 11.9, 18.

Microsoft Scripting Runtime


, , Microsoft Scripting Runtime.
, VBA- . , , , .
Microsoft Scripting Runtime SCRRUN . DLL, Windows\System. Windows 98 Windows NT Option Pack,
VBA 6. VBA 5 Windows 95 - Microsoft Scripting Runtime ,

Web- Microsoft
(msdn . m i c r o s o f t , c o m / s c r i p t i n g / } .
, Microsoft Scripting Runtime,
, . .
1. .
2. Tools 1 ^ References, References
().
3. Microsoft Scripting Runtime , .
4. .
----! .
, Microsoft
Scripting Runtime, VBA ( . , " ").
VBA-, Microsoft Scripting Runtime, , . DLL. . DLL ( ).

414

V.


, Microsoft Scripting Runtime , VBA . - , .
.
1. F i l e S y s t e m O b j e c t ,
.
2. F i l e S y s t e m O b j e c t .
3. .
. F i l e l a c e w i n g s . t x t ,
:
Dim objFileSystem As FileSystemObject
Dim objTextStreaml As File
Set objFileSystem = _
CreateObject("Scripting.FileSystemObject")
Set objFilel =
ob:FileSystem.GetFile("C:\bugs\lacewings.txt")
objFilel.Copy (":\ \.txt")


F i l e , . , S i z e
Dim l n g F i l e S i z e As Long
lngFileSize = objFilel.Size
DateLastModified , :
Dim dateFileDate As Date
d a t e F i l e D a t e = objFilel.DateLastModified

,
F i l e , :
obj F i l e l . " : \ \"
obj F i l e l . M o v e ":\ \"
objFilel.Delete
, , ,
Move, , . ( .) , F i l e .
, ,
C o p y F i l e , MoveFile D e l e t e F i l e F i l e S y s t e m O b j e c t ,
F i l e .
F i l e S y s t e m O b j e c t . F i l e S y s t e m O b j e c t CopyFolder, MoveFolder D e l e t e F o l d e r , .

20. ( ) VBA

415


, , , ,
, . -
.
, ,
. ,
, , . .
F i l e ,
.
, . VBA , , VBA, Open, P u t Get.


,
.
T e x t S t r e a m . .
I S F i l e .
i S .
| / .
T e x t S t r e a m ,
, :
Sub TextStreamDemof)
Dim
Dim
Dim
Dim
Dim

objFileSystem As
objFilel As File
objTextStreaml As
objTextStream2 As
objTextStream3 As

FileSystemObject
TextStream
TextStream
TextStream

Set ObjFileSystem = _
CreateObject{"Scripting.FileSystemObject")
1
File

Set ObjFilel = _
objFileSystem.GetFile(":\.txt"]
Set objTextStreaml = _
obj Filel.OpenAsTextStream(ForReading)

' _
1

Set objTextStream2 = __

416

V.

objFileSystem.CreateTextFile("C:\HoBbiu<ai^n.txt")
' _
'

Set objTextStream3 = _
obj FileSystem.OpenTextFile(":\.txt")
1


objTextStream2.WriteLine _
" , !"
objTextStream2.WriteLine " - , ."
'
objTextStreaml.Close
objTextstream2.Close
objTextStream3.Close
End Sub
T e x t S t r e a m , .
.
.


T e x t S t r e a m ,
, , . T e x t S t r e a m
-.
, (
) (
).
T e x t S t r e a m , , O p e n A s T e x t S t r e a m
F i l e . .

CreateTextFile

( FileSystemObject)
OpenTextFile
( i l e S y s t e m O b j e c t )
OpenAsTextStream
( F i l e )


, (
)

T e x t S t r e a m F i l e , -
O p e n A s T e x t S t r e a m : F o r R e a d i n g ( ), F o r W r i t i n g ( ) ForAppend-

20. ( ) VBA 417

i n g ( ). , ,
:
Set objTS =
obj File.OpenAsTextStreamfForAppending)

T e x t S t r e a m ,
. .
.
, . ,
, . "" .
, , , T e x t S t r e a m .
, :
'
strHeKOTopbifiTeKCT = T e x t S t r e a m . ReadLine
.
( T e x t S t r e a m )
R e a d [_)

( T e x t S t r e a m }
ReadLine
ReadAll
S k i p ( __)

SkipLine








.
,

.
,

, Write WriteLine, :
'
objTextStream.Write ", !"
1

'
objTextStream.WriteLine strMyTwoBits

418

V.

( TextStream)

Write;}

WriteLine()

WriteBlankLines{_
]


,
T e x t S t r e a m ,
. C l o s e :
objTextStream.Close


. , .
. S k i p S k i p L i n e , ,
o b j T e x t S t r e a m . S k i p (-10) .
, T e x t S t r e a m ,
.
-.
, Skip ,
. , 25 ,
5- , :
objTextStream.Close
Set objTextStream = bjFile.OpenAsTextStream(ForWriting)
objTextStream.Skip(4)
objTextStream.Read(25)
, , . , , ,
.
, - , , , , .

2)lctlonanf
D i c t i o n a r y () , . , . , ;

20. ( ) VBA

419

, . ,
D i c t i o n a r y .
D i c t i o n a r y VBA 5, VBA 6.
VBA. Microsoft Scripting Runtime, " ". D i c t i o n a r y VBA-, , " Microsoft Scripting Runtime".

Dictionary

, " ". :
Dim dictBigCats As S c r i p t i n g . D i c t i o n a r y
Set dictBigCats = C r e a t e O b j e c t ( " S c r i p t i n g . D i c t i o n a r y " )
Add:
dictBigCats.Add "", " "
dictBigCats.Add "", " "
dictBigCats.Add "", ", "

,
D i c t i o n a r y
C o l l e c t i o n , 12, .
, F o r
Each . . . Next. :
For Each airplane in dictBiplanes
MsgBox dictBiplanes(airplane)
Next
, ,
:
dictVacationDestinations("Number1")

= "Fresno"

I S , .
| S RemoveAll. I
For Each . . . Next.
.


, . , . Bruce McKinney, Understanding the
Dictionary Class, Visual Basic Programmer's Journal, July 1999, ( Fawcette Technical
Publications), Internet www. vbpj . com.

VBA , , , . -

420

V.

S u b F u n c t i o n ,
.
, , .
.
,
.
, , , ...
. .
, , , . , VBA.
( 12), ( )
, ( ).
, , , ,
. ,
VBA , . .


VBA , . , , ""
.
, VBA- (
I n s e r t ^ C l a s s Module). ,
. , (Name) .


.
| / ,
.
/ ,
.
S

, , .

T h e r m o s t a t
. - , Web- , . ,
T h e r m o s t a t ,
:

20. ( ) VBA 421

Private sngDegrees As Single '


1
Let Temperature:
Public Property Let Temperature(ByVal snglnput As _
Single)
sngDegrees = snglnput
End Property
' Get Temperature:
Public Property Get Temperature() As Single
Temperature = sngDegrees
End Property
1
CalculateEnergyUse:
Public Sub CalculateEnergyUse()
Const cstConversionFactor =2.45
Dim dblResult
dblResult = sngDegrees * 365 * cstConversionFactor
MsgBox " " & _
" " & _
dblResult & " ."
End Sub


, ,
D e c l a r a t i o n s .
( P r i v a t e ) , ,
. ,
, .
. ( , ).
, .



P r o p e r t y L e t P r o p e r t y Get.
.
, P r o p e r t y Let
P r o p e r t y Get. ,
.
, : , P r o p e r t y L e t
P r o p e r t y S e t . , .
Property Let
P r o p e r t y L e t .
P r o p e r t y L e t , . :
Public Property Let Temperature(ByVal snglnput As Single)
sngDegrees = snglnput
End Property

422

V.

, ,

T h e r m o s t a t . T e m p e r a t u r e = 75
VBA L e t T e m p e r a t u r e 75 .
,
. , , , , - .

Property Get
P r o p e r t y Get F u n c t i o n ,
, , Funct i o n , , , ,
. :
P u b l i c P r o p e r t y Get Temperaturef) As S i n g l e
Temperature = sngDegrees
End P r o p e r t y
Get T e m p e r a t u r e ,
, :
sngCurrentSetting = Thermostat.Temperature
If T h e r m o s t a t . T e m p e r a t u r e > 80 Then
MsgBox " !"
End I f


Sub F u n c t i o n , . , ,
, , . , , , .
VBA ,
.
, .


, ,
VBA .
1. , :
Dim objCustomThermostat As Thermostat
2. Set, , , :
Set objCustomThermostat = N^w Thermostat
3. ,
VBA, :
objCustomThermostat.Setting = 65
objCustomThermostat.CalculateEnergyUse

20. ( ) VBA 423


, Microsoft
"". ActiveX, ,
, Windows,
C++, HTML, Visual Basic, VBA. , Microsoft , , .
, VBA ,
ActiveX, VBA.
. ActiveX , , , ,
ActiveX.
21 -
ActiveX, . ActiveX,
(common dialog box
control), Windows.
ActiveX . , , , VBA , .
, ActiveX, Visual Basic,
VBA-, , ,
( 2 ) Visual Basic.


ActiveX,
.
1. .
, .
2. Windows.
, ,
, . , VBA
Visual Basic.
3. , VBA.


, , , , . .

1. Tools^References, ActiveX, .

424

V.

2. Browse (),
Windows .
ActiveX Controls (*.ocx).
3.
.
References.
4.
References .
5. .

Toolbox
, , (Toolbox). .
1. UserForm Visual Basic,
Toolbox.
2. ToolsoAdditional Controls
Toolbox Additional Controls ( ) .
3. , , .
4. .

Toolbox. . 20.2 Toolbox,
.

II tS X
tr~ i* J* & 1-

"&

. 17.2. Toolbox
ActiveX

, Toolbox. Toolbox,
New Page ( ),
, .

20. ( ) VBA

425

ActiveX
ActiveX Toolbox,
, VBA. , - , ,
. ,
. , , <F1>,
.

426

V.

21

VBA
/3 & ...
>

VBA,

>* , VBA
>

Web-, VBA

ActiveX

^ VBA, W v .
, . (
, ,
.)

7leftasi
VBA , , , .
VBA-.
, ,
, . VBA-,
.


VBA-
. ,
. Web-
-. .

Microsoft

VBA? , Microsoft Visual Basic,
,
, .

21. VBA

427

Web- Microsoft VBA. , VBA:


i n s d n . m i c r o s o f t . com/vba/
, Visual Basic:
msdn.microsoft.com/vbasic/
Microsoft
VBA- Office:
msdn.microsoft.com/office/
, Office Microsoft Knowledge Base (
Microsoft),
, .

(
. VBA .
ActiveX , , ,
. VBA, , VBA.
.
1 / Microsoft Office & Visual Basic for Applications Developer
I
www.officevba.com
I S Visual Basic Programmer's Journal
www.windx.com
Inside Visual Basic
www.elementkjournaIs.com/ivb/

Web'OH/,
, ,
Internet.
Web- Visual Basic, . ,
, .
Web- . ,
, :
www.vbapro.com/
www.mvps.org/vbnet/
www.vbcity.com/
www.download.cnet.com ( Software)
www.geocites.com/WallStreet/924 5/
odyssey.apana.org.au/~abrowne/homepage.html (

428

V.

Access)
w w w . s l i p s t i c k . c o m / d e v { Outlo o k)
www,outlookexchange.com
, Web- , , .

^*
, ActiveX
. ,
VBA-, , -
ActiveX, . ActiveX
- Web.
, , ,
.


, .
ActiveX.
, .
, -. , ,
Windows, , Windows
.
.

Infragistics Corp.
www.protoview.com

ComponentOne LLC.
www.shersoft.com


, VBA
, . VBA- , ,
, ActiveX
. .

Lead Tools
www.leadtools.com

21. VBA

429


, , , ( , )
. VBA ,
ActiveX.

ProEssentials
www.gigasoft.com
teeChartPro
www.sterna.com
3D Charting Toolkit
www.nevron.com


, -
, . , , , .

, VBA .
, -
VBA.
Word Excel, VBA. , VBA-
, Word Excel .
, Word Excel , , . ActiveX
, . .


Spread
www.fpoint.com
Formula One

www.tidestcne.com

Text Control
www.subsystems.com
Developer's Kit
www.subsystems.com

430

V.

, !
,
ActiveX,
Internet.
, activeCharge.
. ,

, .
VBA, Visual Basic, , , Access.

PCCharge DevKit
www.gcsoftine.com


Microsoft
ActiveX . Visual Basic 5 6 , . ,
, , Visual Basic, Visual
Basic. He .

*
ActiveX, VBA- , , , , .


VBA - . , VBA
. , ,
PowerBasic.
( PowerBasic), PowerBasic 4-20 , Visual Basic.
PowerBasic VBA, . VBA , .
PowerBasic DLL,
VBA , PowerBasic VBA - , PowerBasic VBA,
.

PowerBASIC Inc.
www.powerbasic.com
21. VBA

431

, !
VBA- ,
.
, ,
. .
, . , ,
VBA. , ""
Windows, HTML, .

RoboHelp
www.blue-sky.com
EasyHelp/Web EasyHTML/Help
www.eon-solutions.com

432

V.

ActiveX, 35
AutoCAD, 27

BASIC, 33

Component Object Model (COM), 287


Corel WordPerfect Office 2000, 27
CorelDraw, 27

D
DLL-. .

E
Excel
, 350
, 356

HTML Help, 38; 53

M.Y.O.B. Accounting, 27
Micrografx iGrafx, 27
Microsoft Access, 39
, 41

, 82
, 82
Microsoft Excel

, 82

Microsoft Office
, 51
, 78
, 75
, 79
VBA, 57
Microsoft Word

, 80

OmniTrader, 27

SQL, 379

V
VBA, 25
, 54
, 54
, 57
VBA 5 VBA 6, 38
VBA-, 26
, 54
VBA-, 26
, 84
VBScript, 39
Visio, 26
Visual Basic, 26

w
WinHelp, 38; 53

A
, 287
, 195

433

VBA, 84
, 147; 156
, 122; 124

, 318

Microsoft Scripting Runtime, 414


, 26

, 73
, 62
, 84
, 225
, 129; 148
, 294
, 192
, 309

, 222

, 28
, 95
, 393

Format, 257

, 392
VBA, 131

, 252
, 157

, 390

, 391

VBA- Visio, 83
, 72
, 293
Code ( Visual Basic),
63; 104
Project Explorer ( Visual
Basic), 96
Toolbox, 213

, 126
Function, 123
Sub, 122
Visual Basic, 50
Object Browser, 102
VBA-, 69

434

VBA, 39


, 393
, 314

Watches, 201
, 118
, 63

, 394

, 411
, 59

Toolbox, 424
, 60; 213
, 224

, 410
, 415
, 316


,217
, 239
. 127
, 106
, 43


, 83
, 72
Visual Basic, 67
, 209
, 99
, 151

, 294
, 289

, 392
, 111

, 218
, 215
, 217
, 181; 304
, 97
, 147
, 285
SELECT, 380

VBA, 33

, 412
ActiveX
, 426
, 165

, 285; 421
, 64; 105
Dim, 145
Me, 242
New, 299; 314
Nothing, 298
Preserve, 307
Private, 145
Public, 146
Rem, 139
Set, 298
Static, 146

. .

, 286; 303

Step Into, 193


Step Out, 193
Step Over, 193
, 136
, 39
, 39; 68
, 260
, 156
, 149
VBA
, 271
, 286
- , 55
, 200
, 218

, 313

, 35; 41
, 43
, 46
, 43

, 47

,218
, 60
., 88
, 218
, 303
, 307
, 304
, 306
, 39
, 411
, 183

Add, 298; 314


Hide, 241
Remove, 316
Show, 328

435

Show, 239
, 285; 292
, 304
, 217
, 59; 117
, 117; 119
, 119
, 117

. .

Microsoft Access, 82
Microsoft Excel, 82
Microsoft Word, 80
, 148
, 217
, 306

, 127
, 145
, 127
, 204
, 283; 285
ActiveDocument, 331
ActivePresentation, 331
Active Workbook, 331
Application, 333
Application, 295
Assistant, 327
balloon, 328
Collection, 296; 303; 313
Command, 377
Dictionary, 419
Documents, 295
Err, 206
Error, 207
File, 415
FileSystemObject, 415
Me, 65
Range, 339

436

Recordset, 371
Sections, 296
Selection, 338
TextStream, 416
UserForm, 287
, 365
, 292
-, 292

, 411; 413
, 286
, 296
-
, 37

, 411
, 149
, 305
, 146
, 141
, 422
, 297
, 319
, 318

Call Stack ( Visual Basic), 112


Code ( Visual Basic), 52; 103
Immediate ( Visual Basic),
52; 112
Locals ( Visual Basic), 52; 112
Object Browser ( Visual
Basic), 52; 100
Project Explorer ( Visual
Basic), 52; 96
Properties ( Visual Basic), 52;
61; 111
UserForm ( Visual Basic), 52
Watch ( Visual Basic), 52
Watches ( Visual Basic), 112
. .
Watch
. .
Locals
. .
Immediate

. . Object
Browser
. . Project
Explorer
. . Code
. . Properties. .
Properties
. . Call Stack
, . UserForm
,117
Exit, 205
Exit Do, 178
Exit For, 302
Go To, 183
If...Then, 165; 168
Is, 299
Load., 239
On Error, 205
Option Base, 130; 306
Option Compare, 130
Option Explicit, 130; 144
Option Private Module, 127: 130
Randomize, 276
ReDim, 307
Resume, 207
SaveSettings, 409
Select Case, 172
Set, 314
Stop, 191
Type, 143; 318
Unload, 65; 242
With, 164; 300
, 136
, 129
, 128
, 129
, 164

Mod (
), 153
, 152
, 153
,152
, 153
, 152; 153
. 153

, 186
, 298

, 69; 185
, 65; 185
, 185
, 186

Toolbox ( Visual Basic), 35;


52;60;213

. .
Toolbox
Debug ( Visual Basic), 88
Edit ( Visual Basic), 88
Standard ( Visual Basic), 87
UserForm ( Visual Basic),
88; 222
Visual Basic, 50
, 88
, 323
, 88
, 323
, 88
, 130
, 99
, 37
, 145
, 296
, 146
, 146
, 348

, 307
, 297
, 421
, 30
, 303; 317

VBA, 152
, 192

, 161

437

, 159
, 132
, 25
VBA, 414
, 151

, 147
, 298
, 250
, 115
, 28; 39
, 119
, 38; 117
Property Get, 422
Property Let, 422
Property Set, 422
, 127
, 127
, 29; 120
, 127
, 120; 422
Function, 317; 120; 123
Sub, 64; 117; 120; 121
, 377

, 160
Declarations , 119

, 81
, 190
, 219
, 297
, 424

, 194
Visual Basic, 33; 50
, 53
, 95
, 94
, 50
, 92

, 91
, 89

438

Call Stack, 112


Code, 103
Immediate, 112; 196
Locals, 112; 197
Object Browser, 100
Project Explorer, 96
Properties, 111
Watches, 112; 200

Debug, 88
Edit 88
Standard, 87
UserForm, 88
, 94
, 87

Auto Data Tips, 192


Complete Word, 108
List Constants, 108
List Properties and Methods, 108
Parameter Info, 110
Quick Info, 110; 195
, 78
Windows, 409

, 417
, 188
, 192

Application, 292
Checked, 330
TooltipText, 326
, 285
, 292
, 289
, 290
, 291
, 291
, 290
, 290

Controls, 288

, 218; 220
, 136
, 267
, 276
, 29; 59; 242; 285; 293
Change, 248
Click. 245
KeyDown, 248; 404
KeyPress,248;404
KeyUp,248;404
VBA, 132

, 412
, 78
Microsoft Office, 75
, 137
Access, 82
, 423
, 421
, 121
, 206
Collection, 314
, 296; 299
New
CreateObject, 299
, 244
, 126
, 422
, 212
Windows, 85
, 58

Windows, 409

Microsoft Access, 82
Microsoft Excel, 82
Microsoft Word, 80
, 219
, 124

, 154
Like, 155
, 155

Auto Data Tips ( Visual


Basic), 192

Complete Word ( Visual


Basic), 108
List Constants ( Visual
Basic), 108
List Properties and Methods (
Visual Basic), 108
Parameter Info ( Visual
Basic), 110
Quick Info ( Visual Basic),
110; 195
, 35; 41
, 43

, 56
, 294
, 304
. 287

, 163
, 163

For Each...Next, 301

, 416

Boolean, 143
Byte, 143
Currency, 143
Date, 143
Decimal, 143
Double. 143
Integer, 143
Long, 143
Object, 143
Single, 143
String, 143
Variant, 143; 156
, 143; 303; 317
, !89
, 190
, 190

439


Microsoft Office, 79
, 190
, 242
, 219
, 164
, 164
, 98
, 25

, 163
, 31; 287
, 59
, 217
, 217

, 256
, 221
, 38
Asc, 162
CDec, 158
Chr, 162
CreateObject,299;412
Format, 67; 256
, 257
FormatCurrency, 260
FormatDateTime, 260
FormatNumber, 260
FormatPercent, 260
GetSetting, 410

440

Ilf, 259
InputBox, 273
MsgBox, 270
Now, 67
Rnd, 276
Sqr, 129
TypeName, 157
Ubound, 309

, 164
Do...Loop, 175
For...Next, 180
, 29

, 418

, 285
, 326
, 35
ActiveX, 424
, 224
, 394
, 62
, 60

Windows, 85

VBA "", 3-

..
..

..

. .

.. , ..

"".
101509, , , , . 43, . 1.
. . 090230 23.06.99
.

12.12.2001. 70 100/16.
Times. .
. . . 36,12. .-. . 27,34.
5000 . 2347.
- 005-93,
2: 953000 - .
" "
,
.
197110, -, ., 15.

V/

COMPUTER

BOOK

SERIES


Boolean

Byte



Integer

Long


Single


Double

Currency

,

19 ,



,
29 ,

28



Decimal

Data
Object
String
(
)


(-1) (0)
0 255
-32768 32767
-2147483648 2147483647
-3.40282338 -1.401298-45

1,401298-45 3.40282338

-1,79769313486232308
-4,94065645841247-324

4,94065645841247-324
1,79769313486232308

-922337203685477,5808
922337203685477,5807


,
:
+/-79228162514264337593543950335

+/-7,922816251426433759354
28

1 100 .
31 9999 .
VBA-


, 0

2 .

String
( )
Variant

User-defined



0
65400
,
,

TM

V/ "

COMPUTER

BOOK

SERIES


...

(,

,
)

<Shifl+F2>

<Shitt+F3>

<Ctrl+H>

<Ctrl+Shift+F2>

...




{
)

<FS>

<Shift+F8>

<Ctrl+Shift+F8>

<Ctrl+F8>

<Ctrl+Z>
<Ctrl+J>
<Ctrl+Shift+J>
<Ctrl+l>

()
,

<Ctrl+F9>
(

<Atl+F5>

<Alt+F8>

<F9>

<Ctrl+Shift+F9>

<Ctrt+F>
<F3>

<Ctrl+Shitt+l>

<Ctrl+n >

<
,

<1+
>

<F5>

<Ctrl+Break>


...


,




<F7>
<Shift+F7>
<Ctrl+Tab>
<F2>
<Ctrl+G>

Hungry Minds

""


. ,
,
, .
.


, .
,
, ,
.

.

BASIC. ,
, C/C++, #, Pascal, Delphi, JBuilder,
Java, JavaScript . ,
, ,
,
, .
, ,
,
,
,
.
,
Internet. , ,
,
, -
. ,
, .
, HTML,
Web- JavaScript
Java-.
"... " .
.

.

JavaScript

""

!?
JavaScript.
. JavaScript
,
Web-. , Web ,
"" Web-.
JavaScript ,
.
-
,
,
JavaScript. , ,
,
"" Web-

Web-.
, JavaScript ,

Web- Microsoft Internet
Explorer Netscape Navigator. ,
Web-,
JavaScript.
, ,
,
JavaScript.; ,

HTML.

Flash

"1

,
Macromedia, Flash 5.

, Web-
Internet.
. ,
Flash
Web-.
,
Flash.

Flash,
,

( Flash) Web-,
, Web-
.

Flash,
Internet. ,
Web-.
,
Flash, ,

.

-
, .

,

,
.

:
3 4 2 8 8


, .


'.
117419, , 2- ,
8, - -;
(085) 956-19-;
( JPEG
) no e-mail: podplskaecomputerra.ru

" "
1. http://www,computerra.rv/*He/sub&ctiptlon/
2.
3.
:
,
,

7729340216
40702810100090000217

" -
30101810500000000219
044525219

- " " ___

7729340216
40702810100090000217__
___ " - '
4LT

30101810500000000219
.

044525219


" "
- _ _

3 4 2 8 8

200_
4

'

V
1

Вам также может понравиться