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

VBA Excel 2 0 0 2

Excel 2002 Power


Programming
with VBA
John Walkenbach

M&T Books
An imprint of Wiley Publishing, Inc.

VBA Excel 2002


-
2003

32.973.26-018.2.75
62
681.3.07

""
. ..
..
.. , .. , .. , ..
..
"" :
info@dialektika.com, hup://www.diatektika.com

, .
62

VBA Excel 2002. : . .


. : '"", 2003. 784 . : . . . .
ISBN 5-8459-0541-9 (.)
Excel , ,
. , VBA
(,
). , Excel, . , -
. , -. Excel.
,
. .
?
32.973.26-018.2.75

.

, , ,
Wiley Publishing. Inc.
Copyright 2003 by Dialektika Computer Publishing.
Original English language edition Copyright 2003 by Wiley Publishing, Inc.
All rights reserved including the right of reproduction in whole or in part in any form. This translation
published by arrangement with Wiley Publishing, Inc.

ISBN 5-8459-0541-9 (.)


ISBN 0-7645-4799-2 (.)

- "", 2003
Wiley Publishing, Inc., 2003

I. EXCEL

31

1. Excel 2002:

32

2. Excel

45

3.

63

4. Excel

82

II. EXCEL

97

5.

98

111

III. VISUAL BASIC FOR APPLICATIONS

129

7. Visual Basic for Applications

130

8. VBA

175

9. VBA

209

10.

240

11. VBA

267

IV.

317

12.

318

13.

335

14.

364

15. UserForm

387

V.

417

16. Excel VBA

418

17.

440

18.

453

19. Excel

497

20.

525

21.

542

VI.

563

22.

564

23.

591

24.

619

25.

635

VII.

64S

26.

646

27. VBA

658

28. Visual Basic

677

29.

695

30. Excel

706

. , Excel

738

. VBA

743

. VBA

751

. ANSI

755

. Web-

763

775

24

25




,
,
,


I
II
III
IV
V
VI
VI]

25
25
26
26
26
27
27
28
28
28
28
28
28
28
29
29
29
29

30

I. EXCEL

31

1. Excel 2002:

32


VisiCalc
Lotus 1-2-3
Quattro Pro
Microsoft Excel

Excel
Excel Microsoft

2. Excel
...

32
32
33
36
38
41
43
44
44
45
45
46
47


XLM
Excel 5/95
Excel




Internet



Analysis ToolPack

3.




RIC1









48
48
48
48
49
49
50
51
51
52
52
53
53
53
54
54
55
55
56
57
57
58
59
59
60
60
60
60
60
60
61
61
61
61
62
62

63
63
64
65
65
66
67
68
68
68
69
70
70
70
71
72












1900

4. Excel

72
73
74
74
75
75
75
76
77
77
78
78
79
81

82

Excel
Excel

Lotus 1-2-3
Quaitro Pro



, Excel
XLS




Excel HTML
?
HTML-
1?
Excel

Excel

82
84
85
85
86
86
86
87
87
87
88
88
89
89
90
90
91
92
93
93
95
96

II. EXCEL

97

5.

98



?





" "
" "

98
99
100
101
101
102
104
105
106
106
106

-""
-


"-"


" "

107
107
108
108
109
109
109
110

6.

111


,


ActiveX








,



Excel,
,

112
113
115
116
116
117
119
120
120
121
121
122
124
124
125
126
126
127
127
127
127
128
128

III. VISUAL BASIC FOR APPLICATIONS

129

7. Visual Basic for Applications

130

BASIC
VBA

VBA XLM
VBA Lotus
VBA LotusScript
VBA
Visual Basic
VBE
VBE
Project Explorer
VBA
VBA

10

130
131
131
131
132
132
132
135
135
136
137
138
138
138
139


VBA
VBA
VBE
Editor
Editor Format
General
Docking


?








Comment:
Comment
Comment
Comment
Comments
Comment
, Comment

Comment
Application
Range
Range
Cells
Offset

,
.

8. VBA
VBA.

139
140
141
146
146
148
149
149
150
150
151
154
155
156
157
157
158
158
158
159
160
160
I6I
162
162
163
163
165
165
166
167
167
168
170
171
171
172
174

175
175
176
178
179
181
133
185
187
187
188
190
190
190

11


.


With-End With
For Each-Next

GoTo
If-Then
Select Case

191
192
192
194
195
195
197
197
198
201
203
208

9. VBA

209





Run^RunSub/UserForm







Immediate




10.
:



12

209
210
211
211
212
212
213
214
215
2]9
219
220
220
221
224
224
225
227
227
227
228
228
229
229
230
231
232
234
235
238
238
239

240
240
240
241
241
242

VBA













VBA,
,

Excel





Windows API
'
Windows API
Windows
<Shift>
API

11. VBA







,




,




Variant




242
242
243
244
245
245
247
247
247
248
249
251
251
252
253
255
256
257
259
260
260
261
262
263
263
264
265
265
266

267
268
268
269
269
270
271
273
274
275
275
276
278
278
279
230
281
282
283
283
284
285
286
286

13



VBA
Boolean





, VBA
FileExists
FileNameOnly
PathExists
RangeNameExists
SheetExists
WorkbookIsOpen

Excel






?
-

SHEETOFFSET: 1
SHEETOFFSET: 2



Windows API




286
2S7
288
288
289
289
290
291
293
294
294
294
295
295
295
296
296
298
298
299
299
300
300
301
302
303
304
305
305
306
307
309
310
310
311
312
312
314
316

IV.

317

12.

318

...

inputBox VBA
InputBox Excel
VBA MsgBox
Excel GetOpenFilename
Excel GetSaveAsFilename

Windows API
FileDialog

14

318
319
319
320
321
325
328
328
328
330

Excel
Dialogs


13.
Excel
UserForm



CheckBox
ComboBox
CommandButton
Frame
Image
Label
ListBox
MultiPage
OptionButton
RefEdit
ScrollBar
SpinButton
TabStrip
TextBox
ToggleButton


Properties











!
UserForm

UserForm
: SpinButton
SpinButton TextBox

Toolbox

331
331
332
333
333
334

335
335
336
336
337
337
337
337
338
338
338
3*38
338
338
339
339
339
339
339
339
339
340
342
342
343
343
343
345
345
346
347
347
348
350
350
351
352
353
353
353
354
354
356
358
359
359

15



ActiveX
""

360
360
361
362
362
363

14.

364

UserForm
CommandButton
ListBox.





ListBox
ListBox
ListBox


ListBox
ListBox
ListBox
ListBox.
ListBox
ListBox
MultiPage

15. UserForm

387



MultiPage

MultiPage

MultiPage





MsgBox
MyMsgBox

MyMsgBox

16

364
364
365
366
367
368
369
370
372
372
373
376
376
377
378
379
380
382
384
385
386
387
388
390
392
393
393
394
394
396
397
398
398
399
400
401
401
404
404
406
406
407

1:
2: OWC ChanSpace

408
409
4-12
415
415
416
416
416

V.

417

16. Excel

418

Excel
VBA

:

Text Tools

Text Tools
FormMain
modMain
ApplyButton_Click


ShowStats



Text Tools

17.




18.




VBA

418
419
419
420
420
420
421
421
422
423
426
427
434
435
437
438
438
439
439

440
440
441
442
442
443
443
445
445
446
447
448
451
452

453
453
453
454
455
456

17


VBA



ChartObject


ChartQbject


,
ComboBox
];:




Chart

Chart


""





""

457
458
458
460
460
461
461
462
463
464
464
466
467
468
473
476
478
481
481
484
485
487
487
487
488
490
491
492
492
494
495
496

19. Excel
Excel





,
Workbook
Open
Activate
SheetActivate
NewSheet
BeforeSave
Deactivate
BeforePrint
BeforeClose
Worksheet
Change

SeleclionChange

18

497

498
498
498
499
499
501
502
503
504
504
505
505
505
506
506
507
509
509
510
514

BeforeRightClick
Chart
Application
Application

Application
UserForm
,
OnTime

OnKey

20.

5]5
515
516
518
518
519
520
521
522
523
524

525




Word Excel
Excel
ADO
SendKeys

525
528
529
530
530
530
533
533
536
537
540
541

21.

542




Excel









XLA XLS

VBA
VBA
Addlns
Addln
Addln


542
542
543
544
545
546
546
546
547
547
548
549
549
550
550
550
550
551
551
553
553
555
557
558
558
559

19

559
559
561
561
562

,


Excel

VI.

563

22.

564
564
565
565
565
566
567
567
570
572
572
573
574
574
575
575
580
581
581
582
583
583
590



Excel





CommandBars

CommandBar









2 3 .
Excel
Excel




VBA












20

591

591
592
592
593
594
594
595
595
596
598
599
602
604
604
604
605
606
609
611
612

24.
Excel
, Excel




Office
WinHelp System HTML Help System
WinHelp
HTML Help

WinHelp
HTML Help
Help

25.
,





614
615
615
615
616
616
618

619
619
621
621
622
623
624
626
629
629
630
631
633
633
633
634
634

635
635
635
636
637
638
642
642
643
644

VII.

645

26.

646



Excel

, Windows API



VBA
""

646
647
648
649
649
650
651
653
654
654
654

21

656
657

27. VBA

658


VBA
FileSearch
,
FileSystemObject











Excel

256
HTML

658
659
661
663
663
665
666
666
667
667
667
668
668
668
669
670
671
671
672
674
676

28. Visual Basic

677


IDE

IDE
VBProjects


VBA VB
UserForm
UserForm
100 CommandButton
UserForm

-,

29.

: NumLock

VBA
NumLock





: CSVFile
22

677
678
679
680
681
683
685
686
687
688
689
690
694

695
695
696
696
697
698
698
698
698
700
700
701




CSVFileClass

30. Excel
Excel
Visual Basic

, ,

CommandBar
. , Excel

701
701
702
704
705
706
707
711
714
718
720
728
733
735
738

Microsoft

Microsoft Knowledge Base
Microsoft Excel
Microsoft Office

,
Microsoft

Web-
Spreadsheet Page
Web- (Chip Pearson), Excel
Web- (Stephen Bullen), Excel
,

738
738
739
739
739
739
740
740
741
742
742
742
742
742

. VBA

743

. VBA

751

. ANSI

755

. Web-

763


Power Utility
Power Utility
Sound-Proof 2000
Sound-Proof 2000

763
763
773
773
773
774
775

23


(John Walkenbach) ,
; JWalk and Associates, Inc. , - . 30 300 , . , PC
World, InfoWorld, PC Magazine, Windows PC/Computing.
The Spreadsheet Page Web- w w w . j - w a l k . c o m / s s / .
Power
Utility Pak, Microsoft.
: .

24

VBA Excel 2002!


, , ,
, Excel , , .


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


Excel. , - .

Excel 2002 "" , , ,


.

Excel 2002. ( )
Excel. .

,
Excel. , . , :

, , ..;

Excel;

25

Excel;

;
Windows, , ,
.
, , , ,
. , , . , Excel 2002,
2.


Excel. ,
Excel 2002, Excel 2000 Excel 97. Excel,
. , , , Excel
Macintosh. , .
, Windows,
Pentium /4 . Excel
,
.
(800x600 , 1 024x768
, 1 1 024 " "). ,
640x480.

,
, , , .

,
. , .

, , , , "
=(2:50) 51".
. , .
=((1:8)+(1:8))/25

VBA
VBA, . ; . ,

26

, . , , , .
,
VBA : , . ,
. , .
If R i g h t ( A c t i v e C e l l , 1) = " ! " Then A c t i v e C e l l
= L e f t ( A c t i v e C e l l , L e n ( A c t i v e C e l l ) - 1)
, , , , .

,
Excel , , " 2 0 ". , , VBA : " G e t T o t a l s " . , , , .

,
, , , , .
"" : "", "", "
", "" ..


.
, .
,
Excel 2002. ,
Excel, .
,
, ,
.
-
, , , .
, , , .

,
, .

27


. .

I
. 1
, Excel
. 2 Excel 2002 ,
Excel. 3, , , . 4 , Excel.

II
. 5
. 6, ,
.

III
7 11. VBA.
VBA, VBA. 11
VBA .

IV
( U s e r F o r m ) . 12 . , , 13. 14 15 ,
.

V
,
. ,
VBA . 19
, , , . 20
(, , Word).
V 21 .

VI
,
. 22 23 -

28

. 24
. 25 ,
, .

VII
, .
26. 27
VBA . 28, , VBA Visual Basic, .
29 . , Excel.

.
Internet, Excel.
VBA ( ). VBA , , ANSI. , , Web- "" ( h t t p : / / w w w . d i a l e k t i k a . c o m ) .


, .
, .
, . ,
"" , ,
" ". , .


, . , , Web- . , , . , . , , , " " "
90 000 ".
, , . . , . , .
, Web- ( h t t p : //vww. j - w a l k . c o m / s s / ) ,
, Excel.

29

-, ,
. ,
.
, Excel.
, .
.
, ,
(Susan Christophersen), .
(Bill Manville), . ,
.
, (Kathlyn), .
(John Walkenbach)
-,

30

Excel

Excel . 1
, ,
Excel
. 2 Excel . 3

, . 4
, Excel , .

Excel 2002:

,
Excel 2002, , Microsoft. ,
, . ,
. , "" "",
.




. , ,
, , , . , .

VisiCalc
VisiCalc
() Bricklin) (Bob Frarikston) 1978 ,
. VisiCalc Apple II
, . (,
Apple II .) VisiCalc
, ,

. VisiCalc ,

Apple II ,
. VisiCalc ,
Apple II .
; /. Sorcitn SuperCalc , .
1981 IB PC, , VisiCorp VisiCalc
. Sorcin SuperCalc, PC.
VisiCalc, SuperCalc .
, , , , ..
. , - .
,
.

Lotus 1-2-3
VisiCalc, ,
, ,
. (Mitch Kapor) (Jonathan
Sachs), . , The Wall Street
Journal Lotus i-2-3, . , . 1983 Lotus Developmen Corporation,
. 495 (,
), VisiCalc,
.
Lotus 1-2-3 VisiCalc SuperCalc , ,
16- IBM PC AT. , Lotus 1-2-3 DOS , , . , ""
. , , Lotus 1-2-3 .
,
. , .
, Lotus 1-2-3 .
Lotus 1-2-3 ,
. (1) (2)
(3), . , ", 1, 2, 3"?
Lotus , Lotus 1-2-3
1983 1.
Lotus

/. Excel

33

. 19S5 1 2,
2.01, . 2,
, (add-ins) ,
, . , 2 ,
, . ,
.
, Lotus 1-2-3 ,
, , . -
Twin Mosaic Software VP Plannet Paperback Software.
, ( " " Lotus 1-2-3)
Lotus Paperback Software . ,
Lotus, Paperback.
1989 Lotus DOS- OS/2- 3 Lotus 1-2-3.
, ,
; " "
. ,
.
Boeing Calc, , SuperCalc 5 CubeCalc.
3 Lotus 1-2-3 ,
, , .
, , , .
, : .
3 ,
80286 1 "" 1989 . Lotus ,
. 3
, 2.01 ( Lotus 1-2-3 2.2)).
, 3 2.
Lotus ,
: , , ,
.
, 2.2 Lotus 1-2-3
, - .
All ways,
''" , , . , WYSIWYG (What You See Is What You Get , ).
, ,
. ,
, Lotus 1-2-3 , , , -
.

34

1. Excel 2002:


, . , .
, , , Microsoft Office XP " ". , " ". , .
, Microsoft , , .
(Lotus 1-2-3 WordPerfect), . ,
, ,
.
,
. , - . , , Microsoft. ? .
, ? ,
. ? . "" ? ,
1990 Microsoft Windows 3,0. , , , . ,
, Lotus , Windows , , Windows. Lotus 1-2-3 for Windows
1991 . , , , .
Windows .
Excel, "" Windows , Windows- ( ). Lotus, 1993
: Lotus 1-2-3 4 Windows. .
5 Windows, 1994 .
Lotus 4.0 DOS (Lotus 1-2-3 Release 4.0
for DOS). ( ) , Windows. ; 3.4. Windows , , , Lotus 1-2-3 DOS,
.
Lotus ( Notes). 1995 IBM Lotus Development Corporation. Lotus 1-2-3, , , ,
" , ". Excel , a Lotus 1-2-3 .
Lotus 1-2-3 , LotusScript , VBA (. 1.1). ,
. , Lotus, Microsoft VBA.

I. Excel

. I.I. Lotus i-2- LotusScript , VBA

Quattro Pro
(, ,
) Borland International. 1994 Novell WordPerfect International
Borland , . 1996
WordPerfect QuattroPro Cord Corporation.
Borland 1987 , , Quattro. , , Lotus 1-2-3, , , . ,
. , , Lotus 1-2-3, , , ,
Lotus 1-2-3.
1989 Borland Quattro Pro , , Quattro, Lotus 1-2-3
. , Quattro Pro
, ,
. , (Graphical User Interface GUI). : Quattro Pro Surpass, Borland.
1990 2.0 Quattro Pro (Quattro Pro Version 2.0),
Paradox Borland.
Quattro Pro 3.0,

1. Excel 2002:

-.
1992 4? " 1 ' ,
. 5,
1994 , , , (.. ).
Lotus, Borland Windows. , 1992 Quattro Pro for Windows ,
Windows-, : Excel 4.0 Lotus 1-2-3 1.1 Windows. , Quattro Pro
Windows , UI Builder ( ). .
, Lotus Borland. Lotus
, Borland Quattro Pro 1-2-3
, Lotus 1-2-3. , 1994 , , Quattro Pro ,
Lotus 1-2-3 ( - ).
,
, .

. 1.2. Quattro Pro Corel


Borland 5 Quattro Pro Windows.
, Novell Borland , , 5 6 (. 1.2).

I. Excel

37

Quattro Pro , WordPerfect


Office 2000. , 1 18 278 (, Excel ). Quattro Pro .
Quattro Pro . Excel 5.

Microsoft Excel
.
, ' Microsoft
1980- .
Microsoft : MultiPlan, , Excel 2002, .
, 1982 Microsoft MultiPlan
. ,
/, MultiPlan , , Apple IT, Apple , XENIX MS DOS.
MultiPlan
. ,
. , Lotus 1-2-3.

. 1.3. Excel 2.1 Windows, , Excel


? ( Microsoft)

38

1. Excel 2002:

MultiPlan Excel, Macintosh


1985 . -, Excel (
MultiPlan). 1987 Microsoft Excel, Windows ( Excel 2.0 for Windows, , Macintosh). Windows , Excel 2.0 Windows , ,
Excel. Microsoft Excel, 2.1
(Excel Version 2.1). 1990 (2.1),
Windows 3.0. 2JC (. 1.3) ,
, , . Excel
(XLM), , . , . , XML VB, .
, Microsoft Excel ( 2.20) OS/2 Presentation
Manager. 1989 , ( 2.21). , IBM, OS/2 .

Pc. /.4. Excel 3 . ( Microsoft)


1990 Microsoft Excel 3 Windows , , (. 1.4). , , ,

I Excel

39

, (Object Linking and Embedding


OLE), , , , . , Excel 3 (
Q+E). Excel OS/2.
4, 1992 , ,
, (. 1.5). ,
, Excel 4 . Microsoft IBM ; Excel 4
OS/2 , a Microsoft Excel, .

. 1.5. Excel 4, Excel 5


. ( Microsoft)
Excel 5 1994 . ,
, .
Lotus 1-2-3 5 Windows Quattro Pro
Windows , ,
, Excel 5 . , , VBA.
Excel 95 ( Excel 7) Microsoft
Windows 95. Microsoft , ,
Office, . . Excel 95
Excel 5. ,

40

1. Excel 2002

. , Excel 95 , Excel 5. , Excel . ,


, VBA .
, Excel 95 , Excel 5 ( , ).
1997 Microsoft Office 97, Excel 97. , Excel 97 Excel 8. , VBA. (
, ). Microsoft Excel 97
, . , Excel 5 Excel 95, Excel 97 , .
26.

Excel 2000 1999 ; Office 2000. , Excel 2000, , , Internet,


.
Excel 2002 2001 . ,
, , . , , . ,
Excel. Excel
.


, , , .
""
, . , . Excel
.
. , , , Excel ,
.

? .
. , - ,
. -

/. Excel

41

. , .

. , , ,
( ,
). : .

. Lotus 1-2-3 Excel


.
, "".
, " ".

. . , , . , , Excel
.

. ,
. ,
, .

. " ", ( ).

. , , , . , , .

. . ,
Excel. VBA,
, Excel.

.
Internet. , , " ", Web- . , , , . , Excel 95%
, Internet.

. , .

.
( ).

, Excel , Windows.
Windows , ,
- . Excel ( , , , , ).
. Excel

42

1. Excel 2002:

, . , Excel
Microsoft.
, Excel . ,
, Excel , Windows . Microsoft (
). Internet , Web- , , Excel ,
Microsoft Office. ,
.

Excel

. Excel 2 0 0 2 , ,
VBA.
Excel.

. . ,

. VBA , .

Visual Basic for Application (VBA).


Excel. , Excel . (, Lotus I-2-3 LotusScript), .

. Excel
, , , ,
..
.

. ,
. Excel 2002, ( Excel 97) , .

.
VBA .

. ,
.
, Excel .

. Excel , ,
.

I. Excel

43

.
, . , , Excel .

. Excel, ,
, .

Microsoft Query. .
,
Web-.

Data Access Objects (DAO) ActiveX Data Objects (ADO).


, VBA.

. . , , Excel .

"" .
XLA- , .

. VBA,
, . , Excel Microsoft Word.

Web-. Excel
HTML.

Excel Microsoft
Excel Microsoft Office , ( , Office). , ,
. Microsoft . Office
VBA.
, Excel VBA,
.

. ,
. , Excel . , - Excel.
Excel.

44

1. Excel 2002:

Excel


Excel 2002. , Excel,
(,
Lotus 1-2-3, Excel). , Excel, ,
.

...
Excel
( VBA), , Excel, .
Excel:

Excel;

Excel;

;
;
ListBox () ( );

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

Office 2002, Office.

VBA
VBA Excel 5. ( Excel 95) VBA
. VBA, , , , VBA-. Excel 97, VBA
. VBE (Visual Basic Editor
Visual Basic). VBA
VBE, <Alt+F11>. .

.
, , Excel, ,
VBA. .


Excel . , Excel, , , . x l s . Excel
( ). :

XLM (, );
( , ).

Excel 2002. , ""
Excel 2002. , "".
( ), . , . , , .
. , .
Excel 2002 ,
^ ^ . ,
.
, . 1 *
. , ,
.
?
, ? {256x65 536) ,
16 777 216 ( ).
.
VGA , 9 18 ( 162 ). 0,001%

46

Z. Excel

. , 104 000 VGA .


(
), , , 194 . 36 000 .
. , , . , , , Excel
. ,
.


. , .
Excel 256 65 536 . , ,
.
, , . , , , Excel, Microsoft (
) .
, Excel 97, 16 384
.

, .
. , ;! , , , . ,
.
,
. , , ("" "") . ,
, (, ,
, , , ,
).
Excel 2002, , . , Excel, Excel 2002 "" .

( , ). ( ) , .

I. Excel

47


.
,
. , , . ,
.

XLM
XLM ( MS Excel 4)
, . ,
XLM , . , , ,
, XLM
XLM. XLM "",
Excel (4-0 ). , Excel 2002 XLM, . XLM . VBA.

Excel 5/95
Excel 5 Excel 95 . , Excel 97 ,
(UserForm). Visual Basic
.
, Excel 5/95,
.
Excel
5/95, .
.
Excel 5.O.
, Excel 5/95- ,
Excel.

Excel
. , , , , .. Excel
, , Windows, , Excel - "" Windows.
Excel Excel.
, ,
"".

48

2. Excel


Excel 97, .
, .
Excel . (
, ,
). Windows ,
, , , .
(, <Ctrl+Z>).
, , .
, (, , ^ ). , .
.
^ 1 ^. ,
, "". , Excel , , . ,
Excel 5 Excel 95, Excel 2002.
, Excel 5 Excel
95, Excel 5 Excel 95.
, .
Excel ,
. ,
.
23.


Excel . , , Analysis ToolPack ( ) .
Excel . . , , (. 2.1). 11
13 . , ? .

( Excel 97).
, (
MultiPage).

IV.

I. Excel

49

. 2.1. , , ,


Excel 2002 ( ). ,
, .
^ 1 ^
. , .
( ) . Excel
.

, , . Excel , , (. 2.2). , Excel , , , .

. 2.2. ,
,
,

50

2. Excel


Excel 2002 .
. , , ,
. , .
, .
. , .
. .
. , .
. , ^.
. ,
. , .
. ( ).
22.

Excel, ,
, , . <Ctrl>,
.
, Excel . <Ctrl>, .
;
.

Windows,
.
( ) OLE-.


Excel . ,
, <Ctrl+C>. Excel ,
, .
Excel.
,
.

/. Excel

51


( , ,
..), , .
, ^ , , , . ,
,
( ).
Excel , ,
.


Excel .
Excel :
( / );

("" "").

, Excel, .
, <Enter>. ,
<Shift+Enter>,
<>, <Shift+Tab>.
,
. , ^. ,
, .
,
, ,
<Ctrl+Enter>.

, <F2>, <Ctrl+Enter>.
,
, , <Ctrl>, .
- ,
.
, .
, <Ctrl+D>, , <Ctrl+R>
.

52

2. Excel


( ). , <Ctrl+Alt+Tab>. ,
<All+Enter>. .
0, ,
( </>). .
,
( ). ; , .
<Ctrl+;> <Ctrl+Shift+;>
.
{ ), ^.
(=). , Excel , Lotus 1-2-3,
(&), (+) (-). ,
<Enter>, .


,
Windows. , . , , . ,
<Ctrl>. , ,
,
, , <Shift>, .
, Excel 97,
. Excel 97, . ,
, <Ctrl>.

Excel : .


"", .
, (. 2.3).
.
, , , (
), .

/. Excel

53

. 2.3. Excel


, .
, , ,
. ,
if ,
. .
: <Ctrl+l>.

( ).
, .
, .
Excel , .
, ^ . , . , , .

,
. Excel ,
. , , (
, ).

54

2. Excel

3, .

, Excel .
, , ,
, . , , , . , .
3.

,
- . Excel . , Analysis ToolPack,
( ).

, . 2.4.
, ( -> <Shift+F3>).
, .

. 2.4.
Excel 2002 , . , ,
. , , ASCII , ,

I. Excel

55

, Excel VBA (. 10).

,
, , , ,
(, , ), .
Excel, ,
. , , .
,
, .
, : . , ,
.

,
, (. 2.5).
, ,
, .

. 2.5. , >

Excel ,
.
^ ( <Shift>). , . ,
"" , .
, ,
, , .

2. Excel

Excel 2002 .
, ^ (, 2.6). , , ,
.

. 2.6. Excel 2002


,

Excel . ,
.
Excel 2000, .
, .
, Excel . , (
, ). , .

, (
). "" .
, Excel (
, ).
, (. 2.7).
.
, .

. Excel : XLM VBA. XLM


. VBA. , Excel
XLM, .
VBA.

/. Excel

57

. 2.7.

VBA III.


{ Lotus 1-2-3).
Excel.
, , , .

58

. , . Excel 65 535 256 (


).

. .

2. Excel


, , Excel
. ,
1
, , ^, , .
Excel,
, . , , , . , .
^^. ,
, . . 2.8 , ,

Puc. 2.8. Excel


,

"",


, ,

^^ .


,
^ . Microsoft Query,
, .
.
Excel 97, Web-, ,
Internet.
, DAO (Data Access Objects )
ADO (ActiveX Data Objects ActiveX),

I. Excel

59

, Excel, VBA
, , , ADO
, .

Internet
Excel , Internet, , HTML, Web-. , , ( ).
Excel HTML, . , Web- Office, Web-
( Web- Office).


, Excel (
). , Excel .

(, , ). Excel
(, ),
. , .


Excel ( )
, . , Excel
,
.


, ,
, Lotus 1-2-3.
Excel , .
, .

Analysis ToolPack
Analysis ToolPack ( ) 19 ( , ),
. Excel .

60

2. Excel

, Analysis
TooLPack . , .
, , , .


Excel . ,
. . , , VBA.
, , -, .
, VBA ,
17.

, Excel ,
. , ^ .


Excel
, "-"
, , .
Lotus 1-2-3 Windows Quattro
Pro Windows. ( ,
Frontline Systems.)

, Excel . ,
1 1 * .
, Excel, , Web- Microsoft. ,
; , Internet.
, 21.

I. Excel

61


, Excel, . Excel , . Excel ,
, . , Excel 97, Excel 2000
Excel 2002 , , , Excel, , .
Excel ,
( 4).
Lotus 1-2-3 DOS,
, , Excel.
Excel.
, 26.

Excel 2002, .
3, .

62

2. Excel

, .
"". Excel

.


. ,
{
, ).
Excel ,

( , ).
,
:
, + () * ();
( );


).

(,

1024- .

. , ,
.

Excel 2002,
"".

_|

Excel 2002 . , ,
. , .
.
. .
.


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

(, ). Excel .

Excel ,
, .
, .

. , D12 D11, D11, D12.


, Excel , ,
. , ,
, .
(
).

, .
.
<F9>
.

<Shift+F9>
. .

<Ctrl+Shift+F9> . ; . , Excel ,
, , VBA.
|
*
--""

64

Excel () . ,
.

3.


, .
( ), ,
. .

. . ,
(: 1).

. . ,
(: $$1).

. . , , ,
,
(: $1).

- . , , , ,
(: $1).

. , . : , ,
<F4> , .


, , , -
. ,
. 3.1. 4 :
=$3*$4

. 3.1.

I. Excel

65


( ) ( 3).
, 8, , F8.
( 3, ,
), . ,
, .

R1C1
, Excel 1. , , , , ,
. , Excel RJC1.
( R row, "", column, "").
1 R1C1. 2 , R2C1 ..
R1C1, ^ ,
R1C1. ,
. , it
.
. 3.1 ,
R1CI. , 1 (
R1C2).

3.1. ,
|

R1C1

s*l+l

=RC[-1]+1

=SA$1+1

=R1C1+1

=SA1+1

=RC1+1

=A$1+1

=R1C[-1]+1

=(1:10)

= (11[-1]:R[9]C[-1])

($$1:$$10)

=CyMM(RlCl:R1OC1)

R1C1 , . , , .
. ,
Rf-5]C[-3] ,
, . , R[5]C[3]
, .
, . , R[5]C , .
, R1C1 ,
. , R1C1, .
(, ). RIC1 . - , , , .

66

3.

, VBA , , , R1CI.


, , , .
, ,
. , :
=2!1+1
, ,
.
( ), . :
=[.xls]1IA1 + 1
, , , (
) . :
='[ 2002 ] 1 ' ! 1 + 1
,
. :
=':\MSOffice\Excel\[ 2002 ]1'!1+1
. . . ( , ).

Excel 2002 ,
. ( Excel), .
Excel, , ( ). , , , . , , B a d f i l e . x l s , ,
1 , 1
1 :
= ': \Files\ [Badfile.xls]JlMCTl !1
,
. , .
. , ^
, , -
( ). :
, .

I. Excel

67


Excel
. , ,
, , . ,
Excel, , ( ''
").


^ ^ ( <Ctrl+F3>). ,
( , ). , , <Enter>.
,
. ^ ^ . ,
. 3.2 , 4 : F4 , 5 : F5 ..
, : 1, D3 : D6 2 ..

. 3.2. Excel
VBA, . ? : , VBA, , VBA- . ,
VBA- 4, R a n g e ("4 " ) ,
, . ,
, , R a n g e (" I n t e r e s t R a t e " ) .


, Excel
, . ,
F10 :
=1-2

68

3.

Al , 2 , =-. ,
. , . ^^. , ,
. , ,
.

, "" . ,

^-*"

,
. , , ,

#?.

Excel . , , .
, .
. , . , , , ,
VBA.
Sub DeleteHiddenNames(}
Dim n As Name
Dim Count As I n t e g e r
For Each n In ActiveWorkbook.Names
If Not n.Visible Then
n.Delete
Count = Count + 1

End If

Next n
MsgBox " " & Count & " "

End Sub

Power Utility Pak ( Web- )


, ""
.


Excel ( ). , . .
, . . 3.2. :
=2 ,
440 ! .
( ) , :
=()

I. Excel

" "
Excel 97, " ", . Excel
. ( ). , ( " ")
=
, .
, .
, , VBA. , Microsoft
. . .


Excel .
! 2 : 5 .
, ! D .. "
" 4, 5 ..
, ,
, .
, ,
. , , , 7,
!.


, .
, . .; , (, !!). ,
, .
,
, , , .
, ,
. .

, , , , .


Excel , (
Excel ). , , , ,
( ).

70

3.

,
. (,
), . ,
:
=*

(. 3.3). , .
, , .
. ,
. , =, .


, .
. ,
, . , , .
. 3.4 (=1 1).
. 1,
, ( , ). - =,
, , ,
. , 10 10 , , 3 4, D10 , S1 (3 4- ):
=

. 3.3. Excel ,

. 3.4. ,

,
, ,
. , D3 2, :

=1!321!32

I. Excel

71

, . ,
, ,
. , 1 , (
). :
=!1!1

, , , . ,
.
, .

Excel
. .
.
, , .
Excel , , .. , . , Excel.
,
, () . , ,
.
"", ,
.


,
, , , . ,
VBA.
, , ^, (
). , , . , <Enter>.
, ,
, . <Enter>.


, , , , . , , , . " "
,
, . ,

72

3.

, .
. 3.2 - ,
.

3.2. Excel,

#/ !

(, ), ,

#/

( ) ,
, . , .
, # / ,

#?

, Excel . ,
, , ,

# !

, ( )

#!

(, , )

#!

. ,

# !

,
, . , , VBA-


, .
, . ,
,' (Excel ).
, 1x5 1x5,
1x5. ,
; . , . Al :5 1 : 5. :

=1:5*1:5
<Ctrl+Shift+Enter>. , ,
({}), . !

I. Excel

73


Excel
, ,
. ,
.
. 3.5
Al :5. ,
,
, . ,
, ,
.
, - . 3.5. Bl
, , , ,
. 3.5, .
Al :5
1. 1 : 3 5 .
2.

:
=(1:5)

3. <Ctrl+Shift+Enter>.
. , 1 : 5, , Al :5 25 .
, 1 : 5
, . ,
( <Orl+Shift+Enter>):
=((Al:5))
:
{=((1:5))}
( ) , , A l : 5. 25.


. 3.6 , . , ,
, 42- .
Web- ,
.

3.

. 3.6. ,

.
, -
.

, .

,
<Ctrl+Shitt+Enter>.


, Internet, .
. ,
, .


Excel, , , ,
, .
: , , ,

I. Excel

75

, .
, : ,
, , , .
. 3.3 . ,
(
). , ,
,
.

3.3.

= (; 12)

, , 12

= (; D + , 1 12
(,-12)
= C O U N T I F (; - < 0 " )
= (; "<>")
= (; " > = i B ) ~
(;">10")
= (; " y e s " )

,

, 1 10
, yes () (
)

= (,-" * " )

= (,-" * s * " )

, s ( )

= ( ; " ? ? ? " )



, ,
( " ").
, , , <Ctrl+Shift+Enter>.
( ), :
=( (() ;1,-0) )
, ,
:
=((();1;0))
( ; ), :
( (; ) >0 ; 1; 0 } )
. 3.4 , , , 3.7.

76

3.

. 3.7.
,

3.4. ,

=((2:10="")*(2:10=
"")*2:10)

=(<2:10="")*(2:10<>
"")"2:10)

=((2:10="")*(2:10=
""))

=((2:10="")*((2:10=
"")+(2:10="")))

=( (2 :10= "") (2 :10>=


200)2:10))

, $200.

=((2:10>=300)*(2:10<=400)
(2:10))

, $300 $400.
,
$300 $400.

=((2:10>=300)*(2:10<=400))


.
, . ( ) , , , . , . .
,
, ..
. 3 .
, ( , Excel).


Excel .
, Excel, 1 1900 .
1. 2 1900
2 ..

/. Excel

77

, .
, a Excel . , 1 1999 , 01.06.1999
( ). Excel
36312, .


, , . , Excel,
, , ,
. . Excel ,
, , , : , . , 1 1999 36312. ( ) 36312,5. ,
.
, . ,
.

, .
, 0 1900 . .. SA

'&* . Excel ( # # # # # # # # # ) . ? 1904 .
^ ,
1904. , .

- ,-.-gv

''

, ,
24- . 24- Excel
. , : , . , ,
24- :
[hh]:mm

1900
, 1 1900 . , Excel, , ,
, 1 1900 . , . ,
Excel , :
4 1776 .
, , , . , , , , , .

78

3.


Web-

Web- Extended Date Functions


{ ). , ,
100- 9999- . . 3.8 , 1900 .

. 3.8. Extended Date Function v 1900



. .. , ,
, - . , ,

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

. 3.9.

I. Excel

79

. 3.9 , . 3.5. ,
. G,
.

3.5. ,
! (. 3.9)

=(1)

= (" ",- 1,-1)

D1

= (" - ;1;1+1)

= { { D 1 ) ; c i ; DI )

F1

= ( B l ; c i )

G1

= (1;{1) - E l )

HI

=Fl&Gl


( )

, .
: , ,
( ). , . ,
Hi , ' 1. :
={(1) ; { " " ; ( Al} ;1) )
&1( (1) ; (1) )(((" ";(1);("
(1);1)+1>);{"
";(1);1);
("
";(1);{"
";(1);1)+1)))
, , , .
, , , , , , . ( , ).

, -

, : Excel 1024 . 10
^*
VBA . , :
NOMIDDLE(Al)
,
.
,
. . , 65536 . ,

80

3.

. VBA-.
. 3.6 .

I 3.6. VBA-

( )

10,8

24,4

6,2

8,9

106J

8,6

, , .
.
, ,
. VBA- . VBA- ; , Excel.
/
Web- ,
/
.

Excel
, , . ,
.
, , Excel
.

\. Excel

81

Excel

Excel,
, . ,
, Excel.
.

Excel
Excel - ( ).
E x c e l . .
, Excel Windows . ( ,
). ;
,
. ,
.

=>.
x l s t a r t ( " Excel")
, ,
.
,
. , 1 . .
,
( ) ,
.
,

x l s t a r t B o o k . x l t .
.
Excel , , . . 4. L

4.1, Excel

/automation

Excel ,
x l s t a r t . " " Excel

Excel "" . ,
Excel
Excel ( )

/embedded
/

Excel 4.0
( )

Excel Windows,
. (.
. / r e g s e r v e r )

/
/s


Excel "" ; , x l s t a r t
Excel Windows,
. , , Excel
, , ,

/regserver

/unregserver

Windows,

, ,
, Excel. , ,
X I f i l e s Excel,
/ , . , ,
. ,
:
"C:\Program Files\Microsoft Office\Office\EXCEL.EXE" / C:\Xlfiles
Excel. . , , ,
Excel.
.

I. Excel

13

Excel
Excel , E x c e l . , .
. . 4.2 ,
Excel, ,
.

4.2. , Excel

HTML-

DLL

(Dynamic Link Library). DLL Windows

. , Excel,
Excel.

OLB

(object type library)

TXT

ASCII-,
(, , ). ,

XLA

Excel. Excel. ,
,

XLS

Excel. Windows, E x c e i i o . x i b ( , )

XLC

Excel 4 (, Excel 5)

XLL

Excel. ,

XLM

Excel 4 (, Excel 5)

XLS

Excel. ,
Excel,

Analysis TooPak ( )

XLT
XLW

XLK

Excel *
, .
Excel 4 ( , , Excel 5,
Excel )

Excel, Excel 2000,
Windows { . h i p ) . Excel 2000, , HTML,
HTML- * , chin.

84

4. Excel



Excel XLS, , .
,
. , , , ? , , , Excel ( XLS) ,
, , XLS.
, XLS ( XLS), ( , ).
, ,
Excel.

Lotus 1-2-3
Lotus.

WKS , Lotus 1-2-3


1 . DOS. Excel , .
, Excel Microsoft Works,
WKS.

WKJ , Lotus 1-2-3


2_\ DOS. * . a l l ( Allways) * . fml (
WYSIWYG (What You See Is What You Get, " , "). Excel .
* . wkl, ,
.

WK3 Lotus 1-2-3 3.x DOS, Lotus 1-2-3 4..v


DOS Lotus 1-2-3 l..v Windows.
. * . fm3 (
WYSIWYG). Excel WK3.
( ) FM3.

WK4 Lotus 1-2-3 4 .. Windows Lotus 1-2-3 5..V Windows ( Lotus, -,


).
. Excel . Excel WK4, : Lotus I-2-3 4 Windows ( )
WK3, Excel .

/. Excel

85

123 Lotus 1-2-3 97 Lotus 1-2-3 Millennium Edition. . Excel ,


Excel 123,
. Lotus 1-2-3, WK3,
Excel .

Quattro Pro
Quattro Pro .

WQI , Quattro Pro


DOS 1, 2, 3 4. ( Excel , .

WQ2 Quattro Pro DOS 5. Excel , .

WBI Quattro Pro Windows 1 5 ( 2 4 ). Excel , .

WB2 Quattro Pro Windows 6.


Excel , .

WB3 Quattro Pro Windows 7 8.


Excel , .
Web- Microsoft Quattro Pro. WB3, Quattro Pro 97. :

http://oficeupdate.microsoft.com/downloadDetails/quattr97.htm.


DBF , dBASfi . Excel DBF
dBASE 4 .
, Excel
. , Microsoft Query,
Excel . Microsoft Query
Excel, * ^ .


.
,
.

86

, , , . Excel
, , . , Excel ,
* . tx1:.

4. Excel

, , ,
. ( ,
, .) .
Excel , , . . Excel ,
CSV.

, , ,
. Excel ,
, . ,
Excel , PRN.

Excel , , .
, :
<Shift>, .

, . 1^ 1* .


Excel .
DIF (Data Interchange Format )
VisiCalc. . Excel , .

SYLK (SYmbolic L i n K ) MultiPlan.


SYLK . Excel
, .

, Excel
Excel . .

XLS
XLS, Excel 2002,
, Excel 2000 Excel 97. Excel,
Excel 97. , ,
Excel. ,
.
Excel . , XLS XLA.

I. Excel

87

XLS
, , , Excel XLS. ; Excel,
XLS, , , ,
, . ,
VBA Excel, 13 .
, . Visual Basic
Editor ( Visual Basic) <:Alt+F11>, , Immediate, <:Ctrl+G>. , <Enter>,
VBA:
Print ActiveWorkbook.FileFormat
Immediate , . , .

16
29
33
39
-4143

Excel
Excel 2
Excel 3
Excel 4
Excel 5. 95
Excel 97, 2000, 2002


,
Excel. , ,
. " '" XLW.
^ .
Excel .
, ,
Excel. , ,
, XLW. , ^
.


( XLT).
, , . , .
, , . .
, ^ , .

88

4. Excel

*',
^ ^

Excel 2002
, . , , Web- Microsoft.
, ,
<Ctrl+N>, . , .

B o o k . x l t ,
. , S h e e t . x l t ,
, . ,
, Excel -, .
.

Xlstan. Book. x l t
S h e e t . xl t. , .

Templates. , .
Templates Excel.
, VBA;

&***
'^

MsgBox Application.TemplatesPath


Excel
XLB. Excel 2002, E x c e l l O . x l b .
Excel; , * . x l b .
( ) ,
.
XLB,
Excel, Excel 2002. x l b .


, .
I s A d d i n T r u e (),
^.
, Excel . , , .

W o r k b o o k s ( ).

Excel .
;
.

I. Excel

89

. , Excel, , .
XLA, .
Excel XLA, XLL ( Excel 2000) . Excel.
, XLA.
21.

Excel HTML
HTML World Wide Web. , , HTML. HTML-
, . ,
.
Excel 2000 , HTML "" . , HTML, HTML- Excel, , , , Excel
(, , , ), . HTML , ,
, Excel "" , , .
, HTML ""
, Microsoft, ,
, " ".
, , HTML .

?
, Excel HTML
"" .
, .
,
,
HTML. ^ ,
. . 4.1 ,
.
.
,
HTML
. HTML-, Excel.
HTML-.
. , HTML- "" , . HTML-
HTML-. , , .

90

4. Excel

HTML- ""
Web-.
,
HTML

HTML-, Excel.
Excel HTML-.
, HTML- . , .
, HTML- .

<heacl> < / h e a d > .

. , < s t y l e > < / s t y l e > , , , < h e a d > < / h e a d > .


"" , , ( < t a b l e > </ t a b l e s ) .
< t d > . ,
Excel .
HTML-, , 4000 ,
Web-, . , , Excel
HTML-.

HTML-
, , . , HTML-.
, 1: A3 <F11>, .
. , Excel ( !).
HTML- ( 10 000
). , ( , ). , ,
HTML- Excel.
HTML-, , , .
JavaScript (JavaScript , Internet Explorer Netscape Navigator). HTML- , HTML-. ,
, .
HTML- ( , ,
).
GIF- ().

/. Excel

91

CSS- ( ,
).

XML-. XML extensible Markup Language, .. ( ). ,


.

, 1 Excel
HTML-. ,
MSO (MSO " Microsoft Office"). ,
Excel (, , , , ..).
, , , Excel HTML
. , , .
, Excel ^. HTML- ,
XLS-. :
HTML , .

?
HTML , . 8 Excel HTML-, ,
. , HTML- ,
, , , , ""
. , ( ), , ( ).
, HTML , , . ^ -.
'. ,
. .
-. ,
.
HTML-, , , .
. 4.2 , Microsoft Internet Explorer.
Script Editor?
, , Excel, Microsoft Script Editor
( Microsoft). , <Alt+Shift+F11>.
HTML- JavaScript ( VBScript). 6 , ,
, ..
.
Excel VBA , Web.
, , , HTML-, , , .

92

4. Excel

. HTML-. , ,
. ActiveX. , ), Office 2000 .
HTML,
Excel 2000 2002. ,
( ).

. 4.2. Excel ,

Excel
Windows. ,
, Excel -

0
Windows 3.1 OLE- .
Windows 95 ( ),
, , .
, .
: S y s t e m . d a t ( )

/. Excel

U s e r . d a t ( ). Windows.
, P o l i c y . p o l , , .
(
R e g e d i t . , Windows), , . ,
, .
" , ...". . 4.3
.

. 4.3. .

, .
. . 4.3 ,
, ,
.

4.3. Windows

HKEY_CLASSES_ROOT

OLE,

HKEY_CURRENT4JSER

u s e r . d a t , ( , HKEYJJSERS)

HKEY_LOCAL_MACHINE
HKEYJJSERS

S y s t e m . d a t

HKEY_CURREWT_CONFIG

HKEY_DYN J3ATA

94

4, Excel

Excel
, Excel 2002,
HKEY_CURRBNT_USER\Software\Microsoft\OfficeM0.0\Excel
,
, Excel .
, ..
Regedit. ,
, .
, , ,
. -, ,
. (
, .) Windows .
-, 1* . ASCII-
. , , , ASCII-,
( ^). Regedit.
, Excel, .
, Excel
. , Excel
. Excel
( , ),
. , Excel ( ), ,
Excel .
. 4.4 , Excel 2002.
, .

4.4. , Excel

Add-in Manager
( )

,
1 ^ . , Excel.
, ,
Converters () () ,
Excel
AutoSave

()
Delete Commands
,
( )

I. Excel

ft-

. 4.4

Error Checking
( )

Init Commands
( )

Init Menus ( )

Line Print ( )

,
Lotus 1-2-3. Excel , Lotus 1-2-3, /wgdu (Worksheet Global Default
Update, " , ")

Options ()

, .
, , . Excel (, )

Recent Files (
)

{ )

Resiliency
( )

Security ()

,
Spell Checker (
)

Userlnfo (
)
W K ? Settings
{ WK?)

Lotus 1-2-3
(, , FM1 FM3)

, Excel, ( )
( Excel ).
; , " , ...".

, Excel.
Excel, ,
Excel ( , HTML). , Excel,
Windows. , .
I. II, ! , .

96

4. Excel


Excel

I ,
,
Excel.
5
. 6
, ,
Excel-

.
.
,
. , , ,
.
Excel .
.

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


,
, (, , , Pascal BASIC)
.
, ,
.

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

, , ,
-.

(
).

. , , , .

, , . , . , . , ,
.

( )
.

, , .

, , .

, , ( ).

,
.

, ( Excel).
,
( , , ,
).


.
, , , ,
.

//. Excel

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

?
15- .
, . .

, .
. , , .

, . , , .
, ,
.

. , . ( ) , " "
.

, . "", , , .
, . , ,
, , . , , , .
,
.

, .

, , .


( ).

, .

100

5.

6.

, ( Excel). , Microsoft , . Excel , , ,


, , . , ,
. , .


, ( ,
) :
.
. , , . 5.1.
. ,
,
( ). ,
, .

' 5.1.


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


, . , , , .
. ,
.

//. Excel

101

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

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


, , , , ,
. , , ,
.
. Excel,
Lotus 1-2-3 DOS.
( ?) . . , . ,
, : .
. 5.2.

5.2. ?

, -

12%

100%

20%

91%

76%

82%

76%

82%

12%

82%

12%

73%

8%

73%

12%

64%

102

5.

. 5.2


,
,

68%
72%



55%
36%

28%
60%

27%
18%

36%
0%
32%

18%
9%
0%

,
, , , , , , . .


,
, . : ; ;
.
12% , ,
, ,
. ,
, , . , , .
, .
. , , , ,
.
, , 12 .
,
, , 90% . . , , , 8% , ,
. , . , , -

U. Excel

103

. Lotus I-2-3.

. .



. Excel.
, , ,
. , , , , .. ,
,
:
.

, , . ,
, :

, .

, . ,
, , , .



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

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

Excel.

104

5.

. ,
Excel,
-, .

. , .
,
.

. Excel,
, , PageMaker.

- ,
,
, .

. , Excel . Internet ( ) ,
Excel .

, , , .
,
. , . , , .


, . , . ,
. . , , ,
.
() :

" ";

" ";

-"";

-;

"-";

" ".

II. Excel

1Q5

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

" "
, , ,
, .
, ,
. , ,
"" , , .. , ,
( , ..) .
, , "
", , .
( ). : , , ,
, .
, .
,
. , Excel,
. , : ,
.
, , , . , , .


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

106

5.


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

-"*
-"". , ; , .
. , . , , , , . , .
, , - ,
.
, -"", ,
. , , :
. ,
. , "" ,
, .

-

. Excel , . . , . ,
Excel, , , ,
, ( )
. , , .

. Excel

107

Power Utility ( ) -
Excel. , . , Excel. Web - .
, VBA-.
- .
, ,
, . - , Excel. , , , . , , .
- , . , Excel : Excel, , .
, . 6 16 , VBA
Excel.


, Excel , . , , .
, VBA.
, .
10
, .


(
), . , (, ),
. , . .
. VisiCalc ( ). , . , . , ,
,

, , ?

108

5.

"-"
, "-" ,
.
,
, . , "-" ( ). , ,
, " , ...?". , . ,
, .
"-" ,
, . , . , , ,
,



,
. ,,
,
. Excel 97, 65 536 . .
, .
, Excel ,
.
, , ,
,
, , Access.


.
Excel , , .
, ,
{executive information system EIS).
.
. Excel,
, .

/I. Excel

109

" "
. " ",
,
. ,
, .
, , , .
" ".
, .
.

. , ,
, . , .

110

5.

, , , ,
,
Excel. ,
,
.
,
" " .
, , , ,
. ,
, ( ) ,
.
,

.

,
.

, , .

(
).

, .

, . . ,
.


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

, .
, .

,
, .

, ( )
. , , , . , ,
.

, . , , , - ,
.

, ,
. , , .

, Excel ( Excel) . , Microsoft ,


, .
, Microsoft Office .
, .
.

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

112

6.

. , era
. , , , , , . , ( )
.

,

, , .. , .
. ,
. , , , .
, , ,
, . Excel!
, .
"" Excel
, , , , ,
, . , , Excel
. ,
, , .
,
, .
, .

. , :
, , .

. , . ,
.

VBA. , : VBA?
.

XLS. ,
.

Excel. Excel-: Excel 2002, ,


Excel 2000 Excel 97? Excel 95 Excel 5?
Macintosh? ,
Excel , .

. . , "" ,

IL Excel

113

. , , ,
.

. , Excel . Excel,
, .

. , ,
.

. , , , Excel
, . , , , ,

. , ,
.

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

. Excel . 18 24 . ,
, ,
, .
Excel 5, VBA, Excel
" " . &
, Excel, , , XLM,
Excel 2, 3 4. Excel 5, , .
Excel 97 "
". , Visual Basic, a
, . Excel 2000
Excel 2002 , , .
Excel 2002 , , " ". Microsoft , .NET, , Office .
, VBA VSA (Visual Studio for Applications).
VBA , ,
, .

114

6.

VBA . , , ,
VBA , . VBA , .
VBA, , VBA.
VBA .
, . , , ,
, ,
.
. . .
, DDE OLE.
, .
.



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

Excel: , ,
.. ,
, . , ,
, , , Excel. , , ( ) . , .
\
Excel ,
:

( );

(, , L i s t B o x ( ) CommandB u t t o n ( )), ;

,
.
Excel
Excel VBA, "".

U. Excel

115


- Excel, .
, .

Excel 97 , (UserForms).
, , Excel 5/95,
. ,
.

. 6.1 ,
.


,
,
. (
). Visual Basic (Visual Basic Editor VBE),
<AH+F11>. , ,
{ , , ..).
ActiveX.
, Excel. , .
,
, ( , ).
, , , . VB-.
. .].

IV.

ActiveX

Excel ActiveX, ,
. . 6.2 . , , , .

6.

,
, Web-
.
, CommandButton.
,
.

. ,
ActiveX .
,
, , .

. 6.2. ,
, ,

ActiveX
.
, , Excel 5/95.
ActiveX; .
. (. 6.1).

6.1. Excel
ActiveX
ActiveX

Excel

Excel

97, 2000, 2002

5, 95, 97, 2000, 2002

VBA-


[, CommandButton1_Click)

...

,
Properties

cl i c k ()
C h a n g e ()



. Excel
. ,

II. Excel

117

{ ) . ,
, , ,
.
Excel 97, Microsoft , . 22,
. . 6.3 , Excel.
Power Utility Pak.
- .
Excel. VBA ,
^ ^.
22,
VBA. VBA : () .
, ^
1 ^ (. 6.4), "". , (, ),
!

Excel 97, ,

>>>*

Excel 5. Excel 97 , , ,
. , , , , : Excel 5 ,
.

. 6.3.

. 6.4. Excel

, , Excel, ,
. , VBA (

118

6.

). . 6.5 ,
. , , .

. 6.5.
23.


Windows,
Excel . .
, , ,
. Excel
, .. .
, , , ,
- , , . , , . , , ,
.
Excel 97, .

, .
, ,
(. 6.6).
, .

U. Excel

119

. 6.6.

22.


, , . . Excel ,
<Ctrl> ( <Shift+Ctrl>). ,
. ,
, , .
, , , ,
.
, ,
. , <Ctrl+S> . ,
. , , ,
, Excel.
,
, , , <Ctrl+Shift+S>.


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

Excel, "'".

120


,
, .


""
? , , . , , . , , Excel, .
, . .
, ,
. ,
VBA , , , .
? Excel?
, , , , Excel,
, ( ) .
. Excel , . , , .
, Excel,
Microsoft ,
. , , , .
, . , . ,
, "" . . Excel
, .
Excel. , , ,
.
Excel - . ,
.
? ,
. , , , ,
, . ? . ? . .
.
.
.
1. , ( ) ,
.
2. , , .
. Excel

121

. , ,
. ,
1
. "'
Excel (. "? Excel?"),
,
, .
, . ,
, MOiyr
, , ( ).
, .
, , . ,
, ,
. , , <Ctrl+F6> . ? ,
, .
-?

.
.
-. , .
Excel, ,
, , , -.
( ) ,
.
-
. , . - , - ,
; , ,
.. - , , .
, ,
, . , - , ( ) .


. ,
. , , . , , , ,
.

122

6.

, , ( ) .
Excel .
, . (
.)
, ^ .
,
, . ^ ^ .
, , . (
.) ,
^ 1 ^ .
Excel
, Microsoft Excel . Excel . ,
. , Excel 2002
, . , ? Excel . , .
- , , ,
.
. (
.) ,
^ ^ .
( - ), { !
^), ( ^ ^ ) (=>
). ,
.
Excel ( ), .
.
^ .
, .
.
=? .
,
.
Excel 2002 ,
. * . , . , .
, .

//. Excel

123


, , ,
,
.
, .
. , Excel. , , . , ,
.
, . .
, , .
. , , ,
Excel. , , .

,
. , ,
- .

,
, , .
, , M u l t i P a g e ( ),
.

, }' .

, , ,
.

, , .

,

,
. , .
,
( , , - ). ,
.
, , .
Excel, , , , , , , ,

124

6.

. : .
?
. ,
. , , .
VBA, ( ,
, ). VBA , , , .
, : . Windows-. Excel-

.
, , , .
23
,


.
? . .
.
, ,
. .
.
,
VBA.
/
/Hi
'
****"

Microsoft Office (Microsoft Office Developers


Edition) ,
.

Excel 2000 Microsoft Authenticode,


" ".
,
, , .
, . (
, ). Web-
Microsoft.
,
. ,

II. Excel

125

? , , .
"" ,
.
Excel?
, , Excel. Excel . , , :
Microsoft Excel?
, , . , Excel (
).
, Microsoft Excel . : , - . , ,
,
.
... Microsoft Excel. Excel Excel. , . Web- Microsoft { h t t p : / /
o f f i c e u p d a t e . m i c r o s o f t . con).


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


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

126

6.

Excel,
Excel .
Excel 2002 , - Excel 95, Excel 97
.
, , , , ,
Excel 95, Excel. , Excel 95, Excel 97, Excel 2000 Excel 2002, (Excel 95),
.
, "" Excel. -,
Microsoft . , Excel 2000, Excel 2000 SR-1 Excel 2000 SR-2.
25.

,
, ,
Excel. ,
. .
25


,
. , ,
, .
, . , , . , , , . "", ,
.

&

VBA, , . -'4 ' | * " ". , ,


* .

Windows :
640x480 (.. VGA), 800x600 1024x768.
, VGA, , VGA-, .

II. Excel

127

VGA! , ,
, .
. , ,
800x600, VGA-
. , , (..
)
. , ,
, , VGA-.
, Windows
, VGA . ,

VGA. He , , . , , , , " " VGA,
( 10),
VBA. , , .


, ,
, .. . , , nporpaMNfa Excel
C:\PROGRAM FILES\MICROSOFT OFFICE\OFFICE. , :. , , Windows WINDOWS. , , . , , VBA.

.
.

128

6.

Visual Basic for


Applications

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

I Visual
Basic for Applications

Excel, ,
. , , Excel. , , Excel.

BASIC

BASIC . ( Beginner's All-purpose Symbolic Instruction Code
) , .
, BASIC 1960- . BASIC ,
.
BASIC . ,
.
,
. BASIC ,
, .
BASIC 1991 , Microsoft Visual Basic Windows (
6.0).
Windows. Visual Basic
BASIC, ,
VBA-

VBA
Excel 5 , Visual Basic for Applications (VBA). VBA
Microsoft, Office 2002 . , VBA Excel,
Microsoft (,
). , ,
.


VBA
. , VBA , (Excel, Word, Access, PowerPoint ..) . , .
, Excel , , , , , ,
, , .
VBA . VBA Excel
. .
, ,
!

VBA XLM
Excel 5 ( ) XLM. Excel
XLM, , Excel 97, XLM. XLM ( , , ),
VBA.
. 7.1 7.2 , XLM VBA. .
. , , VBA . ( ), VBA .

. 7.1. ,
XLM Excel,
. Visual Basic for Applications

131

. 7.2. , VBA Excel, VBA

VBA Lotus
Lotus I-2-3 ( DOS) . , . Lotus 1-2-3
DOS , . , Lotus 1-2-3 DOS,
, :
/RWC Lotus 1-2-3:
/Range Name C r e a t e

(Enter)

, ,
Lotus I-2-3 - . Lotus 1-2-3
, VBA (. ).

VBA LotusScript
Lotus 1-2-3 97, LotusScript ,
VBA. LotusScript, , VBA
.
VBA "" .
, ,
. ,
Lotus () ,
( ) VBA Microsoft.

VBA
, , ,
. , .
VBA.

VBA VBA.

( ) VBA, VBA.

132

7. Visual Basic for Applications

VBA .
, , , . T e s t : , :
Sub T e s t ( )
Sum = 1 + 1
MsgBox ": " & Sum
End Sub

Sub, VBA
.
( ). VBA .
AddTwo:
F u n c t i o n AddTwo(argl, arg2}
AddTwo = argl + arg2
End Function

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

.
. , Excel A p p l i c a t i o n , , , Workbook
( ). Workbook , ,
W o r k s h e e t ( ) C h a r t (). W o r k s h e e t , . R a n g e (), P i v o t T a b l e ( ) ..
Excel.

.
, W o r k s h e e t s , CommandBars CommandBar. .

, , -.
, 1 . 1
:
Application.Workbooks("KHMral.xls")
K H M r a l . x l s W o r k b o o k s .
Workbooks A p p l i c a t i o n . ,
1 1 . x l s :
Application.Workbooks("1.xls").Worksheets
("Jbtcrl")
, :
Application.Workbooks!"KHMral.xls").Worksheets("1").Range("Al")
Excel .
1 , :
Worksheets{"!").Range("Al")

III. Visual Basic for Applications

133

, 1 , :
Range("1")
.
. ,
, V a l u e () Name (), , H a s T i t l e () ().
VBA, .
.
, 1 1 :
Worksheets("!").Range("Al").Value

, . , , VBA. Excel .
Excel workbook ( ).
. Excel
, workbook
( Workbooks). Restaurant.
Excel , , ,
, , VBA .. ,
. , worksheet ( )
Range (), p i v o t t r a b l e ( ), Shape () ..
; ( )
, , Kitchen, DiningArea Tables (
). , Restaurant. , Tables .
. , Kitchen stove
(), V e n t i l a t i o n F a n (), chef (-), sink () ..
. , .
Excel . , Range
value Name, shape Width, Height ..
. ,
stove , Temperature NumberofBurners. V e n t i l a t i o n F a n (TurnedOn
(), RPM ( ) ..).
, Excel ,
. , ciearContents; Range.
, changeThermostat ( ) stove switchOn
() V e n t i l a t i o n F a n .
Excel . clearContents
Range Value Range, , ChangeThermos t a t stove Temperature.
VBA Excel. (" ").

134

7. Visual Basic for Applications

VBA. ( , .
Al 1 Interest,
VBA:
Interest = Worksheets("1").Range("Al").Value

.
, . ,
Range C l e a r C o n t e n t s . .

, .
, Al :
Range("Al").ClearContents

VBA (
, ..).

, VBA . ,
.

Visual Basic
Excel 5 Excel 95 VBA . Excel 97, VBA ,
VBA Visual Basic
( V B E Visual Basic Editor).
VBA ;
. VBE.

V B E , Excel.
VBE, . VBE
Excel; VBE Excel.

VBE
Excel VBE
.

<Alt+Fll>.
^^ Visual Basic.
Visual Basic,
Visual Basic.
Visual Basic Editor Microsoft Script
Editor. . HTML, VBScript JavaScript. .

///. Visual Basic for Applications

135

. 7.3 Visual Basic. , VBE


, . :
, , , ..

. 7.3. Microsoft Visual Basic

VBE
VBE . Visual Basic.


VBE, , , .
, VBE.
, . , View^Immediate Window (^ ) <Ctrl+G>.
^

VBE .
VBE, ,
.


Standard, , , VBE (
). VBE , Excel:
, , .. VBE 1
View^Toolbars^Customize ( ^).

Project Explorer
Project Explorer -,
Excel (
). .

136

7. Visual Basic for Applications

Project Explorer ("


Project Explorer").
Visual Basic Project Explorer , <Ctrl+R>.
, (
Hide ).


( Module) VBA.
. ,
Project Explorer. ,
1 , 1 Project Explorer.
VB -, .

Project Explorer, View Code ( ) Project Explorer.
" '.

Immediate
Immediate VBA,
. .
Immediate , <Ctrl+G>. Immediate, (
Hide ).

Project Explorer
Visual Basic Excel

. , . , ""
Project Explorer.
"" . , Toggle
Folders ( ) Project
Explorer. , ,
.
. 7.4. Project Exp . 7.4 Project Explorer - lorer ( ).

VBE
, Project Explorer. , , Project
Explorer.
Excel , Project Explorer
. , Project Explorer . -

. Visual Basic for Applications

137

,
.

M i c r o s o f t E x c e l O b j e c t s .
( ),
, A c t i v e W o r k b o o k .
VBA, M o d u l e s , .
Forms, U s e r F o r m ( , ). , C l a s s M o d u l e s .
Excel 2002 { Tools^References) : References.
. , ,
,

VBA
VBA, Project
Explorer Insert^Module (^). *
Insert 6 *Module .
Excel VBA .

VBA
VBA ,
Project Explorer F i l e ^ R e m o v e xxx ( ). ,
Remove xxx .
.
, (
), (, 1).


, R e f e r e n c e s ,
.
. , .
, (, VBA U s e r F o r m ) .
, Project Explorer
File^Export File ( <Ctrl+E>). , . , ( ). U s e r F o r m ,
, U s e r F o r m .
, Project Explorer FileOlrnport File. ,

138

7. Visual Basic for Applications

. ,
F\\e*>Export File.
userFonn,
. , , Project Explorer
.


VBA, ,
. . :

( Project Explorer);

(, 1 1
Project);

VBA;

( , );

U s e r F o r m .


VBE , . . 7.5 .

. 7.5.

III. Visual Basic for Applications

139

Excel. ,
, , ..
, , , . , . ,
.
, . ,
.
, . ,
, .
, Project Explorer.
VBE . Excel
. Immediate, . Immediate,
VBA { ) <Enter>:
Workbooks ( " i n y a d d i n . x l a " ) . C l o s e
, C l o s e Workbook, . .

VBA
, .

( Sub). , .

. ,
( VB Excel).

. , .
, VBA. , , .

VBA , . VBA .
VBA VBA;
.

, , . . Excel
. :
sub ( ),
. . ,
sub . 9 10,
,
VBA, . , . , ,

140

7. Visual Basic for Applications

, .
,
( 19) UserForm ( IV).

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

: .

Excel, VBA.
, .


. ... , .. .
<> ,
(, If End I f ) .
, , .
"' ".
VBA .
, , , .
VBA . , , .
,
<Enter> . , VBA, .
MsgBox " " & UCase(SHORTCUTMENUFILE) _
& vbCrLf & vbCrLf & " _
" & T h i s W o r k b o o k . P a t h & vbCrLf & vbCrLf & _
", BudgetMan", v b C r i t i c a l ,

APPNAME

, .
,
.
|

Excel, VBE . , ,
Undo () <Ctrl+Z>, .
Redo (),
, .
, .

: VBA, :

III. Visual Basic for Applications

141

Sub SayHelloO
Msg = " " & Application.UserName & " ? "
Ans = MsgBox(Msgr vbYesNo)
If Ans = vbMo Then
MsgBox " ."
Else
MsgBox ", !"
End If
End Sub
. 7.6 , VBA.

. 7.6. VBA
, VBE
. ,
(=}, VBE . , . , .
S a y H e l l o ,
. .

<F5>.
Rurr^Run Sub/UserForm.

Run Sub/UserForm .
, . (. 1.1), ,
. Excel. ,
Excel.
;
, .
. 7.7. , . 7.6
Excel. , VBE.

VBA (
). , VBE

142

7. Visual Basic for Applications

. , VBE, a Excel , . , .
(
).

( ).

(Msg Ans).

( &).

VBA (MsgBox).

VBA (vbYesNo vbNo).

I f - T h e n - E l s e .

( ).

, ?


VBA Excel.
, S a y H e l l o . , .
.
, ,
. , .
1. ( ).
2.

^^ .
Excel .

3. , .
Excel VBA .
Excel, VBA.
, ,
,
(

).
4.

^ .
Excel .

5. , 1 .
6. (
<=> ).
Excel .
, VBE ( <Alt+Fl 1>)
Project Explorer. Modules, ,
Module 1, ( Module"!, Module2). ,

III. Visual Basic for Applications

143

, 7.1. Excel 2002, , .

7.1.
Sub 1{)
' 1
1
19.08.2003

With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
n
.RightHeader = "
.LeftFooter = ""
.CenterFooter = "
.RightFooter = ""
.LeftMargin = Application nchesToPoints(0.787401575)
.RightMargin = Application.InchesToPoints(0.787401575)
.TopMargin = Application.InchesToPoints(0.984251969)
.EottomMargin = Application.InchesToPoints(0.984251969)
. HeaderMargin = Application. InchesToPoints (0.5)
.FooterMargin = Applicatior.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
PrintQuality = 1200
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize xlPaperA4
.FirstPageNumber - xlAutoma tic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
End With
End Sub
, ,
( ). ,
, Excel ,
.
, , , .
, ,
, .
, .
:

144

7. Visual Basic for Applications

Sub 1
With ActiveSheet.PageSetup
, O r i e n t a t i o n = xlLandscape
End With
End Sub
, , O r i e n t a t i o n . , With-End With
.
Sub 1
ActiveSheet.PageSetup.Orientation = xlLandscape
End Sub
O r i e n t a t i o n PageSetup . , x l L a n d s c a p e , . xlLandscape 2,
a x l P o r t r a i t 1. , ].;
Sub 1
ActiveSheet.PageSetup.Orientation = 2
End Sub
, .
, .
VBA, , , . , . ,
P a g e S e t u p O r i e n t a t i o n .
, VBA. , . , ,
, . , , , .
- . " ".

VBA

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

III. Visual Basic tot Applications

145

VBE
Excel ,
VBE. , .
VBE Tools^Options (). : Editor (), Editor Format ( ). General () Docking (). . , Excel,
Excel ^ . , , .

Editor
. 7.8 ,
Editor Options.

Auto Syntax Check


Auto Syntax Check ( ) ,
, VBE VBA.
. ,
VBE ,

, , .
- . 7.8. Editor Options
Auto Syntax Check,
, , . VBA,
.

Require Variable Declaration


Require Variable Declaration ( ) VBE ; :
Option Explicit
,
. , , , ,
. ,
V a r i a n t ; ,
(. ),

146

Require Variable Declaration , .

7. Visual Basic for Applications

Auto List Members


Auto List Members ( ), VBE VBA, .
, .
, .
. 7.9 Auto List Members (
, VBA ). VBE A p p l i c a t i o n . ,
{ ).

. 7.9. Auto List Members

Auto Quick Info


Auto Quick Info ( ), VBE , , .
, .
. 7.10 : Range.

. 7.10. Auto Quick Info, Range

Auto Data Tips


Auto Data Tips, VBE , .

. Visual Basic for Applications

147

VBE, .
.

Auto Indent
Auto Indent ( ) , VBE ,
. , ,
. (
4).
<>, , , ,

<Shift+Tab>, .

Drag-and-Drop Text Editing


Drag-and-Drop Text Editing ( ) , . , , , .

Default to Full Module View


Default to Full Module View ( ) . . ,
.
.

Procedure Separator
Procedure Separator ( ) ,
. , .

Editor Format
. 7.11 Editor Format Options.

|1 . 7.11. Editor Format


Options

148

7. Visual Basic for Applications

Code Colors
Code Colors ( )
( ) , VBA. , , . , . .

Font
Font () , VBA.
(, Courier New). , , , .

Size
Size () VBA. , , ,
. 10.

Margin Indicator Bar


, . ; .

General
. 7.12 , General ()
Options. .
Error Trapping ( ) , . , ,
Break on Unhandled Errors ( ). Break on All Errors
( ) ( ).
9,

Docking
. 7.13 Docking Options. VBE ,
. ,
VBE. , . ,
, . ,
.

HI. Visual Basic for Applications

149

,. 7.12. General Options . 7.13. Docking Options



, Excel VBA. .
Visual Etasic Excel
. , ,
Visual Basic.
,
.
.

, (.. ), , , ..

, .


, Excel , , VBA.
, . .
1. .
2. , Excel . , .
3. <Alt+FIl>, VBE. ,
.

150

7. Visual Basic for Applications

4. Excel VBE , . (
.)
5. Excel, ^ ^ , .
Excel ( M o d u l e l ) .
6.

VBE.

7. Project Explorer M o d u l e l , .
8. Project Explorer, .
Visual Basic , . 7.14. .

, 7.14.
, Excel. ,
, VBA.
: , , .
, , ..
, .

?
Excel
. , Excel (

///. Visual Basic for Applications

151

, ). , . .
1. .
2. 1.
3. 1 .
4. 1 .
5. , B l : G l
.
6. 1, .
7. .
Excel ;
Sub M a c r o ( )
Range("Bl").Select
ActiveCell ..FormulaRlCl =
Range{"Cl").Select
ActiveCell.FormulaRlCl =
Range("Dl").Select
ActiveCell.FormulaRlCl =
Range("El").Select
ActiveCell.FormulaRlCl =
Range("Fl").Select
ActiveCell.FormulaRlCl =
Range("Gl").Select
ActiveCell.FormulaRlCl =
Range{"Bl").Select
End Sub

""
""
"Map"
"Anp"
""
""

, ^^ ( <AJt+F8>), 1 ( ) .
, . , .

.
, . ,
. .
,
. Excel, . . , . , . ,
.
, B l : D1,
.
1. 1.
2.

152

^^ .

7. Visual Basic for Applications

3. .
4. , .
5. (
), .
.
6. B1:G1 (
).
7. 1.
8. .
, Excel :
Sub M a c r o 2 ( )
A c t i v e C e l l . F o r m u l a R l C l = ""
ActiveCell.Offset(0,
1).Range("Al").Select
A c t i v e C e l l . F o r m u l a R l C l = ""
ActiveCell.OffSet(0,
1).Range("Al").Select
A c t i v e C e l l . F o r m u l a R l C l = "Map"
ActiveCell.OffSet(0,
1).Range("Al").Select
A c t i v e C e l l . F o r m u l a R l C l - "Anp"
ActiveCell.OffSet(0,
1).Range("Al").Select
A c t i v e C e l l . F o r m u l a R l C l ""
ActiveCell.OffSetfO,
1).Range("Al").Select
A c t i v e C e l l - F o r m u l a R l C l = "
ActiveCell.OffSet(0,
1).Range("Al").Select
End Sub
,
^ . .
,
( ). , .
, ,
. . ( ,
, .) O f f s e t .
.. , .
, ,
Al , Al . .
( o f f s e t .)
, , .
, , Excel, , ,
. ,
,
. , \;
, .
Sub
ActiveCell.Offset(0, 0) = ""
ActiveCell.Offset(0, 1) = ""

Hi. Visual Basic for Applications

~~Is3

ActiveCell.offset(0,
ActiveCell.OffsetlO,
ActiveCell.Offset(0,
ActiveCell.offsetlO,
End Sub

2)
3)
4)
5)

= "Map1
= "Anp"
= ""
= ""

, With-End With:
Sub Macros ()
With ActiveCell
.OffsetfO, 0) =
.OffsetlO, 1) =
.Offset(0, 2)
.OffsetlO, 3) =
.Ofset(0, 4) =
.OffsetlO, 5) =
End Sub

""
""
"Map"
"Anp"
""
""

VBA ( ), ,
:
Sub Macro54()
ActiveCell.Resize* , )=( "" , "", "Map", "Anp", "", "")
End Sub
, , , . " , .


VBA . , ^ ^
.
. .


, .
Excel 1, 2 ..
.
.


. , w ( ), , <Ctri+W>. W ( ) <Ctrl+Shift+W>.
, .


Excel, , . Excel .
(Excel
), .

154

7. Visual Basic for Applications


VBA, ,
. P e r s o n a l . x l s , x l s t a r t . . P e r s o n a l . x l s , .

Excel ( ),
, .
, .
, .
Excel, , Excel 97,
,
. Excel, ,
, (. 23).


, , ( ^ ) VBA.
,
.
, . , , , . Excel
, (.. ), S e l e c t i o n .
, (
) .
Range("Al:5").Select
Selection.NumberFormat = "# ,##0 .00"
Selection.Font.Bold = True
Selection.Font.Italic = True
, , Excel . .
. W i t h - E n d With:
Range("A1:C5").Select
With Selection
i
.NumberFormat = "#,##0,00"
.Font.Bold = True
. F o n t . I t a l i c = True
End With
S e l e c t :
With Range(UA1:C5M
.NumberFormat =

"#,##0.00"

III. Visual Basic for Applications

W5

.Font.Bold = True
. F o n t . I t a l i c = True
End With
, VBA,
.
, VBA,
. , VBA
.


VBA, .
.
, .
.
Range("Al}.Value
, . MsgBox:
MsgBox R a n g e ( " A l " ) . V a l u e
, VBA
:
Sub Test()
1

End Sub
<F5>, .
, , . , 1, ,
1.
.
Immediate, Immediate
. Immediate , ,
VBE, <Ctrl+G>.
Immediate VBA <Enter>.
Immediate, {?).
P r i n t . , Immediate :
? Range{"Al").Value
Immediate.


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

156

7. Visual Basic for Applications

A p p l i c a t i o n . Excel.
VBA, VBE Microsoft Word, A p p l i c a t i o n
Word.


A p p l i c a t i o n ( Excel) . , A p p l i c a t i o n :
Workbooks ( Workbook );
Windows ( window );
Addlns ( Addln ).
. , Workbooks
Workbook, Workbook , :
Worksheets ( Worksheet );
Charts ( Chart );
Names ( Name ).
, , .
Worksheets Worksheet Workbook.
Worksheet , :
ChartObjects ( ChartQbject );
Range ;
PageSetup ;
PivotTables ( PivotTable ).
, ,
, . , Excel .


VBA .
( ).
. Workbooks Workbook. Worksheets Worksheet, Workbook.
. , :
Worksheets("!")
1 , .
Worksheets(1)
Workbook Worksheets 12) ..
, Sheets, , . 1 , :
Sheets(1)
. Visual Basic for Applications

157


VBA, , . , Excel , 1?
:
Workbooks("1")-Worksheets("1"}
Visual Basic ! ,
(, 1) 1 1 , :
Workbooks{"!").Worksheets("!").Range("Al")
A p p l i c a t i o n :
Application.Workbooks("1").Worksheets("!").Range("Al")
A p p l i c a t i o n {
). 1 ,
:
Worksheets("1").Range("Al")
1 , :
Range("A1")

6 Excel .
Range, .

( ) .
, ,
.


: . , .


. , Range V a l u e . VBA, V a l u e V a l u e . ,
VBA MsgBox , Al
1 .

Sub ShowValueO
MsgBox Worksheets{"1").Range("Al").Value
End Sub

158

7. Visuaf Basic for Applications

V a l u e
1 1 . ,
1 , .

MsgBox ,
VBA. .

, V a l u e ?
1 V a l u e .
Sub ChangeValue(1
Worksheets("1).Range("Al").Value = 123
End Sub
1 1 123.
.

. Range

Value. , .value
, .
, .

, . , . c l e a r
. 1 :
1 , .
Sub ZapRange()
Worksheets("1").Rangei"Al:C3").Clear
End Sub
, , C l e a r C o n t e n t s Range.
, . , 1 ]
Range- ( , ). * ( ).
, .
Sub CopyOne()
Worksheets("1").Range("Al").Copy _
Worksheets("1").Range("Bl")
End Sub

VBA .
;
. ,
,
. , ,

III. Visual Basic for Applications

159

. p r o t e c t .
, Protect : , , .
.
, MyBook.xls, :
Workbooks("MyBook.xls").Protect "xyzzy", True, False
( 1).
{ 2), ( 3).
, :
Workbooks{"MyBook.xls").Protect , True, False
, , .
( ) . .
Workbooks{"MyBook.xls").Protect S t r u c t u r e : = T r u e , Windows:=False
,
,
. .
, , . , Address Range .
, :
MsgBox Range("Al").Address False
'
:
MsgBox Range("Al").Address(False)
, .
MsgBox Range("Al").Address(rowAbsolute:=False)
, , .

Comment:
, Comment. Comment Excel
^ .
. , .
.

Comment
. . 7.15 Comment.
, , . , Properties, Comment,
Methods, .

160

7. Visual Basic for Applications

. 7.15. Comment

,
<F1>. , , . 7.15.
, , , , , , .
Comment <F1>.
comment ,
. , Comment; , Comment,

Comment
Comment . . 7.1
. , , VBA , .
7 . 1 . Comment

A p p l i c a t i o n

, , (.. Excel)

Author

creator

parent

, , . Excel Windows ( Excel


Macintosh)
(
Range)

///. Visual Basic for Applications

161

. . 1

shape

shape, ,

visible

T r u e ,

Comment
. 7.2 , Comment.
, ...
.

7.2. Comment

Delete

Next

comment,

Previous

comment,

Text

( )
, , T e x t , . :
, .
, .
, ,
.

Comments
, .
Comments, Comment .
, .
, 1
Worksheets{"1").Comments(1)
,
1 :
MsgBox W o r k s h e e t s ( " 1 " } . C o m m e n t s ( 1 ) . T e x t
or , Comment Name. , , ,
Comment R a n g e (. ).
Comments , .
, :
MsgBox A c t i v e S h e e t . C o m m e n t s . C o u n t

162

7. Visual Basic for Applications

C o u n t Comments, Comment .
, :
MsgBox ActiveSheet.Comments(1).Parent-Address
Comments (1) Comment Comments.
P a r e n t Comment ,
R a n g e . A d d r e s s Range. , .
, , F o r E a c h - N e x t ( 8).
:
For Each cmt in ActiveSheet.Comments
MsgBox cmt.Text
Next cmt
,
,
Intermediate:
For Each cmt in ActiveSheet.Comments
Debug.Print cmt.Text
Next cmt

Comment
Comment. ,
R a n g e Comment. , Comment
Comment. ,
Comment Al:
Range("Al").Comment
, Comment
:
Comments(1)
Al ,
MsgBox

Range("Al").Comment.Text

Al , .
, , (,
, VBA).

, Comment
, . , 1. Comment, , . .

///. Visual Basic for Applications

163

1. Shape Comment, Shape, .


2. F i l l Shape, F i l l F o r m a t .
3. ForeColor F i l l F o r m a t ,
ColorFormat,
4. RGB ( SchemeColor) ColorFormat,
.
, Comment , . .
A p p l i c a t i o n (Excel)
Workbook
Worksheet
Comment
Shape
FillFormat
ColorFormat
, ! "" VBA , :
Worksheets("1").Comments(I).Shape.Fill.ForeColor _
.RGB = RGB(0, 2 55, 0)
S c h e m e C o l o r ( 0 80):
Worksheets("1").Comments(1).Shape.Fill.ForeColor _
. S c h e m e C o l o r = 12
,
. , , , Excel .

?
VBA , . : Excel
56- , . ,
( , ). VBA? , VBA, , .
. ,
, .
Shape RGB SchemeColor.
RGB , . RGB, , 0 255. RGB 0
16777215. Excel, , 56 . , RGB,
56- . SchemeColor 80. ,

764

7. Visual Basic for Applications

.
.
Range
i n t e r i o r . : Color
Color Index. c o l o r i n d e x 0 56
( ). . , , ,
c o l o r i n d e x , ColorIndex , , .
Colorindex , .
Color
RGB. ,
, .
, , TextFrame
Comment, C h a r a c t e r s , , ,
Font. Color C o l o r i n d e x Font.
, C o l o r i n d e x 5:
Worksheets ("1") . Comments (1) _
.Shape.TextFrame.Characters.Font.Colorindex = 5


1 :
MsgBox Range("Al").Comment.Text
1 , : O b j e c t v a r i a b l e o r With b l o c k v a r i a b l e n o t s e t .
, , , , Comment, .. N o t h i n g (
VBA). T r u e , 1 :
MsgBox R a n g e ( " A l " ) . C o m m e n t I s N o t h i n g
, I s , .

Comment
, , Comment
. , AddComment R a n g e . () 1 :
Range("Al").AddComment
, , AddComment , . ,
:
Range{"Al").AddComment " JW"

AddComment , .

III. Visual Basic for Applications

165

comment , Web-. Comment


V8A. , , ,
VBA .

Application
, Excel . , (
).
VBA , . , , ,
. VBA A p p l i c a t i o n
. , A p p l i c a t i o n A c t i v e C e l l , . 1 :
ActiveCell-Value = 1
, A p p l i c a t i o n ,
. , ,
. , VBA , , , .
,
. , ( ).
A p p l i c a t i o n S e l e c t i o n ,
, .. (),
C h a r t O b j e c t , TextBox Shape.
. 7.3 A p p l i c a t i o n ,
.

7.3. Application .

ActiveCell
ActiveChart
Activesheet
Activewindow



. , N o t h i n g
{ )

ActiveWorkbook
R a n g e S e i e c t i o n , ( window)
Selection

( Range, Shape, C h a r t O b j e c t ..)

Thisworkbook

,
, ,

166

7. Visual Basic for Applications

. VBA, , . , , :

ActiveCell.ClearContents
, :
MsgBox ActiveSheet.Narae
, :
MsgBox ActiveBook.Name
, ,
. S e l e c t i o n
A p p l i c a t i o n Range, .
Value Range, , .
Selection.Value = 12
: (, ChartObject
Shape), , ChartObject Snaps
Value.
, Range,
( ), 12. , RangeSelection Window:
ActiveWindow.RangeSelection.Value = 12
, , Count:
MsgBox ActiveWindow.RangeSelection.Count

Range
, , VBA, , .
VBA
, .
Range Worksheet . Range VB .
Range Worksheet Range.
C e l l s Worksheet.
Offset Range.

Range
Range Range. Range
, :
.Range(!);
.Range(!,
2).
Range : Worksheet Range. ! 2 , Excel

III. Visual Basic for Applications

167

( )
( ). Range.
, .
, : 1
1 1 :
Worksheets("!").Range("Al").Value = 1
Range , .
,
Worksheets("1").Range("").Value = 1
20- . , :
ActiveSheet.Range("A1:B1O").Value = 2
, .
Range("Al",

"BIO")

= 2

, ,
. , ,
( Range V a l u e ) .
Range.
, .

Excel (). . , 3 :
Range("1:10 :") 3
, 4 , .. . :
Range("Al,A3,5,7,9") = 4
Range
W o r k s h e e t . Range Range. , .
Range Range (
Range ). Range
, ,
2, 5. ,
Range. , 5
, :
ActiveCell.RangeCB211) = 5

O f f s e t (. ).

Cells
C e l l s .
Range, C e l l s Worksheet Range. C e l l s :

168

7. Visual Basic for Applications

.Cells(_,
_);
. C e l l s {1_);
. Cells.
C e l l s .
Al 1 9. ,
( 1 65536) ( 1 256):
Worksheets("1").Cells(1, 1) = 9
, 7 D3 (..
3, 4) :
A c t i v e S h e e t . C e l l s ( 3 , 4) = 7
C e l l s Range. Rai.ge,
C e l l s , Range, . ? . . 5 . , Al :
A c t i v e C e l l . C e l l s ( 1 f 1) = 5
, (. 8).
.
.
5 , ,
:
ActiveCell.Cells{2, 1) = 5
; , Al. .
C e l l s ,
1 16777216. (65536 256 ). , Al ,
. 256- XVI. 257- 2.
(520- ) 2:
ActiveSheet.Cells<520) = 2
(IV65536),

MsgBox ActiveSheet.Cells(16777216}
Range.
Range. , Range Al; D1Q (40 ), C e l l s I 40
Range. 2000 2,
2 ( , ) :
Range("Al:D10").Cells(5) = 2000
C e l l s 1 40. , , , , ,
, , , A l : D10.

1)1. Visual Basic for Applications

169

C e l l s .
, *;,
, . ClearContents
, C e l l s . :
ActiveSheet.Cells.ClearContents

Offset
Offset ( Range Cells) Range.
, Offset Range
. :
.Offset{_,
_)
Offset
Range. (
), ( ) .
12 , :
A c t i v e C e l l . O f f s e t ( l , 0 ) . V a l u e = 12
15 :
ActiveCell.Qffset(-l,O).Value = 15
1, o f f s e t
, Range.
Offset (. ).
Excel
o f f s e t (..
). , . ( 1),
1: , 1:
Sub MacrolO
ActiveCell.FormulaRlCl = " 1 "
A c t i v e C e l l . O f f s e t d , 0) .Range ("Al") . S e l e c t
ActiveCell.FormulaRlCl = "2"
A c t i v e C e l l . O f f s e t d , 0} .Range {"Al") .Select
ActiveCell.FormulaRlCl = "3"
ActiveCell.Offset(-2,
0).Range("Al").Select
End Sub
FormulaRlCl. , Value. FormulaRlCl
Formula .
, Al,
, . , .
Range "1" ), :
Sub Modified MacrolO
ActiveCell.FormulaRlCl = " 1 "
A c t i v e C e l l . O f f s e t d , 0) .Select
ActiveCell.FormulaRlCl = "2"

170

7. Visual Basic for Applications

ActiveCell.OffsetCL, 0).Select
ActiveCell.FormulaRlCl = "3"
ActiveCell.Ofset(-2, 0).select
End Sub
( ,
), :
Sub M a c r o l ()
ActiveCell = 1
ActiveCell.Offsetd. 0) 2
ActiveCell.Offset(-2, 0) = 3
End Sub


( ), . .

,
, ,
VBA. VBA, .

.
.
(. Name) (, D e l e t e ) .

.
Excel,
XLM. ,
, .
Excel . ,
.
.
, , . , Workbook Myf i l e Workbooks :
Workbooks("My f i 1 e . x l s " )
, .
. , F o n t F o n t , Range:
R a n g e ( " A l " ) . F o n t . B o l d = True

,
.
, S a l e s ,
. Summary.
:

///. Visual Basic for Applications

171

Workbooks(' Sales.xls").Worksheets("Summary">
Workbooks(1).Worksheets(1)
Workbooks(1).Sheets{1)
Application.ActiveWorkbook.ActiveSheet
ActiveWorkbook.ActiveSheet
ActiveSheet
,
. , , . { ),
. ,
, .


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


, " Visual Basic for Applications". , .


, V B A , Excel. ,
, . ,
VBA, .


, Excel
.

. 7.16. VBE

172

7. Visual Basic for Applications

. 7.16 V a l u e . , S e e Also (. ). Applies To ( ) Example (). S e e Also,


( ). Applies To , , . Example,
(
VBA, ).


Object Browser ( ) ,
. VBE Object Browser
.

<F2>.

View^Object Browser.

Object Browser .

Object Browser . 7.17.

/. 7. /7.
Object Browser
, ,

Excel.

MSForms ( ).

. Visual Basic for Applications

173

O f f i c e (, Microsoft Office).

S t d o l e ( OLE).

VBA.
( ,
).

, C l a s s e s (), C l a s s e s Members of
( ).
,
, . ,
.
, , .
1. . , , <AII Libraries>.
2. Comment .
3. , .
Search Results ( ) , . , C l a s s e s . , (, ).
, . <F1>, .
Object Browser , ,
.

Immediate
,
Immediate VBE
VBA. Immediate, .

VBA,
. , VBA , VBA
, . ,
VBA.
8 , VBA.

174

7. Visual Basic for Applications

VBA

VBA,
.

VBA. ,
. VBA , .

VBA.
7 , .

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

Sub VBA_Demo()
1
VBA
Dim Total As Integer, i As Integer
Total = 0
For i = 1 To 100
Total = Total + i
Next i
MsgBox Total
End Sub

, ( ), ( T o t a l ) ,
( T o t a l = 0 T o t a l = T o t a l + i ) , ( F o r - N e x t ) VBA (MsgBox).
.

VBA . , ,

, . VBA . ( ).
. . VBA
, , . , , :
Msg = "Can't c o n t i n u e "
VBA :
Sub Comments()
1

=
'
'

MsgiBox
End Sub
, , Rem. :
Rem -
R e m , , BASIC;
VBA . , Rem
, .
,
. , , .
. , , :
Sub BadComments{)
1

Dim As Integer
Dim y As Integer
Dim z As Integer
'

x = 100 ' 100
= 2 00 ' 2 00
1
z
z = +
1

MsgBox z
End Sub

176

8. VBA

VBA
VBA, VBA. . . , . :
Sub OneLine()
= 1 : = 2 : z = 3 : MsgBox x + + z
End Sub
, ,
:
Sub OneLine(!
=1
=2
z=3
MsgBox + + z
End Sub
; VBA ,
. VBA: (_). ,
Sub LongLine()
SummedValue = _
Worksheets("1").Range{"Al").Value + _
Worksheets("2"}.Range("Al").value
End Sub
Excel .
Visual Basic
.
. , Ans=l+2 (
), VBE .
Ans = 1 + 2
VBA , .
.
Result=activesheet.range("al").value=12,
VBA :
Result = ActiveSheet.Rangef"al").Value = 12
, ( , "al") .
VBA ,
,
, . ^
, xaKmyvalue (
) MyValue ( ), VBA
MyValue. ,
, Dim
, , .
VBE . VBE
, ,
. Tools^Options VBE,
Options. , ?
( Editor Format) ( Auto Syntax Check Editor).
///. Visual Basic for Applications

177

. , :
;
, .
, ;
( , , );
, Excel
.
, , .
, , . , , , .
VBA () .
, .
Edit VBE .
, Comment Block,
. Uncomment Block
. , .

,
VBA . , , . .
. , , (True F a l s e ) (.
). (
),
, . , VBA , .
, , .
VBA . , (, I n t e r e s t R a t e ,
interestrate).
.
, ( I n t e r e s t R a t e ) .
(#, $, %, & I)
.
254
!

178

8. VBA

, .
. , .
=1
InterestRate 0.075
LoanPayoffAmount = 234089
DataEntered = False
x = x + 1
MyNum = YourNum * 1.25
UserName = "Bob Johnson"
DateStarted = #3/14/98#
VBA , .. ,
. , . , , N e x t
, :
Next = 1 3 2
, .
: C o m p i l e e r r o r : E x p e c t e d
V a r i a b l e ( : ). , ,
, VBA .


VBA , .

. , , .
.
, : , , .. VBA ,
:
. , VBA ,
.
, ,
. , VBA .
. 8.1 VBA ( , ,
).

III. Visual Basic for Applications

179

. 8.!

Integer
Long

-32768 32767

Single

2
4
4

Double

-1,79769313486232308 -4,94065645841247-324
( ); 4.94065645841247-324
1,79769313486232308 ( )

Currency
Decimal

8
14

-922337203685477,5808 922337203685477,5807

Date
object

8
4

1 100 31 9999

s t r i n g ( )

10 +

0 2 .

s t r i n g ( )

1 65400

V a r i a n t () 16

-2147483648 2147483647
-3,40282338 -1.401298-45 ( ); 1,401298-45 3.40282338 (
)

+/-79228162514264337593543950335 ;
+/-7,9228162514264337593543950335 28-

Double.
V a r i a n t (-

22 +

0 2 .

Decimal Excel 2000 . ,


. "" V a r i a n t . v a r i a n t (Decimal) VBA CDec
, ,
. VBA
, VBA.
, , VBA
.
Excel Double, VBA
.
I n t e g e r , , , , 32767.
Long. Excel Long,
, i n t e g e r .

180

8. VBA


, VBA,
V a r i a n t . , V a r i a n t ,
: , , . , :
Sub V a r i a n t D e m o ( )
MyVar " 1 2 3 "
MyVar = MyVar / 2
MyVar = " : " & MyVar
MsgBox MyVar
End Sub
V a r i a n t D e m o MyVar . "" ,
MyVar , MyVar .
MsgBox : : 61,5.
, 1
V a r i a n t , ) :
Sub V a r i a n t D e m o 2 ()
MyVar = "12 3"
MyVar = MyVar + MyVar
MyVar = ": " & MyVar
MsgBox MyVar
End Sub
. : 123123.
, , .
, V a r i a n t , + .


VBA TypeName.
.
MyVar. , , D o u b l e .
Sub VariantDemo2{)
MyVar - " 1 2 3 "
MsgBox TypeName(MyVar)
MyVar = MyVar / 2
MsgBox TypeKame(MyVar)
MyVar = ": " & MyVar
MsgBox TypeName(MyVar)
MsgBox MyVar
End Sub
VBA, . ,
, .

III. Visual Basic for Applications

181


, ,
, .
Sub TimeTest()
Dim x As Integer, As Integer
Dim A As Integer, As Integer, As Integer
Dim i As Integer, j As Integer
Dim StartTime As Date, EndTime As Date
1

StartTime = Timer
1

=
=
For i = 1 5000
For j = 1 1000
= + + i
= - - i
= - - i
Next j
Next i
1

EndTime - Timer
1

MsgBox Format(EndTime - StartTime, "0.0')
End Sub
4,0 ( , ,
). Dim,
(.. Dim , ), VBA v a r i a n t .
. 6,3
.
, , VBA- ,
!

. VBA .
.

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

, . , O p t i o n E x p l i c i t ,
(. ).
, C u r r e n t R a t e . C u r e n t R a t e = 0 . 7 5 . , , , .

182

8. VBA


,
VBA:
Option. E x p l i c i t
,
. VBA ,
.
o p t i o n E x p l i c i t VBA, Require Variable Declaration (
) Editor Options
VBE. .


, > . .

D i m s t a t i c

D i m P r i v a t e

P u b l i c

.
,

VBA,
.
. , . , ""
. ,
.


, . , .
, Excel .
, s t a t i c (. " s t a t i c " ) .
Dim
Sub End Sub. Dim Sub, .
Dim, ,
Dimension (). BASIC

. Visual Basic for Applications

183

. VBA Dim , .
, Dim:
Sub MySub{)
Dim x As Integer
Dim First As Long
Dim InterestRate As Single
DimTodaysDate As Single
Dim UserName As String * 20
Dim MyValue
'
-[ ] -

End Sub

, Dim , . V a r i a b l e .
, , Dim:
Dim x As Integer, y As Integer, z As Integer
Dim First As Long, Last As Double

BASIC, VBA , . , MyVar , %:
Dim MyVar%
VBA
{ 1 ).


%
Integer
&
Long
!
Single
#
Double
Currency
@
String
S
BASIC;
, .
, VBA , . , , , i n t e g e r ;
Dim i, j, k As Integer
VBA ,
Variant. i, j i n t e g e r ,
Dim i As I n t e g e r , j As I n t e g e r , k As I n t e g e r
,
,
.

184

ft VBA

, , VBA
, , .


, . (
).
D i m . MySuta YourSub C u r r e n t V a l u e .
Dim C u r r e n t V a l u e As I n t e g e r
Sub MySubO
1
-[ ] End Sub
Sub YourSub()
1
-[ ] End Sub
.

Public
VBA , Publ i c , Dim:
P u b l i c C u r r e n t R a t e a s Long
P u b l i c C u r r e n t R a t e
, , . . , VBA, .

Static
S t a t i c . .
Sub MySub()
Static Counter As Integer
-[ ] End Sub


( ""). , .


Const. :
Const NumQuarters as I n t e g e r = 4
Const Rate = .0725, Period = 12
Const ModName as S t r i n g = "Budget Macros"
Public Const AppName as S t r i n g = "Budget Application"

///. Visual Basic for Applications

185

. ,
V a r i a n t . ,
.
, . ,
, S u b
F u n c t i o n .
, . , P u b l i c .
P u b l i c Const I n t e r e s t R a t e As Double = 0.0725
VBA
( ). , .

. ,
, , ,
, .
, , .


Excel VBA , .
. , . ( x l L a n d s c a p e ) :
Sub SetToLandscape()
ActiveSheet.PageSetup.Orientation = xlLandscape

End Sub


,
. * .

. , , , bwassaved. , ,
Boolean.
.

Boolean
Integer
Long
Single
Double
Currency
Date/Time

186

b
x

d
E

at
ft VBA

String
Object
Variant

str
obj
V
u

x l L a n d s c a p e . . ,
AutoList Members, .
VBA , .
x l L a n d s c a p e 2. x l P o r t r a i t 1. ,
.
Object Browser, 7,
Excel VBA. Object Browser, VBE <F2>.


Excel, VBA , (). VBA
.

.
65535 .
2 . .

1 , .
Dim , ( ), (
). .
M y S t r i n g
50 . Y o u r S t r i n g ,
:
Dim MyString As String * 50
Dim YourString As String


, ,
. D a t e
.
, D a t e , 8
1 100 31 9999 . 10000
!
D a t e . VBA
, # (. ).

III. Visual Basic for Applications

~87

Excel
Excel ( ) ,
1900 . Excel 29 1900 . Excel 29-
1900- :
=(1900;2;29)

VBA . Excel VBA


DateSerial. () 1 1900 ;
=DateSerial(1900,2,29]
, Excel VBA. 1
1900 1 1900 .
, VBA, , Excel, 1 1900 . , , ,
Excel.

Date.
Dim Today As Date
Dim StartTime As Date
Const FirstDay As Date = #l/l/2001#
Const Noon = #12:00:00#
//, (, //).
, , . ,
(12- 24-). Windows.
Web- Extended Data Functions. VBA. Excel , , 1 1900 .


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

188

8. V8A

VBA (=).
( ):
= 1
X = X + 1
= ( * 2) / (z * 2)
FileOpen = True
FileOpen = Not FileOpen
Range("The Year").Value = 2001

.
, ( - ^ J V ).

. VBA,
Excel , VBA.
VBA .

VBA . , (+), (*), (/), (), ( ) (&). :


( \ ) , M o d . Mod .
, 2:
17 Mod 3
VBA , Excel:
(=), (>), (<), (>=), (<=) ( ) .
, VBA (. 8.2).
( ) VBA.

8.2. VBA

Not

And

Or

xoR

Eqv

imp

VBA , Excel. ,
, .
Not . DisplayGridLines True F a l s e . , Not True F a l s e , a F a l s e True:
ActiveWindow.DisplayGridLines = _
Not ActiveWindow.DisplayGridLines

///. Visual Basic for Applications

189

And.
MsgBox T r u e , 1 1. , MsgBox F a l s e :
MsgBox A c t i v e S h e e t . N a m e = " 1 " And A c t i v e C e l l . R o w = 1
Or. MsgBox T r u e , 1 2:
MsgBox A c t i v e S h e e t . N a m e = " 1 " _
Or A c t i v e S h e e t . N a m e = "2"

, ;
, . ,
12- , .
MonthNames,
MonthNames {0), MonthNames (1) .., MonthNames ( 1 1 ) .


, , Dim P u b l i c .
, :
, . , , 100 :

Dim MyArrayd To 100) As I n t e g e r


,
VBA . , :
Dim MyArray(0 100) Integer
Dim MyArray(lOO) As Integer
101 - .


. VBA 60- ,
( ). 100-
:

Dim MyArrayd To 10, 1 10) As I n t e g e r


10x10. , . , .
,

4)

= 125

,
, .
.
:
Dim MyArray() As Integer

190

8. VBA

, , ReDim, VBA,
( ReDim P r e s e r v e , ). ReDim , , ,
.


, , , . :

, ,
Dim P u b l i c . , I n p u t A r e a
Range:
P u b l i c I n p u t A r e a As Range
, , , :
Sub NoObjVarl)
Worksheets("1").Ranget"Al").Value = 124
Worksheets("1").Rangef"Al"}.Font.Bold = True
Worksheets("1").Range!"Al").Font.Italic = True
End Sub
Al 1 ,
.
. , :
Sub ObjVart)
Dim MyCell As Range
Set MyCell = W o r k s h e e t s ( " 1 " ) . R a n g e ( " A l " )
MyCell.Value = 124
M y C e l l . F o n t . B o l d = True
M y C e l l . F o n t . I t a l i c True
End Sub
MyCell Range S e t
. MyCell
W o r k s h e e t s ("1") . Range {"Al").
VBA ,
. , , . . , VBA
(, sheets ( l ) .Range ("Al")),
.
. , .
With-End w i t h , . .
.

///. Visual Basic for Applications

191


VBA , , ( Pascal ).
. , ,
CustomerInfo:
Type Cus t o m e r I n f
Company As S t r i n g * 25
C o n t a c t As S t r i n g * 15
RegionCode As I n t e g e r
S a l e s As Long
End Type

.


Dim. :
Dim Customers(X 100) As Customerlnfo
100 ( C u s t o m e r l n f o ) . :
Customers(I).Company = "Acme Tools"
Customers(1).Contact = "Tim Robertson"
Customers(1).RegionCode = 3
Customers{1}.Sales = 150677
.
, C u s t o m e r s (1) C u s t o m e r s (2), :
Customers(2) = Customers(1)
:
Customers(2).Company = Customers(1}.Company
Customers(2).Contact = Customers(1).Contact
Customers(2).RegionCode = Customers(1).RegionCode
Customers(2).Sales = Customers(1).Sales


, VBA
, . , - . VBA
( ) Excel. , VBA UCase,
, Excel .
VBA .
.

192

8. VBA

VBA , VBA {.).


VBE VBA ,
(. 8.1). . ,
, Auto List Members, Tools ^Options,
Editor.

. 8.1. VBA VBE


VBA , Excel. , VBA.
,
Sqr VBA,
:
Suto Shov/Root ()
MyValue = 25
SquareRoot = Sqr(MyValue)
MsgBox SquareRoot
End Sub
Sqr VBA Excel.
( ) Excel VBA. Worksheer.Function, A p p l i c a t i o n ,
, VB .
Excel VBA,
:
Application.WorksheetFunction
, Excel
VBA- Excel ROMAN :
Sub ShowRoman()
DecValue = 2001
RomanValue = Application.WorksheetFunction.Roman(DecValue)
MsgBox RomanValue
End Sub

MsgBox
MsgBox VBA.
.
. , , MsgBox,
.

III. Visual Basic for Applications

, , .
MsgBox , , . ,
MsgBox, .
MsgBox, , .
MsgBox ( ):
MsgBox([, ] [, ]{, _, ])
( ) , .
( ) , , ( ) . (, vbYesNo).
( } , . Microsoft Excel.
_ ( ) , .
( ) . .
. Ans:
Ans = MsgBox(" ?", vbYesNo + vbQuestion, "!")
If Ans = vbNo Then E x i t Sub
,
(vbvesNo + vbQuestion). vbYesNo
: . vbQuestion {. ). ,
Ans , vbYes vbNo. , .
, , MsgBox.
MsgBox MMI.
, , Excel , .
, Excel, VBA . , VBA
, VBA : S q r . , :
MsgBox A p p l i c a t i o n . W o r k s h e e t F u n c t i o n . ( 1 2 3 )
'
10, VBA , , Excel.


, .

194

8. VBA

. VBA ,
.
With-End With.
For Each-Next.

With-End With
W i t h - E n d W i t h . , , , (, Range):
Sub ChangeFontl()
Selection.Font.Name = "Times New Roman"
S e l e c t i o n . F o n t . F o n t s t y l e = "Bold I t a l i c "
S e l e c t i o n . F o n t . S i z e = 12
S e l e c t i o n . F o n t . U n d e r l i n e = xlUnderlineStyleSingle
Selection.Font.Colorlndex = 5
End Sub
With-End With. ,
, , :
Sub ChangeFont2()
With Selection.Font
-Name = "Times New Roman"
.FontStyle = "Bold Italic"
.Size = 12
.Underline = xlUnderlineStyleSingle
.Colorlndex = 5
End Sub
, . , , ,
W i t h - E n d With, ,
.
VBA Excel With-End w i t h
. , ^ .

For Each-Next
, , . , Workbooks Workbook.
, . For Each-Next, , .
, ,
. For Each-Next.
For Each-Next .

///. Visual Basic for Applications

195

For Each In
[]
[Exit For]
[]
Next []
For Each-Next
:
Sub MacrolO
Dim MyArray(5}
For i = 0 To 5
MyArray(i) = Rnd
Next i
For Each n In MyArray
Debug.Print n
Next n
End Sub
, , For Each-Next
Sheets . MsgBox Name . ( ,
MsgBox .)
Sub CountSheets()
Dim Item as Worksheet
For Each Item In ActiveWorkBook.Worksheets
MsgBox Item.Name
Next Item
End Sub
item (, worksheet). item ; .
For Each-Next
Windows:
Sub HiddenWindows()
Dim AllVisible As Boolean
Dim Item As Window
AllVisible = True
For Each Item In Windows
If Item.Visible - False Then
AllVisible = False
Exit For
End If
Next Item
MsgBox AllVisible
End Sub

, A l l V i s i b l e F a l s e
For Each-Next. True ( )
F a l s e ( ). Exit For . For Each-Next. If-Then (. ).

196

8. VBA

, . I f - T h e n Workbooks:

Sub CloselnActive()
Dim Book as Workbook
For Each Book In Workbooks
If Book.Name <> ActiveWorkbook.Naine Then Book.Close
Next Book
End Sub
F o r E a c h - N e x t
, . S e l e c t i o n
, Range, Range.
VBA UCase , ,
( ):
Sub MakeUpperCase()
Dim Cell as Range
For Each Cell In Selection
Cell.Value = UCase(Cell.Value)
Next Cell
End Sub


VBA .
(, , ).
, , ,
.
F o r E a c h - N e x t , .
VBA.

GoTo.

I f - T h e n .

S e l e c t C a s e .

F o r - N e x t .

Do W h i l e .

Do U n t i l .

GoTo

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

III. Visual Basic for Applications

197

VBA I n p u t B o x
. ,
WrongName, . . E x i t Sub :

Sub GoToDemo(}
UserName = InputBox(" :")
If UserName <> "" Then GoTo WrongName
MsgBox (", .")
'
-[ ]
Exit Sub
WrongName:
MsgBox ", ."
End Sub
: , GoTo, , ,
. ,
GoTo VBA (. 9).

If-Then
, I f - T h e n VBA.
. . Excel, , .
I f - T h e n :

If Then _ [Else _]
I f - T h e n
. E l s e .
, .
I f - T h e n E l s e . . VBA , Excel. ,
0 . 5 . VBA Time ,
. , . 0 . 5, , .
Sub G r e e t M e l O
If Time < 0.5 Then MsgBox " "
End Sub
, ,
I f - T h e n :
Sub G r e e t M e 2 ( )
If Time < 0 . 5 Then MsgBox " "
If Time >= 0 . 5 Then MsgBox " "
End Sub
, I f - T h e n >=
( ), ,
12:00.

198

8. VBA

Else If-Then.
Sub GreetMe3()
If Time < 0.5 Then MsgBox " " Else _
MsgBox " "
End Sub
, a If-Then-Else
.
(, ,
), If-Then, If-Then-Else. .
Sub GreeCMe4{)
If Time < 0.5 Then MsgBox " "
If Time >= 0.5 And Time < 0.75 Then MsgBox " "
If Time >= 0.75 Then MsgBox " "
End Sub
0.75 18:00 ,
.
, . , , . , , , , . ,
,
, .
:
If

Then
[_]
[Elseif -n Then
[_]]
[Else
[__] ]
End If
GreetMe:
Sub GreetMe5()
If Time < 0.5 Then
MsgBox " "
Elseif Time >=0.5 And Time < 0.7 5 Then
MsgBox " "
Elseif Time >=0.75 Then
MsgBox " "
End If
End Sub
, , , If-Then . ,
. ,
.
. I f - T h e n - E l s e ( E l s e i f ) . , . , If End If.

///. Visual Basic for Applications

199

Sub GreetMe6()
If Time < 0.5 Then
MsgBox " "
Else
If Time >=0.5 And Time < C. 75 Then
MsgBox " "
Else
If Time >=0.75 Then
MsgBox. " "
End If
End If
End If
End Sub
,
I f - T h e n . Q u a n t i t y I n p u t B o x
, Q u a n t i t y , . , (, ).
Sub D i s c o u n t l O
Quantity = InputBox(" : ")
If Quantity = "" Then Exit Sub
If Quantity >= 0 Then Discount. = 0.1
If Quantity >= 2 5 Then Discount = 0.15
If Quantity >= 50 Then Discount = 0.2
If Quantity >= 75 Then Discount = 0.25
MsgBox ": " & Discount
End Sub
, I f - T h e n
, D i s c o u n t . .
, . _
.
Sub Discount2()
Quantity = InputBox(" : ")
If Quantity = "" Then Exit Sub
If Quantity >= 0 And Quantity < 25 Then
Discount = 0.1
Elself Quantity < 50 Then
Discount = 0.15
Elself Quantity < 75 Then
Discount = 0.2
Elself Quantity >= 75 Then
Discount = 0.2 5
End If
MsgBox ": " & Discount
End Sub
I f - T h e n . .
, S e l e c t C a s e .

200

8. VBA

Select Case
S e l e c t Case .
IfThen-Else. S e l e c t Case :
Select Case _
[Case -
[-]]
[Case Else
[__] ]
End Select
VBA IK
VBA i f - T h e n n f .
Excel. :
I I f {, &^, _1),
( ) , ;
_ ( ) ,
, True;
4acTb_False ( ) ,
, False.
n f . , 1 , He-, 1 :
MsgEox I l f (Range["Al") = 0, "", "He-")
, [4acTb_False) , {^) . ,
, :
MsgBox I l f (n = 0, 0, 1/)
S e l e c t Case, GreetMe, :
Sub GreetMeO
Select Case Time
Case Is < 0.5
Msg = " "
Case 0.5 To 0.75
Msg = " "
Case Else
Msg = " "
End Select
MsgBox Msg
End Sub
Discount, S e l e c t Case. , Q u a n t i t y . .
Sub Discount3{)
Quantity = inputBox.( " : ")
III. Visual Basic for Applications

291

Select Case Quantity


Case ""
Exit Sub
Case 0 To 24
Discount = 0.1
Case 25 To 49
Discount = 0.15
Case 50 To 74
Discount = 0.2
Case Is >= 75
Discount = 0.25
End Select
MsgBox ": " & Discount
End Sub
C a s e Or.
, ( ),
VBA WeekDay, .
Or, .
Sub GreetUser(}
Select Case Weekday(Now)
Case 1 Or 7
MsgBox " ."
Case Else
MsgBox " ."
End Select
End Sub
C a s e ,
C a s e , T r u e .
C a s e , ,
, (
VBA ). . :
Sub Discount3()
Quantity = InputBox(" : ")
Select Case Quantity
Case "": Exit Sub
Case 0 To 24: Discount = 0.1
Case 25 To 49: Discount = 0 . 1 5
Case 50 To 74: Discount = 0.2
Case Is >= 75: Discount = 0.25
End Select
MsgBox ": " & Discount
End Sub
VBA Select Case, case, True. , .
S e l e c t C a s e . , Excel (, , ), . Excel , .

202

8. VBA

Sub AppWindow()
Select Case Application.WindowState
Case xlMaximized: MsgBox " "
Case xlMinimized: MsgBox " "
Case xlNormal: MsgBox " "
Select Case ActiveWindow.WindowState
Case xlMaximized: MsgBox " "
Case xlMinimized: MsgBox " "
Case xlNormal: MsgBox " "
End s e l e c t
End Select
End Sub
S e l e c t Case , , S e l e c t Case End S e l e c t .
, ,
. , , :
Sub AppWindow{)
Select Case Application.WindowState
Case xlMaximized: MsgBox " "
Case xlMinimized: MsgBox " "
Case xlNormal: MsgBox " "
Select Case ActiveWindow.WindowState
Case xlMaximized: MsgBox " "
Case xlMinimized: MsgBox " "
Case xlNormal: MsgBox " "
End Select
End Select
End Sub
!


. , , .
, , , . ( S t a r t V a l ) , (NumToFill). GoTo. Cnt, , ,
, , DoAnother:
Sub BadLoop()
StartVal = 1
NumToFill = 100
ActiveCell.Value = StartVal
Cnt = 1
DoAnother:
ActiveCell.Offset(Cnt,0).Value = StartVal + Cnt
Cnt = Cnt + 1
If Cnt < NumToFill Then GoTo DoAnother E l s e E x i t Sub
End Sub
.
1? GoTo,
. GoTo
. ,

///. Visual Basic for Applications

203

. ,
.

( ) .
VBA ,
GoTo.

, . , , , .
VBA?
,
. , ,
.
GOTO. ,
, "" ; .
, . , .
VBA . , , If-Then-Else Select Case, For-Next, Do U n t i l Do While.
, VBA .
,
.

For-Next
F o r - N e x t (
). :
For = fStep mar]
[ ]
[Exit For]
[]
Next []
F o r - N e x t , E x i t F o r . Sum = Sum + Sqr (Count)
100 100 :
Sub S u m S q u a r e R o o t s ( )
Dim Sum As Double, Count As Integer
Sum = 0
For Count = 1 To 100
Sum = Sum + Sqr{Count}
Next Count
MsgBox Sum
End Sub
C o u n t (- ) 1 1 . Sum C o u n t ,

204

8. VBA

For-Next, , . , For Next.


,
. ,
, ,
,
. , , 1 100:
Sub SumOddSquareRooCs{)
Sum =
For Count = 1 To 100 Step 2
Sum = Sum + Sqr(Count)
Next Count
MsgBox Sum
End Sub
Count 1,
3, 5, 7 .. Count, , 99. , Count 101.
, BadLoop,
" ". GoTo, .
F o r - N e x t :
Sub GoodLoop{)
StartVal = 1
NumToFill = 100
ActiveCell.Value - StartVal
For Cnt = 0 To NumToFill - 1
ActiveCell.OffsetfCnt,01.Value = StartVal + Cnt
Next Cnt
End Sub
F o r - N e x t E x i t For.
, ,
. , :
Sub ExitForDemo()
MaxVal = A p p l i c a t i o n . W o r k s h e e t F u n c t i o n . M a x ( R a n g e { " A : A " ) )
For Row = 1 To 65536
Set TheCell = Range{"Al").Offset(Row - 1, 0)
If T h e C e l l . V a l u e = MaxVal Then
MsgBox " " & Row
TheCell.Activate
E x i t For
End If
Next Row
End Sub
Excel MAX.
MaxVal. F o r - N e x t
. MaxVal, E x i t F o r . .

///. Visual Basic tor Applications

205

E x i t F o r D e m o F o r - N e x t .
. :
Range("A:A").Find(Application.WorksheetFunction.Max _.
(Range("A:A"))}.Activate
. F o r - N e x t
F o r - N e x t . ,
F o r - N e x t 10x10x10 - 1 .
1000 - 1 .
Sub NestedLoopg{)
Dim (1 10, 1 10, 1 10)
Dim i As Integer, j As Integer, k As Integer
For i a 1 To 10
For j = 1 To 10
For k = 1 To 10
MyArrayfi, j, k) = -1
Next k
Next j
Next i
End Sub

Do While
Do While , VBA.
For-Next, Do w h i l e , . Do While
:
Do [While ]
[]
[Exit Do]
[]
Loop

DO
[]
[Exit Do]
[]
Loop [While ]
, VBA W h i l e . , . .
( ).
Do Whi le :
Sub DoWhileDemo()
Do While Not IsEmpty(ActiveCell)
ActiveCell.Value = 0
ActiveCell.OffSet(1, 0)-Select
Loop
End Sub

206

8. VBA


, .
. , VBA I s E m p t y
, .
Do w h i l e ,
, :
Sub DoWhileDemo2()
Do
ActiveCell.Value = 0
A c t i v e C e l l . O f f S e t d , 0) . S e l e c t
Loop While Not IsEmpty(ActiveCell)
End Sub
Do W h i l e .
, , ,
, Al, . VBA EOF, T r u e , ^
. .
Sub DoWhileDemolO
Open " c : \ d a t a \ t e x t f i l e . t x t " For Input As #1
LineCt = 0
Do While Not EOF(l)
Line Input #1, LineOfText
Range("Al").Offset(LineCt, 0) = UCase{LineOfText)
LineCt = LineCt + 1
Loop
Close #1
End Sub
VBA 27.

Do Until
Do U n t i l Do w h i l e . , . Do w h i l e , . Do U n t i l , .
Do U n t i l :
Do [Until ]
[ ]
[Exit Do]
[ ]
Loop

Do
[]
[Exit Do]
[ ]
Loop [Un t i l ]
, , Do W h i l e ,
Do U n t i l -

///. Visual Basic for Applications

207

Do. , , Do W h i l e :
Sub DoUntilDemoK)
Open " c : \ d a t a \ t e x t f i l e . t x t " For Input As #1
LineCt
Do U n t i l EOF(l)
Line Input #1, LineOfText
Range("Al").Offset(LineCt, 0) = UCase{LineOfText)
LineCt m LineCt + 1
Loop
Close #1
End Sub

VBA. , , , VBA. ,
.
9 , , VBA.

208

8. VBA


VBA

VBA,
.
VBA .
, ,
.
11 ,
.


VBA,
VBA, VBE. .
, ,
. (
).
,
,
. ,
,
, , . .
.
, . , Excel. ,

.


Sub .
[Private | Public]{Static] Sub ([_])
[]
[Exit Sub]
[]

End Sub

P r i v a t e ( ) ,
.
P u b l i c ( ) ,
. ,
O p t i o n P r i v a t e Module, .
S t a t i c ( ) ,
.
Sub ( } .
.
_ , , it . . , .
() VBA.
E x i t Sub ( )
.
End Sub ( ) .
, VBA
. ,
, (, o p t i o n E x p l i c i t ) .

. , ,
, . , , . , (, ProcessDate , P r i n t R e p o r t - , s o r t _ A r r a y - checkFilenaine - ). (Dolt, Update, F i x ..).
, , (, WriteReportToTextFile - , G e t _ P r i n t _ O p t i o n s _ a r d _ P r i n t _ R e p o r t -
).
, .

210

9. VBA


, , . , ,

Public
P u b l i c ..
. P u b l i c
, .
P u b l i c .
Sub F i r s t ( )
' ...[ ]..
End Sub
Public Sub Second
... [ ]. .
End Sub

Private
P r i v a t e ,
.
^^ Excel
Public-. ,
, ,
, P r i v a t e
.
P r i v a t e MySub:
P r i v a t e Sub MySubO
. .. [ ] . .
End Sub
P r i v a t e , Public.
sub:
Option Private Module
, P r i v a t e .
Excel 1,
2 .. P u b l i c , .


, , , VBA.
Rurr^Run Sub/UserForm ( VBE).
<F5>. Excel , . , .

. Visual Basic for Applications

211

Excel ( ,
^ ^ ) .
<Alt+F8>.
<Ctrl> (
).
.
.
.
.
.
. , , ,
, .
Immediate Visual Basic. , , <Enter>.
Excel 5 Excel 95 Tools
(). Excel 97, .

.
, . ,
. , ,
. , , .


Run^Run Sub/UserForm
R u n ^ R u n Sub/UserForm VBE . Visual Basic, . Rurr^Run Sub/UserForm
( <F5>) VBE. ( , , ),
R u n ^ R u n Sub/UserForm
, VBE ,
.


^ ^ Excel
. 9.1 ( ,
<Alt+F8>). .
,
(, ).

212

9. VBA

. , P r i v a t e , , .
, ,
, , .
.

. 9.1.



, , <-1+ , U p d a t e
<Ctrl+U>, <CtrI+U> .
, .
.
( ), .
1. Excel ^ ^ .
2. .
3. , , . 9.2.
4. Ctrl+.
, Ctrl +, . s , <Ctrl+S>.
5 , <Ctrl+Shift+S>.
. 9.2.
5. ( ). KO.U
( .
)
6. ,
,
, .

HI. Visual Basic for Applications

213


Excel,
. , <Ctrl+S> Excel
. ,
<Ctrl+S> .
Excel <GVc\+xnaeuwa>: E, J, L, M, Q .
Excel < t I+S h if t+ >,
, F, .


23, Excel : ^ ^ VBA. , .
) 5 Excel 95 ,
Excel 97 .

,
. , : , O p e n C u s t o m e r F i l e .
1.

^ ^. Excel .

"". , .
.

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

6. : & , . 9.3.
7. .
8. O p e n C u s t o m e r F i l e
.
9. , , , .
, , , . , ,
*, . , , .

214

9. VBA

23 VBA , , .


. .
( )
.
C a l l , (
), .
Run A p p l i c a t i o n .
VBA XLM. Run , , .
Run .
. My Sub ( ), U p d a t e S h e e t
:
Sub MySubO
...[]...
UpdateSheet
.-.[]...
End Sub
Sub UpdateSheet()
...[]...
End Sub
. C a l l U p d a t e ,
; . .

///. Visual Basic for Applications

25

Sub MySub()
MonthNum = InputBox(" ;
Call UpdateSheetfMonthNum)
1
...[]...
End Sub

")

Sub

UpdateSheet(MonthSeq)
...[]...
End Sub
C a l l ,
( ).

Run, U p d a t e S h e e t
MonthNum ;
Sub MySub()
M o n t h K u m = InputBox(" : ")
Result e A p p l i c a t i o n . R u n ( " U p d a t e S h e e t " , MonthNum)
...[]...
End S u b
Sub UpdateSheet(MonthSeq}
. . . [] .. .
End S u b
, Run
.
(. ). Main VBA WeekDay ( 1 7, ). S u b T o C a l l , . Run (Weekend D a i l y ) .
Sub M a i n ( )
Select Case Weekday(Now)
Case 1: SubToCall = "Weekend"
Case 7: SubToCall - "Weekend"
Case Else: SubToCall = "Daily'
End Select
Application.Run SubToCall
End Sub
Sub Weekend()
MsgBox " "
1
,
End Sub
Sub Daily
MsgBox " "
'
,
End Sub


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

216

9. VBA

, ,
. VBA , ..
, . . , MyEub
: M o d u l e l M o d u l e 2 . , MySub M o d u l e 2
MySub M o d u l e l , :
Modulel.MySub
C a l l Modulel.MySub
,
: Ambiguous Name D e t e c t e d ( ).


, , . :
Run ,
, VBE T o o l s o
References. References, . 9.4, .
, , ,
.
, , .

. 9.4. References


, , . Browse
References, . ,
, VBE.
VBAProjecr. , , , ,
Properties VBE. , References, ActiveX,
. Excel 2002 :

Visual Basic for Applications;

Microsoft Excel 10.0 Object Library;

III. Visual Basic for Applications

217

OLE Automation;

Microsoft Office 10.0 Object Library;

Microsoft Forms 2.0 Object Library (; ,


U s e r F o r m ) .
Excel 2002 , ,
Project Explorer VBE. References,
, ,
YourSub, YourSub:
YourSub
Call YourSub
, ,
, :
MyProj t.MyModule.MySub
C a l l :
Call MyProject.MyModule.MySub
Run A p p l i c a t i o n . .
, C o n s o l i d a t e ,
budget macros . x l s , :
Application.Run

"'budget m a c r o s . x l s ' ! C o n s o l i d a t e "


, , . : ?
-, . , . . ,
. :
Sub Mainf(
Call GetUserOptions
Call ProcessData
Call Cleanup
Call CloseltDown
End Sub
, , . , , , .
, . ,
, , .

. ,
, .

218

9. VBA



Excel,
, . ;
.
, .
.
1.

^ ^. Excel .
Excel "". ,
.

2.

3. .
4.
.
5. , .
6. . "", , . ;
, .
7. , , . Excel .
8. .
9. , .
10. , .

, , , .
, , ^
^ , . , , .
23.


Excel ,
. .
.

III. Visual Basic for Applications

219

, , ^. .

ActiveX, UserForm. , , ( ActiveX).
! 5
Excel 95. ( ).
. ActiveX 13.
B u t t o n (), , .
1. , .
2. .
3. .
<Alt>,
. <Shift>,
.
Excel . , , .
,
.
.


, () . , ,
, C o m m a n d B u t t o n . , , - ,
. .
, ,
. , , , Workbook_Open.
.
19.

Immediate
, Immediate
VBE. , <Ctrl+G>. Immediate

220

9. VBA

VBA, . ,
Immediate <Enter>.
, Immediate
, . :
Sub ChangeCase()
MyString = " "
MyString = UCase(MySbring)
Debug.Print MyString
End Sub
. 9.5 ChangcCase Immediate.
D e b u g . P r i n t .

. 9.5. immediate


, . :

, Excel :

, Excel, (. ).
(, ) .
(, ) ( 30).

HI. Visual Basic for Applications

221

Excel . , ( ).
, , :
Sub,
. , ,,
, , . M a i n
P r o c e s s F i l e ( C a l l F o r - N e x t ) . P r o c e s s F i l e . . P r o c e s s F i l e
( T h e F i l e ) . , Sub. P r o c e s s F i l e , C a l l .
Sub Main
File(l) = "deptl.xls"
File(2) = "dept2.xls"
File(3) = "dept3.xls"
.For,i = 1 To 3
Call ProcessFile{File(i))
Next i
End Sub
Sub ProcessFile(TheFile)
Workbooks.Open FileName:=TheFile
...[]...
End Sub
, ( ). :
Sub Main
Call ProcessFile("budget.xls")
End Sub
: ,
( ) .
. ,
.
. P r o c e s s
( ). , M a i n MyValue 10, P r o c e s s MyValue
. P r o c e s s ( YourValue)
10. P r o c e s s Main, a
MsgBox MyValue : 100.
Sub Main
MyValue = 1 0
Call Process(MyValue)
MsgBox MyValue
End Sub
Sub Process(YourValue)
YourValue = YourValue * 10
End Sub
, ,
, ,

222

9. VBA

, .
ByVal. ,
. , , ,
YourValue Process, MyValue Main. MsgBox 10, 100.
Sub Process(ByVal YourValue}
YourValue = YourValue * 10
End Sub
, , . , , ,
.
,
. , ByVal, ; .
,
. .
,
V a r i a n t . , ,
.
Sub , : . I n t e g e r , s t r i n g :
Sub P r o c e s s ( I t e r a t i o n s As Integer, TheFile As String)
, , , . , Process S t r i n g : ByRef argument type mismatch.
, . . 10, , , .
Public
8 , , Public ( ),
.
Public, ,
, MonthVal ProcessMonth:
Sub MySub()
Dim MonthVal as Integer
1
...[]
MonthVal = 4
Call ProcessMonth{MonthVal)
...[]
End Sub
III. Visual Basic for Applications

223

:
Public MonthVal as I n t e g e r
Sub MySub()
1

. . . [ ]

MonthVal = 4
Call ProcessMonth
1
...[]
End Sub
, MonthVal Public, ProcessMonth ,
.


, , , VBA
: ( , )
( ).
.
,
Break on All Errors. B VBE Toofs^Qptions,
General Options. Break on All Errors,
VBA .
Break on Unhandled Errors.
, VBA, , .
.
,
.
, VBA.
VBA .


, , On E r r o r . .
VBA . E r r . , , .
[ , . .
,
:
On E r r o r Resume Next

224

ft VBA

, .
, . E r r . E r r . V a l u e
{ E r r ) , E r r o r . ,
, Visual Basic ( ):
MsgBox "" & E r r & ": " & E r r o r ( E r r )
. 9.6 VBA, . 9,7 ,
, Excel. ,
, .

. 9.6. ,
VBA,

. 9.7.

Err Number Err. , :


MsgBox E r r
MsgBox Err.Number
On E r r o r ,
. , ::
On Error GoTo ErrorHandler


, .
S p e c i a l C e l l s , . (, ^ , , , , .)
S p e c i a l C e l l s
, , . , VBA . On E r r o r Resume Next :
Sub S e l e c t F o r m u l a s ( )
On Error Resume Next
Selection.SpecialCells{xlFormulas, xlNumbers).Select
On Error GoTo 0
End Sub
, On E r r o r GoTo 0 .

III. Visual Basic for Applications

225

:
.
Sub SelectFormulas2(}
On Error Resume Next
Selection.SpecialCells{xlFormulas,
xlNumbers).Select
If Err <> 0 Then MsgBox "He ."
On Error GoTo 0
End Sub
E r r , , .
:
Sub ErrorDemo(}
On Error GoTo Handler
Selection.Value = 123
Exit Sub
Handler:
MsgBox " ."
End Sub
.
(, ), . On E r r o r H a n d l e r
. , E x i t Sub.
, . , , .
, .
, . .
Sub CheckForFileO
FileName = "BUDGET.XLS"
F i l e E x i s t s = False
1


For Each book In Workbooks
If UCase{book.Name) = FileName Then
F i l e E x i s t s = True
End If
Next book


If F i l e E x i s t s Then _
MsgBox FileName & " ." Else
MsgBox FileName & " ."
End Sub
F o r E a c h - N e x t
Workbooks. , F i l e E x i s t s T r u e . , , .
, ""
. On E r r o r Resume
N e x t VBA .
, ( S e t ) .
, . I f - T h e n - E l s e E r r :

226

ft VBA

Sub CheckForFile{)
Dim x as Workbook
FileName = "BUDGET.XLS"
On Error Resume Next
Set x = Workbooks(FileName)
If Err = 0 Then
MsgBox FileNAme & " ."
Else
MsgBox FileNAme & " ."
End If
On Error GoTo 0
End Sub
11 , .


. ,
.
. , .
,
( , 5). , VBA.
, : ,
. , , .
Web- .

,
, (
Excel ). , ,
, . , .


? .
, .
.
1. (.. ) .
2. .

III. Visual Basic for Applications

227

3.

. ,
.

4. .
5.

VBA.


, . Excel, .

Excel . , .
, .
: ,
, .

, . VBA.

. VBA.

Excel , .
: , . , , VBA ,
.

, .

, ,
, .
: ,
.

, VBA
.
: ...

, ,
, , .
1.

2. .
3. .
4. ( ).
5. .

228

9, VBA


. , .

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


, , VBA.
, . ,
.
. Excel :
Sub M a c r o l O
Sheets("").Select
Sheets("").Move Before:=Sheets(1)
End Sub
Move: , . , . , .
, .
Count , . Immediate VBE
? ActiveWorkbook.Count
! , :
? ActiveWorkbook.Sheets.Count
. . 9.8 -

. 9.8. immediate VBE

///. Visual Basic for Applications

229

\
? . Immediate
?

ActiveWorkbook.Sheets(I).Name

( ).
, .
, F o r E a c h - N e x t
. , :
Sub T e s t ( )
For Each sht In ActiveWorkbook.Sheets
MsgBox Sht.Name
Next Sht
End Sub
! ,
.
, . ,
S o r t R a n g e . .
. ,
, VBA.


,
. . , ,
1. : 1, ,
, 4 5.
2. , .
3.

Testxls.

4.

VBE Personal.xls Project.


P e r s o n a l . x l s Project, ,
. Excel , () ,
.

5.
6.

VBA ( lnsert=>Modu!e).
S o r t S h e e t s (. 9.9).
. . , is .

7. Excel. ^ ^
, . <Ctrl+Shift+S>.

230

VBA

. 9.9. ,


. . , , Dim . , ReDim
.
, S h e e t N a m e s . , MsgBox, , :
Sub SortSheets()
Dim SheetNames() as String
Dim i as Integer
Dim SheetCount as Integer
SheetCount = ActiveWorkbook.Sheets.Count
ReDim SheetNames{1 To SheetCount)
For i = 1 To SheetCount
SheetNames{i) = ActiveWorkbook.Sheets(i).Name
MsgBox SheetNames(i)
Next i
End Sub
, T e s t . x l s
<Ctrl+Shift+S>. .
. , , MsgBox (
).
S o r t S h e e t s
. : S h e e t N a m e s , .

III. Visual Basic for Applications

~~231

MsgBox, P r i n t Debug, Immediate.


MsgBox
.
Debug.Print SheetNames(i)
, MsgBox.


S h e e t N a m e s .
S o r t S h e e t s , , (
).
, .
: ,
. , Internet.
.
( , ).
.
F o r - N e x t ,
. , ,
. (.. - 1 ).
11 .
.
Sub BubbleSort(Liat() As String)
1
L i s t
Dim F i r s t As I n t e g e r , Last As I n t e g e r
Dim i As I n t e g e r , j As I n t e g e r
Dim Temp As S t r i n g
F i r s t = LBound(List)
Last = UBound(List)
For i = F i r s t To Last - 1
For j = i + 1 To Last
If L i s t ( i ) > L i s t ( j ) Then
Temp = L i s t ( j )
List(j) = List(i)
L i s t ( i ) = Temp
End If
Next j
Next i
End Sub
: L i s t . , , .
F i r s t L a s t LBound UBound,
.

232

9. VBA

, , ,
S o r t S h e e t s . B u b b l e S o r t , SheetNames. .
Sub SortSheets()
Dim SheetNames() As String
Dim SheetCount as Integer
Dim i as Integer
SheetCount = ActiveWorkbook.Sheets.Count
ReDim SheetNames(1 To SheetCount)
For i = 1 To SheetCount
SheetNames(i) = ActiveWorkbook.Sheets(i).Name
Next i
Call BubbleSort(SheetNames)
End Sub
Sub BubbleSort{List{) As String)
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As String
First = LBound(List)
Last = UBound(List)
For i = First To Last - 1
For j = i + 1 To Last
If List{i) > List(j) Then
Temp = List(j)
List(j) = Lisc(i)
List(i) = Temp
End If
Next j
Next i
End Sub
S o r t S h e e t s , . , Intermediate, S o r t S h e e t s :
For i = 1 SheetCount
Debug.Print SheetNames(i)
Next i
. SheetNames,
, , , , ?
Sheets{"").Move Before:=Sheets(1)
F o r - N e x t ,
, SheetNames:
For i = 1 SheetCount
Sheets(SheetNames(i)>.Move
Next i

Before:=Sheets(i)

, (ij 1. S h e e t Names ( ) !. , Move


:
Sheets("1").Move

Sheets(l)

///. Visual Basic for Applications

233

:
Sheets("2").Move Sheets(2)
SortSheets :
Sub SortSheets
SheetCount = ActiveWorkbook.Sheets.Count
ReDim SheetNames(1 To SheetCount)
For i = 1 To SheetCount
SheetNames(i) = ActiveWorkbook.Sheets(i).Name
Next i
Call BubbleSort(SheetNames)
For i = 1 To SheetCount
ActiveWorkbook.Sheets ISheetNames{i)).Move _
Before:=ActiveWorkbook.Sheets(i)
Next i
End Sub
, T e s t . x l s .
. , ,
, ,
. S o r t S h e e t s :
Sub SortSheets{)
1

' .
Dim SheetNames{) as String
Dim i as Integer
Dim SheetCount as Integer
1

,
SheetCount = ActiveWorkbook.Sheets.Count
ReDim SheetNames(1 To SheetCount)

'


For i = 1 To SheetCount
SheetNames(i) = ActiveWorkbook.Sheets(i).Name
Next I

'


Call BubbleSort(SheetNames)


For i = 1 SheetCount
ActiveWorkbook.Sheets(SheetNamos(i)).Move _
ActiveWorkbook.Sheets(i)
Next i
End Sub
, . ,
T e s t . x l s . !


, , . ,
T e s t . x l s , , .
,

234

9. VBA

. , ( ,
). .
,
.
. ,
SUMMARY ( )
Sheet 1. B u b b l e S o r t ( U
, ).
, <Ctrl+Shift+S> .

, Move .

Move .
.
, , Excel.
. ; , , .
<Ctrl+Break> VBA .


. S o r t S h e e t s
Application.ScreenUpdating = F a l s e
BubbleSort.
UCase, . ,
.
:
If U C a s e ( L i s t ( i ) ) > UCase(List(j)} Then
" "
Option Compare Text
VBA . , , , , CBepicy, . , . On E r r o r Resume N e x t , ,
E r r . E r r , ,
. , . :
On E r r o r Resume N e x t
SheetCount = ActiveWorkbook.Sheets.Count
If E r r <> 0 Then E x i t Sub '
On E r r o r Resume N e x t .
, , :
If ActiveWorkBook Is Nothing Then Exit Sub

ill. Visual Basic for Applications

235

.
; , . P r o t e c t S t r u c t u r e Workbook True,
. :
'


If ActiveWorkbook.ProtectStructure Then
MsgBox ActiveWorkbook.Name & ' ., _
vbCritical, ."
Exit Sub
End If

. :
, (
V i s i b l e ) . . ( S h e e t H i d d e n )
:
1


For i = 1 SheetCount
SheetHidden{i) = Not ActiveWorkbook.Sheets(i).Visible

If SheetHidden(i) Then ActiveVforkbook.Sheets(i).Visible = True
Next i

, , , ,
:

For i = 1 SheetCount
If S h e e t H i d d e n ( i ) Then A c t i v e W o r k b o o k . S h e e t s ( i ) . V i s i b l e
Next i

= False

, "".
, , (oldActive),
.
<Ctrl+Break> , VBA .
, .
, A p p l i c a t i o n E n a b l e C a n c e l K e y , <Clrl+Break>.
:
Application.EnableCancelKey = xlDisabled
, .
, .
, , .
S o r t S h e e t s
, 9.1.
,

236

9. VBA

9.1. SortSheets
Sub SortSheets{)
1

Dim
Dim
Dim
Dim
Dim
Dim
Dim

SheetNames() As String
SheetHidden As Boolean
i As Integer
SheetCount As Integer
VisibleWins As Integer
Item As Object
OldActive As Object

If ActiveWorkbook Is Nothing Then Exit Sub


SheetCount ActiveWorkbook.Sheets.Count
1


If ActiveWorkbook.ProtectStructure Then
MsgBox ActiveWorkbook.Name & " .", _
vbCritical, " ."
Exit Sub
End If

'

Ctrl+Break
Application.EnableCancelKey = xlDisabled


SheetCount = ActiveWorkbook.Sheets.Count


ReDim SheetNames(1 To SheetCount)
ReDim SheetHidden{1 To SheetCount}

Set Old&ctive = ActiveSheet

'


For i = 1 SheetCount
SheetNames(i) = ActiveWorkbook.Sheets(i).Name
Next i
For i = 1 To SheetCount
SheetHidden(i) = Not ActiveWorkbook.Sheets{i).Visible
unhide hidden sheets
If SheetHidden(i) Then ActiveWorkbook.Sheets(i).Visible = True
Next i


Call BubbleSort(SheetNameg}

'


Application.ScreenUpdating = False


For i - 1 To SheetCount
ActiveWorkbook.Sheets(SheetNames(i)}.Move _
Before:=ActiveWorkbook.Sheets(i)
Next i

///. Visual Basic for Applications

237

'


For i = 1 SheetCount
If SheetHidden(i) Then ActiveWorkbook.Sheets(i).Visible
False
Next i
1


OldActive.Activate
End Sub


S o r t S h e e t s , Excel. ( <AU+F8>) <Ctrl+Shift+S>.
. , .


, . : , , ,
( ) VBA.
: "", ,
11 2, 2 11.
Web- , . : ( ).
,
Excel . ,
, 12,
VBA.
, , VBA . .

Immediate,
, .
, , , .
, . ,
, , ,
, .
. , .

238

9. VBA

.
, , ,
.

, . , .
VBA .

III. Visual Basic for Applications

239

V >:
Sub . Sub .
.
11 .
.

,
.
,
( ), Excel VBA. ,
.

,

VBA.

, .

, Excel VBA.

, Excel. ,
,
. Excel 300 ,
, . , VBA.

, Excel VBA,
. .
VBA.
, , .
. , , , ,
.
,
. , , . , ,
.
. , , ,
.
, . , . ,

Excel. ,
.
;
.


, VBA.


, VBA.
R e v e r s e . .
Function Reverse(InString) As String
1

Dim i as Integer, StringLength as Integer
Reverse = ""
StringLength = Len(InString)
For i = StringLength To 1 Step -1
Reverse = Reverse & Mid(InString, i, 1)
Next i
End Function
, " ".
, , , VBA. ,

III Visual Basic for Applications

241


, R e v e r s e , Excel . :
=Reverse(A1)
10.1. , . .
, .
, ^
. .

, , ( ) R e v e r s e . :
=Reverse(Reverse(Al) )

VBA
VBA, ,
R e v e r s e , . VBA , , R e v e r s e (. 10.2). ,
Sub Reverselt{)
Dim Userlnput as String
Userlnput = InputBox(" :")
MsgBox Reverse(Userlnput), , Userlnput
2nd Sub
, . 10.2, I n p u t B o x
VBA Excel. MsgBox -.

. 10.1.

. 10.2. VBA


, . , , .
, .

242

10.


Function Reverse(InString) As String
'
Dim i as Integer, StringLength as Integer
Reverse = ""
StringLength = Len(InString)
For i = StringLength To 1 Step -1
Reverse = Reverse & Mid(InString, i, 1)
Next i
End Function
, F u n c t i o n , Sub,
(Reverse). ( I n S t r i n g ) , . As S t r i n g ,
, . (Excel
V a r i a n t , ).
(), . Dim
(i S t r i n g L e n g t h ) , .
. ,
. ,
, .
VBA Len S t r i n g L e n g t h .
For-Next.
. , S t e p For-Next , .
VBA Mid, , .
R e v e r s e , . , ,
End F u n c t i o n .
,
, VBA, ,
. , , "". ,
. , .
, . , , . ,
, . ,
. : , .


. (
9.)

///. Visual Basic for Applications

243


:
[Public | P r i v a t e ] [ S t a t i c ]
[]
[ = ]
[Exit Function]
[]
[ = ]
End Function

Function ([^]) [As ]

Public

( ) ,
VB

Private

( ) ,

Static

( ) , , ,

Function

() , ,

() , .
, .

_ () , , . .

() ,

() VB A

Exit Function

() ,

End F u n c t i o n

() ,

, VBA, : , , ,
.
VBA ( ). F u n c t i o n ,
( ) . , ,
As ( , ). VBA,
, , . End F u n c t i o n .
, . , ,
(, J21).

244

10.

, ,
Excel. Excel .


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

, 1.

, P r i v a t e ,
. , ,
VBA, P r i v a t e ,
,

VBA ,
, ,
VBE T o o l s ^ R e f e r e n c e s .


,
.


,
. , SuinArray , :
T o t a l = SumArray(MyArray)
S u m A r r a y , T o t a l .
, Run A p p l i c a t i o n . :
Total

= Application.Run

("SumArray",

"")

Run .
() . Run (
), .


, . , Excel
. ,
. ,
Excel, .
.

III. Visual Basic for Applications

245

. , C o u n t N a m e s ,
Myf u n c . x l s , :
=Myunc.xls!CountNames(Al:AlOOO)
1 *, .
. VBE
Tools 1 ^ References. , .
, , ( ).
. ,
, ,
. . 21,

Excel VBA .
.
Excel ( ) upCase. ,
{. .)
Function UpCase(InString As S t r i n g ] As S t r i n g
1

Dim StringLength As Integer
Dim i As Integer
Dim ASCIIVal As I n t e g e r
Dim CharVal As Integer
StringLength = Len(InString)
UpCase = InString
For i = 1 To StringLength
ASCIIVal = Asc(Mid{InString, i, 1))
CharVal = 0
If ASCIIVal >= 97 And ASCIIVal <= 122 Then
CharVal = -32
Mid(UpCase, i, 1) = Chr(ASCIIVal + CharVal)
End If
Next i
End Function
, , , , 10000
26 . 13 . Excel . ,
, .
UpCase
, ,
, VBA.
, , ,
. , VBE Run^Sub/UserForm ( <F5>),

246

10.

(
, ). .
, . ,
.


.
( ), , .
.

{ 1 60).
, .
#!, .
, (, ).
. " " .


, ,
. .
Web- .


, .
, Excel , ,
() ( ) . .
, . UserName A p p l i c a t i o n . Excel , Windows.
Function User()
'
User = A p p l i c a t i o n . U s e r N a m e
End F u n c t i o n
(, ).
=User()

III. Visual Basic for Applications

247

. , VBA, ,
.
, , .
U s e r ,
MsgBox , . (&) U s e r ,
Sub ShowUser(}
MsgBox " " & User
End Sub


Excel () .

. ^ .
, , .
VBA Rnd (}, 0 1. :
Function StaticRand()
' ,
1

S t a t i c R a n d = Rnd()
End F u n c t i o n

] 000,
=UEflOE(StaticRand() *1000)
, , , () -


, , , .
, Excel.
, ..
. . , .
A p p l i c a t i o n . V o l a t i l e True
V o l a t i l e A p p l i c a t i o n (True False).
v o l a t i l e ( ), , .
, StaticRand
V o l a t i l e , Excel ( ) .

248

10.

Function
NonStaticRandl)
' ,
1

Application.Volatile True
NonStaticRand = Rnd()
End Function
False V o l a t i l e
, (
, ).
,
, <Ctrl+Alt+F9>. , , s t a t i c R a n d , .


,
. .

0-S9999

8,0%

$10000-$199

10,5%

$20000-$39999

12,0%

$40000

14,0%

, . , , .
, Excel. ,
, :
=((1>=0;1<=9999,99);1*0,08;
<(1>=10000;1<=19999,99};1*0,105;
{<1>=20 000;1<=3 9999,99);1*0,12;
ECJIMF(A1>=4OOOO;A1*O,14;0}))}
. -, ,
. -, , -
.
(
VBA):
. :
=(1;;2}*1
( ) , .
F u n c t i o n Commission{Sales)
Const T i e r l = 0 . 0 8
Const Tier2 = 0.105
Const Tier3 =0.12
Const Tier4 = 0.14
1

///. Visual Basic for Applications

249

Select Case Sales


Case 0 To 9999.99: Commission. = Sales * Tierl
Case 10000 To 19999.99: Commission = Sales * Tier2
Case 20000 To 39999.99: Commission = Sales * Tier3
Case Is >= 40000: Commission = Sales * Tier4
End Select
End Function
VBA
VBA.
3000 (
25000 12%);
=Commission(2 5000)
,
VBA. , VBA
, VBA. ,
Commission ,
Sub CalcComm()
Dim Sales as Long
Sales = InputBox(" :")
MsgBox " " & Commiss ion(Sales)
End Sub
CalcComm
. .
, . , - . W.3.
,
(. 10.3).

Sub CalcComm()
Dim Sales as Long
Dim Msg As String, Ans As String
'
Sales = Val(InputBoxf" :", _
" "))
'
Msg = " : " & vbTab & Format (Sales, "S#i##0 . 00")
Msg = Msg & vbCrLf & ":" & vbTab
Msg = Msg & Format(Commission(Sales), "$#,##0.00")
Msg = Msg & vbCrLf & vbCrLf & " ?"
'
Ans = MsgBox(Msg, vbYesNo, " ")
If Ans = vbYes Then CalcComm
End Sub
VBA: vbTab
( ), a vbCrLf ( ). VBA F o r m a t ( , ).

250

10.

Commission
; Excel , , .


, , , , : ,
, 1% , .
Commission ( ) ,
.
. Commission2.
Function Commission2(Sales, Years)
'
'
Const T i e r l = 0.08
Const Tier2 = 0.105
Const = 0 . 1 2
Const Tier4 = 0.14
Select Case Sales
Case 0 To 9999.99: Commission2 = Sales * T i e r l
Case 10000 To 19999.99: Commission^ = Sales * Tiec2
Case 20000 To 39999.99: Commission2 = Sales * Tier3
Case Is >= 40000: Coramisaion2 = Sales * Tier4
End Select
Commission2 = Commiasion2 + (Commission2 * Years / 100)
End Function
, ? (Years) Function
, .
(, 1, , , 1):
=Commission2(Al;Bl)


, () . , , .

Function SuinArray(List) As Double


Dim Item As Variant
SumArray = 0
For Each Item In L i s t
If WorksheetFunction. isNumber (Item) Then __
SumArray = SumArray + Item
Next Item
End Function
Excel IsNumber , , .
.

///. Visual Basic for Applications

251

, .
M a k e L i s t 100- . MsgBox
SumArray.
Sub M a k e L i s t ( )
Dim Numsd To 100) As Double
Dim i as I n t e g e r
For i = 1 To 100
Hums(i) = Rnd * 1000
Next i
MsgBox SumArray (Nums)
End Sub
SumArray (
V a r i a n t ) , . ,
1 ;10:
=SumArray(Al:C10)
, , SumArray
Excel , , SumArray
( 30-). ,
. SumArray
Excel .


Excel .
, . :
([;_])
, . . Excel
1, , , :
(1;1)
{1>
, VBA, . , O p t i o n a l .
.
.
. , .
( ), , ,
.
Function Draw(Rng As Variant, Optional Recalc As Boolean = False}
'
' , Recalc True
Application.Volatile Recalc
'

252

10.

raw = Rng(Int((Rng.Count)
End Function

* Rnd + 1))

, Draw O p t i o n a l ,
.
,
:
=Draw(Al:A100)
=Draw(Al:A100;False)
=Draw(Al:Al00,-True)
,
..

VBA,
VBA A r r a y . A r r a y V a r i a n t , ( ). Excel, A r r a y VBA. <Ctrl+Shift+Enter>.
Excel , , . 3.
, , Array,
, Variant.
, Variant , Variant.
MonthNames, , A r r a y :
Function MonthNames()
MonthNames = Array("", "", "", "", _
"", "", "", "", "", "", _
"", "")
End Function
MonthNam.es .
, MonthNames.
, , , VBA .
( 12 ),
<Ctrl+Shift+Enter>:
^MonthNames{)
. 10.4 . ,
12- ( A l : L1).

. 10.4. tbNemes

III. Visual Basic for Applications

253

, , <Ctrl+Shift+Enter>:
=(MonthNames())
Excel
.
MonthNames.
Function MonthNames()
Dim AllNames As Variant
AllNames = Array("", "", "", "", _
"", "", "", "", "", "", _
" ", "")
If isMissing(MIndex) Then
MonthNames = AllNames
Else
Select Case MIndex
Case Is >= 1
1
, (, 13=1)
MonthVal = ({MIndex - 1) Mod 12)
MonthNames = AllNames(MonthVal)
Case Is <= 0 '
MonthNames = Application.Transpose(AllNames)
End Select
End If
End Function
, VBA
I s M i s s i n g ,
,
. I s M i s s i n g ,
V a r i a n t .
, .

, .

0, . Excel .

1, , ,
Mod . Mod . , 13 0, 24 11 ..

, . 10.5.
A l : L1 , . A l : L1, <Ctrl+Shift+Enter>:
=MonthNames()
A3: 14 1 12.
, 11 , :
=MonthNames(A3)
D3 : D14 , :
=MonthNames(-1)

254

10.

, /0,5.
, <Ctrl+Shift+Enter>.
, Array,
, o p t i o n Base . Option Base , 0.

,
,
. R e v e r s e , .
Function Reverse(InString) As String
'
Dim i as Integer, StringLength as Integer
Reverse = ""
StringLength Len (InString)
For i = StringLength To 1 Step -1
Reverse = Reverse & Mid(InString, i, 1)
Next i
End Function
( ) . , ,
.
, , (#/ ).
, ,
Excel:
R e v e r s e = "#/"
, ,
, .
, CVErr,
.
, VBA ,
.
Excel, VBA. .

III. Visual basic for Applications

255

xlErrDivO ( #/0 !);


xlErrNA ( #/);
xlErrName ( #?);
x l E r r N u l l ( #!>;
xlErrNum ( #! ):
xlErrRef ( #1! );
x l E r r V a l u e ( #!}.
#/ , :
Reverse = CVErr(xlErrNA)
Reverse. Excel ETEKCT
( I s T e x t ) , . ,
. ( ),
#/.
Function R e v e r s e ( I n S t r i n g ) as Variant
' - ,
1
#N/A
Dim i as I n t e g e r , StringLength as I n t e g e r
If Application.WorksheetFunct.ion. IsText ( I n S t r i n g ) Then
Reverse = ""
StringLength = Len{InString)
For i = StringLength To 1 Seep -1
Reverse = Reverse & Mid(InString, i, I)
Next i
Else
Reverse = CVErr{xlErrNA)
End If
End Function
, ,
. - , , v a r i a n t .


Excel . , :
(1;2;..)
, 29- .
_-.
CyMM(Al:A5;Cl:C5rEl:E5;Gl:G5)
. , : , ,
:
=(1:5;12;24*3)
, . ( ) P a r a m A r r a y .

256

10.

ParamArray . v a r i a n t ( O p t i o n a l ).
, .
( -).
.
Function SimpleSum(ParamArray a r g l i s t ( ) As Variant) As Double
For Each arg In a r g l i s t
SimpleSum = SimpleSum + arg
Next arg
End Function
SimpleSum , Excel .
, , ,
, , .

Excel
MySum.
SimpleSum, , MySum
Excel .
MySum Excel . . - (
), , ,
, ,
. :
=(1;5;"6";;;(4);1:5)
,
:

, ;

, ;

MySum ( 10.1) .
, MySum, Web-
.

'
Dim i As Variant
Dim TempRange As Range, cell As Range
Dim ECode As String
MySum = 0
1


For i = 0 To UBound(args)
'

If Not IsMissinglargs{i)) Then
'

Select Case TypeName(args(i))
Case "Range"
/
Set TempRange = Intersect(args(i).Parent.UsedRange,_
args(i))
For Each cell In TempRange
If IsError(cell) Then
MySum = cell ' return the error
Exit Function
End If
If cell = True Or cell = False Then
MySum = MySum + 0
Else
If IsNumeric(cell) Or IsDate(cell) Then MySum_
= MySum + cell
End If
Next cell
Case "Null"
'
Case "Error' '
MySum = args(i)
Exit Function
Case "Boolean"
If args(i) = "True" Then MySum = MySum + 1
Case "Date"
MySum = MySum + args(i)
Case Else
MySum = MySum + args{i)
End Select
End If
Next i
End Function
MySum .
( I s M i s s i n g )
.
(Range, E r r o r ..)
VBA TypeName. .
-
.
V a r i a n t , ,
.
(, # ! /0), MySum Excel .

258

10.

Excel , 0,
(.. , ).
, MySum ,
( VBA I s N u m e r i c } .
- : I n t e r s e c t
, . ,
, .
, MySum.
, MySum ,
. 1000- , . MySum. 12 . MySum ,
, .
, , .
, , ,
, Excel.


. (# !). , 11
, '' .

MsgBox, . ,
- , . , ,
- ( ).

, , .
, ( ), .

.
.
, ,
, Debug^Toggle Breakpoint ( <F9>).

D e b u g . P r i n t ,
VBE Immediate. , , .
F u n c t i o n VowelCount(r)
Count = 0
For i = 1 Len(r)
Ch - UCase(Mid(r, i, 1))
If Ch Like " [AEIOU]n Then
Count = Count + 1
Debug.Print Ch, i
End If

111. Visual Basic for Applications

Next i
VowelCount = Count
End Function
, Ch i, Immediate
, D e b u g . P r i n t . . 10.6 ,
M i s s i s s i p p i .

. 10.6. Immediate


Excel .
(. 10.7).
,
. , , .
;,
.
, ,
( ).
. 10.7.
,
P r i v a t e , (
). VBA, ,
P r i v a t e .
Excel, , Excel 2002
. Excel 2002 , -
. , , VBA.


, Excel
. , , , VBA.

260

10.

C o m m i s s i o n
.
Appli.tion.MacroOptions Macro: = "Commission" , Category: =1
( ,
).
.
. 10.1 , VBA.
, ( 10 13)
.
. .

- 10.1.

10

11

12

13

14

15




(. 10.8), : VBA.

. 10.8. Excel

. Visual Basic for Applications

261

,
. , .


, .
1. VBE.
2. Excel, ^ ^ .
,
.
3- .
4. , .
5. , . 10.9.
.
6. , .
. 10.9.

-
, 5.
, , 24.
, .
. , .

VBA
VBA.
C o m m i s s i o n :
1icat ion.MacroOptions _
Macro:= "Commission", _
Description:= " "
( , ).



. :
.

262

10.

ZapSpaces, Myfuncs . x l s .
( Myfuncs . xls),
=Myfuncs.xls!ZapSpaces(Al:C12)
My t u n e s , x l s Excel, , , :
=ZapSpaces(Al:C12)
21.

Windows API
VBA ,
Excel VBA , DLL (Dynamic Link Library ), Windows . VBA
, .
Windows API (Application Programming Interface )
, Windows.
Windows VBA Windows API. Windows, Windows, DLL,
, ,
.
Excel, , DLL. e x c e l . e x e ,
DLL, .
Excel . , , .
DLL . ,
Windows . Windows
DLL, .
, , DLL,
.
, DLL VBA. Visual Basic Microsoft
DLL, Excel.

Windows API
Window's API,
. VBA
U s e r F o r m , ), API P r i v a t e .
API ; . VBA .

III. Visual Basic for Applications

263

API .
API.
API.
API VB .

Windows
API.
Declare Function GetWindowsDirectoryA Lib "kernel32" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
, , ,
Windows ( VBA ). Windows lpBuffer,
enSize.
Declare GetWindowsDirectoryA.
:
Sub ShowWindowsDir()
Dim WinPath As S t r i n g
Dim WinDir As String
WinPath = Space(255)
'
WinDir = Left(WinPath, GetWindowsDirectoryA _
(WinPath, Len(WinPath)))
MsgBox WinDir, vblnformation, " Windows"
End Sub
ShowWindowsDir
Windows. Windows
: \Windows, . Windows NT , ,
: WINNT.
(wrapper) API. , , API. API. VBA:
Function
WindowsDir{) As String
1
Windows
Dim WinPath As String
WinPath = Space<255)
WindowsDir = Left(WinPath, GetWindowsDirectoryA _
(WinPath, Len(WinPath)))
End Function
.
MsgBox WindowsDir
:
=WindowsDir{)
API ,
(, , ). Windows,
is Excel VBA ,
, , , Windows API,
.

264

10.

<Shift>
. , VBA, . , -, <Shifi>.
<Shift>, API G e t K e y S t a t e .
G e t K e y S t a t e , .
, n V i r t K e y , .
, , , B u t t o n _ C l i c k <Shift>. ,
<Shift> ( ), G e t K e y S t a t e . G e t K e y S t a t e
0, , <Shift> ; <Shift> .
Declare Function GetKeyState Lib "user32"
(ByVal nVirtKey As Long) As Integer
Sub Button_Click{)
Const V K _ S H I F T As Integer = &H10
If GetKeyState(VK_SHIFT) < 0 Then
MsgBox "Shi ft "
Else
MsgBox "Shift "
End If
End Sub
Web- ,
( ): <Ctrl>, <Shift>, <Alt>.

API
Windows API .
API . , ,
. Excel API . Internet ,
.
11 Windows API

Web- win32api.
t x t , Windows API.
VBA.
API so
, .

III. Visual Basic for Applications

2S5

, Excel, "" , API. , Excel 97 ,


API, Excel 5
Excel 5 { Excel 5 16- ).
Excel 97 32- . 26.


VBA- VBA.
, , Windows API.
, , .

10.

Ha VBA

, , ,
. VBA. , ,
, ,
. ,
VBA.
, Excel.

, , ,
.
,
VBA.
, ,
.

VBA.

, VBA.

, .

Windows API.

,
, , ..


. , ,
.
, . , .


VBA
Excel.
Web- .


Excel ,
, . ,
, , .
,
VBA-.
Sub M a c r o l ( J
Range("Al")-Select
Selection.Copy
Range("Bl").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
, . VBA
. ,
, S e l e c t .
, , .
Sub CopyRange()
Range("Al").Copy Range{"Bl")
End Sub
, , .
, .
1 F i l e . x l s 2 F i l e 2 . x l s . , , .
Sub CopyRange2 <)

Workbooks("Filel.xls").Sheets{"I ).Range("Al").Copy
_
Workbooks("File2.xls").Sheets("2").Range{"Al"}
End Sub

268

11, VBA


, .
Sub ()
Set Rngl = Workbooks("Filel.xls"). _
Sheets("1").Range("Al")
Set Rng2 = Workbooks{"File2.xls"). _
Sheets{"2").Range("Al")
Rngl.Copy Rng2
End Sub
, . ,
. , ( ):
Sub CopyRange4()
Range("Al:C800").Copy Range("Dl")
End Sub


VBA
(. ). ,
C u t . ,
.
18 ( 1 : 6) ,
HI:
Sub MoveRange()
Range("Al:C6").Cut Range("HI")
End Sub


,
( ). , ,
.
.
. 11.1 .
, . , ,
, .

fwc. //./.

, 1
2 ( 1). C u r r e n t R e g i o n ,
Range,
( 1).

. Visual Basic for Applications

269

Sub CopyCurrentRegion2i)
Range<nAl").CurrentRegion.Copy
Sheets("2"J.Range("Al")
End Sub
currsntRegion :
^, ,
. ,
. , CurrentRegion ,
.


, VBA, ,
.
Excel , (
<Ctrt+Shift+> >), .
Excel 2002 ,
.
, , , .
C u r r s n t R e g i o n ( ), End Range, End. , , . , ,
.
Range(ActiveCell, ActiveCell.End(xlDown)).Select
, : x l u p (), x l T o L e f t () x l T o R i g h t ().
End. ,
End .
Web- , . ,
Selection Demo, , (. 11.2).

. 11.2. ,

VBA

270

11. VBA

. S e l e c t C u r r e n t R e g i o n
<Ctrl+Shift+*>:
Sub SelectCurrentRegion()
ActiveCell.CurrentRegion.Select
End, Sub
.
(, ). . , ,
S e l e c t C u r r e n t R e g i o n ,
Range . ,
Web-, :
Sub FormatCurrentRegion()
Set WorkRange = ActiveCell.CurrentRegion
WorkRange.Font.Bold = True
End Sub

.
.
, . , A c t i v a t e Worksheets.
.
, .
VBA . ,
Range ( " T o t a l " ) , Range (" D4 5"). 45 . ,
(D46).
,
(, <Ctrl+Shift+> > ), . Excel .
, ,
, . , . , , .
Excel . , , <Ctrl> .
.


, :
1 :
Sub GetValuel()
Range("Al").Value = InputBox(" ")
End Sub
. 11.3 .
III. Visual Basic for Applications

171

. }}.3. InputBox
,

. (Cancel) , ,
.
(Cancel) : .
Sub GetValue2{)
UserEntry = InputBox(" ")
If UserEntry <> "" Then Range("Al").Value = UserEntry
End Sub
, .
, I 12.
, . , .
, (Cancel).
Sub GetValue3{)
Dim MinVal As I n t e g e r , MaxVal As I n t e g e r
Dim UserEntry As S t r i n g
Dim Msg As S t r i n g
Dim I n t E n t r y As I n t e g e r
MinVal = 1
MaxVal = 1 2
Msg = " " & MinVal & " " & MaxVal

Do

UserEntry = InputBox(Msg)
If UserEntry = "" Then Exit Sub
If IsNumeric(UserEntry) Then
IntEntry = CInt(UserEntry}
If IntEntry >= MinVal And IntEntry <= MaxVal Then
Exit Do
End If
End If
Msg = " ."
Msg = Msg & vbNewLine
Msg = Msg & " * & _
MinVal & " " & MaxVal
Loop
ActiveSheet-Range("Aln).Value = UserEntry
End Sub
. 11.4, , .

. 11.4. ,
., VBA InputBox

272

11. VBA



. , (. 11.5).

/*. /J.-5".

Sub GetDatai)
Dim NextRow As Long
Dim Entryl As String, Entry2 As String
Do
NextRow = Range{"A65536").End(xlUp).Row + 1
Entryl = InputBoxf" "}
If Entryl = "" Then Exit Sub
Entry2 = InputBox(" ")
If Entry2 = "" Then Exit Sub
Cells(NextRow, 1) = Entryl
Cells(NextRow, 2) = Entry2
Loop
End Sub
, , (
Cancel) E x i t Sub.
, .

, NextRow. ,
: 65536 ( ), <End:>
. .
Row ; ( ), 1.
, : ,
2.

///. Visual Basic for Applications

273

,

, , , . Excel I n p u t B o x .
, , ,
.
Sub GetUserRange()
Dim, UserRange As Range
Output = 565
Prompt = " ."
Title = " "
'


On Error Resume Next
Set UserRange = Application.InputBox( _
Prompt:=Prompt, _
Title:=Title, _
Default:=ActiveCell.Address,
_
Type:=8) '
On Error GoTo 0
1
,
If UserRange Is Nothing Then
MsgBox " ."
Else
UserRange.Range("Al") = Output
End If
End Sub
, I J..6.

. 11.6.

8.
, On E r r o r Resume N e x t .
, ,
. u s e r R a n g e .
" ". , . On E r r o r Resume N e x t .
. Excel .
, screenupdating.
.

274

11. VBA


, , Count, ,
( ). ,
, :
MsgBox Selection.Count
data, data CellCount:
CellCount = Ranget"data"I.Count
, . .
Selection.Columns.Count

Rows. d a t a
RowCounc.
RowCount = Range("data").Rows.Count


Excel .
.
.
.
.
.
( ).
, VBA ,
- .
Range .
, , Areas, Areas.
.
, ,
, :
NumAreas Selection.Areas.Count
NumAreas ,
.
AboutRangeSelection , :
Function
AreaType(RangeArea As Range) As String
1

Select Case True
Case RangeArea.Cells.Count = 1
AreaType = "Cell"
Case RangeArea.Count - Cells.Count

III. Visual Basic for Applications

275

AreaType = "Worksheet"
Case RangeArea.Rows.Count = Cells.Rows.Count
AreaType - "Column"
Case RangeArea.Columns.Count = Cells.Columns.Count
Are a Type = "Row"
Case Else
AreaType = "Block"
End Select
End Function
R a n g e
, : , , , . S e l e c t C a s e
, T r u e . , ,
C e l l . , W o r k s h e e t .
, Column.
, Row.
C a s e T r u e , B l o c k .
, . , 65536 , c e l l s . c o u n t .
Excel 5 Excel 97 (
16384 ).
Web- (
AboutRangeSelection), AreaType
, (. 11.7).
Range.

. .7. AboutR&ngeSelect ion



, , Excel
. , , <Ctrl>, 1,
. AboutRangeSelection.


, , , . 11.1 :

276

11. VBA

S e l e c t i v e C o l o r l , .
.
11.1.
Sub S e l e c t i v e C o l o r l ( )
1
,
If TypeName(Selection) <> "Range" Then Exit Sub
Const REDINDEX = 3
Application.ScreenUpdating = False
For Each c e l l In S e l e c t i o n
If c e l l . V a l u e < 0 Then
c e l l . I n t e r i o r . C o l o r l n d e x = REDINDEX
Else
c e l l . I n t e r i o r . C o l o r l n d e x = xlNone
End If
Next c e l l
End Sub
S e l e c t i v e C o l o r l , . , , ? ?
? , , , .
(SelectiveColor2) 11.2.
11.2. S e l e c t i v e C o l o r l

Sub SelectiveColor2()
1
,
Dim FormulaCells As Range
Dim ConstantCells As Range
Const REDINDEX = 3
'


On Error Resume Next
Application.ScreenUpdating = False

'


Set FormulaCells = Selection.SpecialCells _
(xlFormulas, xlNumbers)
Set ConstantCells = Selection.SpecialCells _
(xlCons tants, xlNumbers)


If Not FormulaCells Is Nothing Then
For Each cell In FormulaCells
If cell.Value < 0 Then _
cell.Font.Colorlndex = REDINDEX
Next cell
End If

'


If Not ConstantCells Is Nothing Then
For Each cell In ConstantCells
If cell.Value < 0 Then

///. Visual Basic for Applications

277

c e l l . I n t e r i o r . C o l o r I n d e x = REDINDEX
Else
c e l l . I n t e r i o r . C o l o r I n d e x = xlNone
End If
Next c e l l
End If
End Sub
,
. S p e c i a l C e l l s : ,
.
F o r E a c h - N e x t . , .
On Error , SpecialCells , . , .


. , ,
" ", UsedRange Worksheet.
Sub DeleteEmptyRows{)
Dim LastRow As Long, r As Long
LastRow - ActiveSheet.UsedRange.Rows.Count
LastRow = LastRow + ActiveSheet.UsedRange.Row - 1
Application.ScreenUpdating = False
For r = LastRow To 1 Step -1
If Application.CountAfRows(r)) = 0 Then Rows{r).Delete
Next r
End Sub

LastRow. , ,
1. , LastRow : 1.
Excel (COUNTA) ,
. 0, . , For-Next. ,
"" . , .

,

InRange, , , Range. , .

278

11, VBA

Function InRange{rngl, rng2) As Boolean


1
True, rngl rng2
InRange = False
If rngl.Parent.Parent.Name = rng2.Parent.Parent.Name Then
If rngl.Parent.Name - rng2.Parent.Name Then
If Union(rngl, rng2).Address - rng2.Address Then
InRange = True
End If
End If
End If
End Function
, I n R a n g e , 1,

. , P a r e n t .
- . ,
r n g l :
rngl.Parent.Name
r n g l :
rngl.Parent.Parent.Name
VBA I n t e r s e c t i o n Range,
Range. .
, ,
,


Excel , , . , . , VBA IsEmpty, I s D a t e I s N u m e r i c .
C e l l T y p e . -
(, , . , , ),
.
VBA.
F u n c t i o n CellType(Rng)
'

'

Application.Volatile
Set Rng = Rng.Range("Al")
Select Case True
Case IsEmpty{Rng)
CellType = ""
Case WorksheetFunction.IsText(Rng)
CellType = ""
Case WorksheetFunction.IsLogical(Rng)
CellType - " "
Case WorksheetFunction.IsErr(Rng!
CellType - ""
Case IsDate(Rng)
CellType = ""
Case InStrd, Rng.Text, ":") <> 0
CellType = ""
Case IsNumeric(Rng)

III. Visual Basic^rJpplications^

279

CellType = ""
End Select
End Function
S e t Rng. C e l l T y p e
- , , .


, ,
, . , Excel , . W r i t e R e a d R a n g e , 11.3, .
F o r - N e x t
. T i m e r , .

11.3.

Sub WriteReadRange()
Dim MyArray()
Dim Timel As Date
Dim NumElements As Long, i As Long
Dim WriteTime As String, ReadTime As String
Dim Msg As String
NumElements = 60000
ReDinv MyArray (1 To NumElements)
'


For i = 1 To NumElements
MyArray(i> = i
Next i

'


Timel = Timer
For i = 1 To NumElements
Cellsfi, 1) = MyArrayli)
Next i
WriteTime = Format(Timer - Timel, "00:00")


Timel = Timer
For i = 1 To NumElements
MyArray(i) - Cells(i( 1)
Next i
ReadTime = FormatfTimer - Timel, "00:00")


Msg ": " & WriteTime
Msg = Msg & vbCrLf
Msg = Msg & ": " & ReadTime
MsgBox Msg, vbOKOnly, NumElements & " "
End Sub

280

11. VBA

60000 15 , 4 , .



F o r - N e x t .
.
11.4,
( ) . F o r - N e x t .

11.4.
Sub LoopFillRange()
1

Dim
Dim
Dim
Dim

CellsDown As Long, CellsAcross As Integer


CurrRow As Long, CurrCol As Integer
StartTiine As Date
CurrVal As Long


CellsDown = Val(InputBoxt" ?"})
CellsAcross = Val(InputBoxf" ?"))

'


StartTime = Timer

'


CurrVal = 1
Application.ScreenUpdating = False
For CurrRow = 1 To CellsDown
For CurrCol = 1 To CellsAcross
ActiveCell.Offset(CurrRow - 1, _
CurrCol - 1).Value = CurrVal
CurrVal = CurrVal + 1
Next CurrCol
Next CurrRow

'


Application.ScreenUpdating = True
MsgBox Format(Timer - StartTime, "00.00") & " seconds"
End Sub
11.5 . .

11.5.
Sub ArrayFillRange()
1

Dim CellsDown As Long, CellsAcross As Integer

III. Visual Basic for Applications

281

Dim
Dim
Dim
Dim
Dim
1

i As Long, j As Integer
StartTime As Date
TempArrayf) As Long
TheRange As Range
CurrVal As Long


CellsDown = Val(InputBox(" s ?"))
CellsAcross = Val(InputBox(" "))
1


StartTime = Timer


ReDim TempArray(l To CellsDown, I To CellsAcross)


Set TheRange - ActiveCell.Range(Cells(1,
Cells{CellsDown, CellsAcross)}

1), _


CurrVal =
Application.ScreenUpdating = False
For i = 1 To CellsDown
For j = 1 To CellsAcross
TempArray(i, j) = CurrVal + 1
CurrVal = CurrVal 4 1
Next j
Next i


TheRange.Value = TempArray


Application.ScreenUpdating - True
MsgBox Format(Timer - StartTirre, "00.00") & " "
End Sub
, 500x256 (12S000 )
202.34 . 0,77 250 ! ? Excel, .


, ( ).
..
. , .
Excel (TRANSPOSE). 100-
{hi :100).
Range(1:100}.Value = _
Application.WorksheetFunction.Transpose(MyArray)

282

11. VBA

Variant
Excel VI! .
, , V a r i a n t .
V a r i a n t .
Sub RangeToVariantf}
Dim x As Variant
x = Range("Al:L60Q")
MsgBox UBound(x, 1)
MsgBox UBoundfx, 2)
End Sub
600 ( ), 12 ( ). ,
V a r i a n t .
V a r i a n t , V a r i a n t
.
Sub R a n g e T o V a r i a n t 2 ( }
Dim UserRange As Range
Dim x As Variant
Dim r As Long, As Integer
Set UserRange = Range("Al:L600")
'


x = Range("Al:L50")


For r = 1 To TJBound{x, 1)
For = 1 To UBound{x, 2)
Multiply by 2
x(r, c) = x(r, c) * 2
Next
Next r


Range("Al:L50") =
End Sub
.


GoToMax 11.6 ,
. ; , .
Fin.d.

11.6. ,
Sub GoToMax()

Dim WorkRange as Range

III. Visual Basic for Applications

283

Dim MaxVal as Double


,
If TypeName(Selection) <> "Range" Then Exit Sub

'

, ;
-
If Selection.Count = 1 Then
Set Workrange = Cells
Else
Set Workrange = Selection
End If


MaxVal = Application.Max(Workrange)


On Error Resume Next
Workrange.Find{what:=MaxVal,
After:=Workrange.Range("Al"), _
Lookln:=xlValues, _
LookAt:=xlPart, _
SearchCrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False _
).Select
If Err <> 0 Then MsgBox " : " _
& MaxVal
End Sub
, , F i n d
Excel .

F i n d F o r m a t
, .
, , ..
FindFormat Excel 2002. ,
Excel.
SelectByForcnat .
Sub SelectByFormatU
1

1

Excel 2 002
If Val(Application.Version) < 10 Then
MsgBox " Excel 20 02 ."
Exit Sub
End If
Dim FirstCell As Range, FoundCell As Range
Dim AllCells As Range

284

11. VBA

'


With Application.FindFormat
.Clear
.Interior.Colorlndex = 6 '
.Font.Bold = True
End With


Set FirstCell = Cells.Find(What:="", SearchFormat:=True)

,
If FirstCell Is Nothing Then
MsgBox " .
Exit Sub
End If

AllCells
Set AllCells = FirstCell
Set FoundCell = FirstCell

'

, FirstCell
Do
Set FoundCell = Cells.FindNext(After:=FoundCell}
Set AllCells = Union(FoundCell, AllCells)
If FoundCell.Address = FirstCell.Address Then Exit Do
Loop

'


AllCells.Select
MsgBox " : " & A l l C e l l s . C o u n t
End Sub
F i n d F o r m a t . :
. , .
F i n d , . What F i n d , , . , S e a r c h F o r m a t True, , .
, , . A l l C e l l s ( ).
FindNext , ,
. , ,
.
, Excel.



VB .

///. Visual Basic for Applications

285

Web- .


Workbooks
, .
Public Sub SaveAllWorkbooks()
Dim Book As Workbook
For Each Book In Workbooks
If Book.Path <> "" Then Book.Save
Next Book
End Sub
, P a t h . -
P a t h , , (
). ,
P a t h .


Workbooks.
.
Sub CloseAllWorkbooksO
Dim Book As Workbook
For Each Book In Workbooks
If Book.Name <> ThisWorkbook.Name Then
Book,Close savechanges:=True
End I f
Next Book
ThisWorkbook.Close savechanges:= True
End Sub
, if ,
, , , , , .


Excel ^ ,
. VBA. , & .
Sub LastSavedf)
Dim SaveTime As String
On Error Resume Next
SaveTime = ActiveWorkbook. _
BuiltinDocumentProperties("Last Save Time"}.Value
If SaveTime = "" Then

286

11. VBA

Else

MsgBox ActiveWorkbook.Name & " ."

MsgBox ": " & SaveTime,


End If
End Sub

, ActiveWorkbook.Name

, Last
Save Time . On- E r r o r .
I f - T h e n - E l s e SaveTime
. , , . . 11.8 .
. 11.8.
, VBA, Excel.
.


, , , ,
, Excel "" . ,
, . VBA, ,

.
, .
, .
.
Sub SynchSheets()

1

If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
Dim UserSheet As Worksheet, sht As Worksheet
Dim TopRow As Long, LeftCol As Integer
Dim UserSel String
Application.ScreenUpdating = False
1


Set UserSheet = ActiveSheet


TopRow = ActiveWindow.ScrollRow
LeftCol = ActiveWindow.ScrollColumn
UserSel = ActiveWindow.RangeSelection.Address

For Each sht In ActiveWorkbook.Worksheets
If sht.Visible Then '
sht.Activate
Range{UserSel}.Select

III. Visual Basic for Applications

287

ActiveWindow.ScrollRow = TopRow
ActiveWindow.ScrollColumn = LeftCol
End If
Next sht
1


UserSheet.Activate
Application.ScreenUpdating = True
End Sub

VBA
! VBA,
.
, , Web- .

Boolean
B o o l e a n , :
T r u e () F a l s e (), N o t , , W r a p T e x t .
Sub ToggleWrapText()
1

If TypeName(Selection) = "Range" Then
Selection.WrapText = Not ActiveCell.WrapText
End If
End Sub
, . , (, , ), , Excel
. , , ,
.
Excel.
, TypeName , . , .
N o t . , :
ActiveWindow.DisplayHeadings = Not _
ActiveWindow. Display-Headings
:
ActiveWindow.DisplayGridlines = Not _
ActiveWindow.DisplayGridlines

288

11. VBA


, Excel , . VBA :
Sub PageCount()
MsgBox (ActiveSheet.HPageBreaks.Count + 1) * _
(ActiveSheet-VPageBreaks.Count + I)
End Sub
VBA
.
Sub ShowPageCount()
Dim PageCount As Integer
Dim sht As Worksheet
PageCount = 0
For Each sht In Worksheets
PageCount = PageCount + (sht.HPageBreaks.count + 1 ) * _
(sht.VPageBreaks.Count + 1)
Next sht
MsgBox " = " & PageCount
End Sub


, Excel ,
VBA.
D a t e A n d T i m e
(. 11.9). .
, 11.7, F o r m a t D a t e .
. .
. 11.9. ,

11.7.
Sub DateAndTime()
TheDate = Format(Date, "Long Date")
TheTime = Format(Time, "Medium Time")
1

'


Select Case Time
Case Is < Tin\eValue{ "12:00"): Greeting = " .
Case Is >= TimeValue("17:00"): Greeting = " .
Case Else: Greeting = " , "
End select

III. Visual Basic for Applications

289

FullName = Application.UserName
SpacelnName = InStrd, FullName, " ", 1}
'

,
If SpacelnName = 0 Then SpacelnName = Len(FullName)
FirstName = Left(FullName, SpacelnName)
Greeting = Greeting & FirstName


MsgBox TheDate & vbCrLf & TheTime, vbOKOnly, Greeting
End Sub
("Long Date" "Medium Time")
, . ,
//, :
T h e D a t e = F o r m a t ( D a t e , "mm/dd/yy"
,
, S e l e c t C a s e . VBA
, Excel. 0,5 (), . 0,7083
(5 ), . .
VBA TimeValue, .
, . VBA I n S t r . ,
, . ,
Nobody,
, ,
. ( , , Excel User). S p a c e l n N a m e
, L e f t .
MsgBox , vbCrLf
. vbOKOnly , 0; . G r e e t i n g , .


, , Excel
.
.
. F o n t C o n r o l .
(, ), CommandBar, .
CommandBar
22.

290

11. VBA

Sub ShowInstalledFonts()
Dim FontList As CommandBarControl
Dim TempBar As CommandBar
Dim i As Integer
Set FontList = Application.CommandBars("Formatting"). _
FindControl(ID:=1728)
1

,
If FontList Is Nothing Then
Set TempBar = Application.CommandBars.Add
Set FontList = TempBar.Controls.Add(ID:=1728)
End If


Range("A:A").ClearContents
For i = 0 To FontList.ListCount - 1
Cells(i + 1, 1) = FontList.List(i + 1)
Next i

'

,
On Error Resume Next
TempBar.Delete
End Sub


, Excel , VBA . , ,
Excel, .
, VBA .
.

. Excel;
. .
65536 ( ).

( 9). ,
, .

. , ,
, , .

. ,
.
Web- , . .

III. Visual Basic for Applications

291

. 11,10 . ( 100 100000 ); ( D o u b l e ) .

. JhlO. ,

. 11.1 . 0,00 ,
, , 0,01 .

11.1. ,

-
Excel
V3A


VBA

100

0,05

0,00

0,05

0,00

500

0,06

0,11

0,05

0,00

1000

0,11

0,44

0,11

0,00

5000

0,55

8,89

0,77

0,05

10000

1.16

31,69

1,75

0,06

50000

6,98

788,62

100000

10,21

0,22

20,60

0,44

, . . 11.2.

11.2.

-
Excel
VBA

KaVBA


VBA

100

0,05

0,00

0,00

0,00

500

0.05

0,11

0,00

0,00

1000

0,11

0,27

0,11

0,00

5000

0,33

7,09

0,55

0,05

10000

1.15

32,02

1,70

0,06

50000

3,35

718,52

9,23

0,22

100000

20,60

0,44

292

11. VBA

, ,
: .
, ,
, .
, .



. .8 , , . ,
, .
, .

11.8. ,
Sub BatchProcess{)
Dim FS As FileSearch
Dim FilePath As String, FileSpec As String
Dim i As Integer
1


FilePath = Thisworkbook.Path & "\"
FileSpec = "text77.txt"

FileSearch
Set FS = Application.FileSearch
With FS
.WewSearch
.Lookln = FilePath
-FileName = FileSpec
.Execute
,
If -FoundFiles-Count = 0 Then
MsgBox "No files were found"
Exit Sub
End If
End With


For i = 1 FS.FoundFiles.Count
Call ProcessFiles(FS.FoundFiles(i))
Next i
End Sub
,
Web-: TextOl. t x t , TextO2 . t x t TextO3 . t x t .
, . FileSearch, Excel 2000 .

. Visual Basic for Applications

293

F i l e S e a r c h , For-Next.
P r o c e s s F i l e s , .
OpenText . ,
, .
Sub P r o c e s s F i l e s (FileNeune As s t r i n g )
1

Workbooks.OpenText Filename:=FileMame, _
Origin : =xlWindows, __
StartRow:=l, _
DataType:=xlFixedWidh, _
Fieldlno:=
Array(Array(0, 1 ) , Array{3, 1 ) , Array(12, 1J)
1

Range("Dl").Value = "A"
Range("D2") .
Range ("D3") . Va 1U'
Range {"El :E3") .F<
Range ("F1:F3") .
End Sub

,
VBA
"" ,
. ,
VBA. , ; P r i v a t e Excel .
, , Web- ,

FileExists
( ) ,
.
Private Function FileExists(fname) As Boolean
' ,
FileExists = {Dir(fname) <> "")
End Function

FileNameOnly
( ) .
, .
Private Function FileNameOnly(pname) As String
1
/

294

11. VBA

Dim i As Integer, length As Integer, temp As String


length = Len(pname)
temp = ""
For i = length To 1 Step -1
If Mid{pname, i, 1) = Application.PathSeparator Then
FileNameOnly = temp
Exit Function
End If
temp = Mid f pname, i, 1) & temp
Next i
Fi 1 eNameOn ly = pnarae
End Function
F i l e N a m e O n l y (
). ,
.
P r i v a t e Function FileNameGnly2(pname) As String
FileName0nly2 = Dir(pname)
End Function

PathExists
() , .
P1 r i v a t e Function PathExists{pname) As Boolean
,
Dim x As String
On Error Resume Next
x = GetAttr(pname) And 0
If Err = 0 Then PathExists = True _
Else PathExists = False
End Function

RangeNameExists
( ) , .
P1 r i v a t e Function RangeNameExists(nname) As Boolean
,
Dim n As Name
RangeNameExists = False
For Each n In ActiveWorkbook.Names
If UCase(n.Name) = UCase(nname) Then
RangeNameExists = True
Exit Function
End If
Next n
End Function

SheetExists
( ) ,
.
P r i v a t e Function SheetExists(sname) As Boolean
,

///. Visual Basic for Applications

295

Dim x As
On Error
Set x a
If Err =
Else
End Function

Object
Resume Next
ActiveWorkbook.Sheets(sname)
0 Then SheetExists = True _
SheetExists = False

WorkbooklsOpen
( ) ,
.
Private Function WorkbooklsOpen(wbname) As Boolean
,
Dim As Workbook
On Error Resume Next
Set x = Workbooks(wbname)
If Err = 0 Then WorkbooklsOpen = True _
Else WorkbooklsOpen = False
End Function

"" ,
, .
P r i v a t e Function I s I n C o l l e c t i o n ( C o i n As O b j e c t , _
Item As S t r i n g ) As Boolean
Dim Obj As Object
On Error Resume Next
Set Obj = Coin(Item)
I s I n C o l l e c t i o n = Not Obj
End Function

Is Nothing

: () (), . , . ,
True; False.
I s I n C o l l e c t i o n , : RangeNameExists, SheetExists WorkbooklsOpen. , Data, I s I n C o l l e c t i o n

MsgBox IsInCollection{ActiveWorkbook.Names, "Data")

, Budget,
MsgBox I s I n C o l l e c t i o n { W o r k b o o k s , " b u d g e t . x l s " i
, 1,
MsgBox IsInCollection(ActiveWorkbook.Worksheets, "!")


VBA .
, Excel, VBA (GetValue, ),
. XLM

296

11. VBA

Private Function GetValue(path, file, sheet, ref)


1

Dim arg As String
'


If Right(path, 1) <> "\" Then path path & " \"
If Dir(path & file) = "" Then
GetValue = " "
Exit Function
End If


arg =
& path & " [ " & file & " ] " & sheet & '"!" & _
Range(ref).Range("Al").Address(, , xlRlCl)

XLM
GetValue = ExecuteExcel4Macro{arg)
End Function
GetValue ;
p a t h (, "d: \f i l e s " ) ;
f i l e (, "budget . x l s " ) ;
s h e e t (, "1");
r e f (, "4").
, GetValue. 1 1 9 9Budget . x l s ( XLFiles\
Budget :}.
Sub TestGetValue{)
= "C:\XLFiles\Budget"
f = "99Budget.xls"
s = "1"
a = "Al"
MsgBox GetValue(p, , s, a)
End Sub
. 1200 (100 12
) .
Sub TestGetValue2
= "c:\XLFiles\Budget"
f = "99Budget.xls"
= "Sheetl"
Application.ScreenUpdating = False
For r = 1 To 100
For = 1 To 12
a = Cells(r, c}.Address
Cellstr, c) = GetValue(p, f, s, a)
Next
Next r
Application,ScreenUpdating = True
End Sub
Getvalue , . .
.

. Visual Basic for Applications

297

Excel
, , ,
. , VBA (
(ThisWorkbook), .).
Web- .


,
0 .
(, , , , ).
, Excel.
(
), <Ctrl+Alt+F9>,
, , ,
.
Function ISBOLD(cell) As Boolean
1
,
ISBOLD = cell.Range("Al").Font.Bold
End Function
, () .
Function ISITALIC(cell) As Boolean
1
,
ISITALIC = cell.Range("Al").Font.Italic
End Function
, , . , , , .
Function ALLBOLD(cell) As Boolean
1
,
'
If IsNull(cell.Font.Bold) Then
ALLBOLD = False
Else
ALLBOLD = cell.Font.Bold
End If
End Function
FILLCOLOR, , ,
( ). ,
4142.

298

11. VBA

Function FILLCOLOR(cell) As Integer


1
,
1

FILLCOLOR = cell.Rangef"Al").Interior.Colorlndex
End Function



Excel ,
B u i l t i n D o c u m e n t P r o p e r t i e s Workbook.
.

Function LASTSAVEDO
Application.Volatile
LAST/SAVED = ThisWorkbook. _
BuiltinDocumentProperties("Last Save Time")
End Function
, .
Function LASTPRINTED()
Application.Volatile
LASTPRINTED = ThisWorkbook. _
BuiltinDocumentProperties("Last Print Date")
End Function

( <Ctrl+Alt+F9>), .
, Excel . , , , .


, Excel :
. A p p l i c a t i o n .
Excel , , , ..
, Range.
A p p l i c a t i o n
Workbook
Worksheet
Range
- Range () Worksheet ( ), . Worksheet Workbook ( ), ,
Workbook A p p l i c a t i o n (, .. Excel).
? VBA
SheetName, . () , .
P a r e n t Range. P a r e n t , ,
Range.

HI. Visual Basic for Applications

299

Function SheetName(ref) As S t r i n g
SheetName = ref . Parent: .Name
End Function
, WorkbookName . , Parent . Parent
Worksheet, P a r e n t Workbook.
Function WorkbookName(ref) As String
WorkbookName = ref.Parent.Parent.Name
End Function
AppName, , , P a r e n t .
A p p l i c a t i o n . , M i c r o s o f t Excel.
Function AppNartiefref} As S t r i n g
AppName = ref.Parent.Parent.Parent.Name
End Function


COUNTBETWEEN ( ), , .
Function COUNTBETWEEN{InRange, numl, num2) As Long
numl num2
With Application.WorksheetFunction
COUNTBETWEEN = .Count!f(InRange, ">=" & numl) .CountIf(InRange, " >" & num2)
End With
End Function
, Excel (COUNTIF).
, COUNTBETWEEN ''", .
, COUNTBETWEEN. Al:A100, 10 20.
=COUNTBETWEEN<A1:A100;10;20)
VBA, .
*((1:100; ">=10"))-(1:100; ">=20")}


COUNTVISIBLE, , () . , .
Function COUNTVISIBLE(rng)
1

Dim CellCount As Long
Dim cell As Range
Application.Volatile
CellCount = 0
Set rng = Intersect(rng.Parent.UsedRange, rng)
For Each cell In rng

300

11. VBA

If Not IsEmpty(cell) Then


If Not cell.EntireRow.Hidden And _
Not cell.EntireColumn.Hidden Then _
CellCount = CellCount + 1
End If
Next cell
COUNTVISIBLE = CellCount
End Function
,
. , , .
, , , C e l l C o u n t
1.
COUNTVISIBLE
. .

Excel . ( 2 3) . ,
.



: LASTINCOLUMN , a LASTINROW . . ( LASTINCOLIMM)
( LASTINROW).
, . , :
=LASTINCOLUMN(B5)
7:
=LASTINROW(C7:D9)

LASTINCOLUMN
LASTINCOLUMN .
Function LASTINCOLUMN(rng As Range)
Application.Volatile
Set LastCell = rng.Parent.Cells(Rows.Count, rng.Column) _
.End(xlUp)
LASTINCOLUMN = LastCell.Value
If IsEmpty(LastCell) Then LASTINCOLUMN =
If rng.Parent.Cells(Rows.Count, rng.Column) <> "" Then _
LASTINCOLUMN = rng.Parent.Cells{Rows.Count, rng.Column)
End Function
, ,
.
A p p l i c a t i o n . V o l a t i l e , .

///. Visual Basic for Applications

301

Rows.Count .
, 6:5536, ( Excel - ).
r n g . Column rng.
r n g . P a r e n t , r n g
.
End ( xlup)
<End> "",
IsEmpty , . , . 0.
If . ,
.

LASTINROW
LASTINROW .
LASTINCOLUMN.
Function LASTINROW(rng As Range)
Application.Volatile
Set LastCell = rng.Parent.Cells(rng.Row, Columns.Count) _
.End(xlToLeft)
LASTINROW = LastCell.Value
If IsEmpty(LastCell) Then LASTINROW = ""
If rng.Parent.Cells(rng.Row, Columns.Count) <> Then _
LASTINROW = rng.Parent.Cells(rng.Row, Columns.Count)
End Function

?
ISLIKE ( ). ,
.
ISLIKE . , "", VBA Like.
Function ISLIKE{text As String, pattern As.String) As Boolean
' , ,
If text Like pattern Then ISLIKE = True _
Else ISLIKE = False
End Function
ISLIKE :
t e x t , ;
p a t t e r n , .
()
?
*
#
[_]
[! ^]




(0-9)
_
,
_

11. VBA

, * . , , :
= I S L I K E ( " g u i t a r " ; "*")
, ? . " U n i t 12", :
=ISLIKE("Unitl"; "Unit?")
,
:
=ISLIKE("a"; " ( a e i o u l " )
, Al : ,
, i, , u. A, E, I, , . UPPER
:
=ISLIKE(UPPER(A1)I U P P E R ! " [ a e i o u ] " ) )
, Al ,
1 (.. 100 199):
=ISLIKE(A1; " 1 # # " )

-
ExtractElement (
VBA), . ,
, ExtractElement
:
123-456-789-0133-8844
, , 0133, ..
. (-) :
=ExtractElement("123-456-789-0133-8844",4,"-")
ExtractElement :
Txt , .
:
, ;
S e p a r a t o r , .
- ,
, .
, .
VBA ExtractElement.
Function ExtractElement(Txt, n, Separator) As String
' - ,
' -.
Dim Txtl As String, TempElenient As S t r i n g
Dim ElementCount As I n t e g e r , i As I n t e g e r

III. Visual Basic for Applications

303

Txtl - Txt
- ,
If Separator = Chr(32) Then Txtl Application.Trim{Txtl)

'
1


If Right{Txtl, Len(Txtl)) <> separator Then _
Txtl = Txtl & Separator

'

ElementCount = 0
TempEleraent = ""


For i = 1 To Len(Txtl)
If Mid(Txtl, i, 1) = Separator Then
ElementCount = ElementCount + 1
If ElenientCount = n Then

n- ,
ExtractElement = TempElement
Exit Function
Else
TempElement = ""
End If
Else
TempElement = TempEleaient & Mid (Txtl, i, 1)
End If
Next i
ExtractElement=""
End Func t i on


, ,
, , - , VBA S t a t F u n c t i o n .
: (rncf) ().
, Excel:
(AVERAGE), (COUNT), (), (MEDIAN), (MIN),
(MODE), (STDEV), (SUM) (VAR).
, :
=STATFUNCTION(1 2 4;24)
24, :
, , .. .
Function STATFUNCTION(rng,
Select Case UCase(op)
Case "SUM"
STATFUNCTION =
Case "AVERAGE"
STATFUNCTION =
Case "MEDIAN"
STATFUNCTION =
Case "MODE"
STATFUNCTION =
Case "COUNT"

304

op)
WorksheetFunction.Sumfrng)
WorksheetFunction.Average(rng)
WorksheetFunction.Median(rng)
WorksheetFunction.Mode(rng)

11. VBA

STATFUNCTION
"MAX"
STATFUNCTION
Case "MIN"
STATFUNCTION
Case "VAR"
STATFUNCTION
Case "STDEV"
STATFUNCTION
Case Else
STATFUNCTION
End Select
End Function

= WorksheetFunction.Count(rng)

Case

= WorksheetFunction.Max(rng)
= WorksheetFunction.Min(rng)
= WorksheetFunction.Var(rng)
= WorksheetFunction.StDev(rng)
= CVErr(xlErrNA)

SHEETOFFSET: 1
, , Excel " ". , ,
. ,
. , , .
, , VBA (
SHEETOFFSET), . ,
1
=SHEETOFFSET(-1;1)
,
. .
, .
VBA SHEETOFFSET.
Function SHEETOFFSET<offset, Kef)
Application.Volatile
SHEETOFFSET = Sheets(Application.Caller.Parent.Index _
+ offset).Range(Ref.Address)
End Function
.
, .

SHEETOFFSET: 2
SHEETOFFSET. ,
, . SHEETOFFSET
, .
Function SHEETOFFSET2{offset, Ref)
Dim WBook As Workbook
Dim WksCount As Integer, i As Integer
Dim CallerSheet As String, CallerIndex As Integer
Application.Volatile
1

///. Visual Basic for Applications

305

Set WBook = Application.Caller.Parent.Parent


Dim Wks() As Worksheet
WksCount = 0
For i = 1 To WBook.Sheets.Count
If TypeName(WBook.Sheets(i)) = "Worksheet" Then
WksCount = WkaCount + 1
ReDim Preserve Wks(l To WksCount)
Set Wks{WksCount} = WBook.Sheets(i)
End If
Next i
1

,
CallerSheet = Application.Caller.Parent.Name
For i = 1 To UBound(Wks)
If CallerSheet = Wks(i).Name Then Callerlndex - i
Next i

'


SHEETOFFSET2 = Wks(Callerlndex + _
offset).RangefHef.Address)
End Function



1
,
=(!:4!1)
1 1 , 4
.
, 4 (5)?
, ,
:
={1:5!1)
MAXALLSHEETS, , ( ) . ,
1 :
=MAXALLSHEETS{1)
.
Function MAXALLSHEETS(cell)
Dim MaxVal As Double
Dim Addr As String
Dim wksht As Object
Application.Volatile
Addr = cell.Range("Al").Address
MaxVal = -9.9E+307
For Each Wksht In cell.Parent.Parent.Worksheets
If Wksht.Name = cell.Parent.Name And _
Addr - Application.Caller.Address Then
'
Else

306

11. VBA

If

IsNumeric{Wksht.Range(Addr)J Then
If Wksht.Range(Addr) > MaxVal Then _
MaxVal = Wksht.Range<Addr).Value
End If
End If
Next Wksht
If MaxVal = -9.9E+307 Then MaxVal = 0
MAXALLSHEETS = MaxVal
End Function
For Each
cell.Parent.Parent.Worksheets
, . , For Each .
If , , , . , .
,
: , ,
..



RANDOM INTEGERS, ,
.
. . . , Al:D10

{=RANDOMINTEGERS()}
<Ctrl+Shift+Ent.;r>.
, .
40 , 1 40.

. 11.11. .

. Visual Basic for Applications

307

RANDOMINTEGERS.
Function RANDOMINTEGERS()
Dim FuncRange As Range
Dim V() As Variant, ValArray(} As Variant
Dim CellCounc As Double
Dim i As Integer, j As Integer
Dim r As Integer, As Integer
Dim Tempi As Variant, Temp2 Variant
Dim RCount As Integer, CCount As Integer
Randomize
1

Range
Set FuncRange = Application.Caller

'

, FuncRange
CellCount = FuncRange.Count
If CellCount > 1000 Then
RANDOMINTEGERS = CVErr(xlErrNA)
Exit Function
End If

'


RCount = FuncRange.Rows.Count
CCount = FuncRange.Columns.Count
ReDim V(l To RCount, 1 To CCount)
ReDim ValArrayd To 2, 1 To CellCount)



For i = 1 CellCount
ValArrayd, i) = Rnd
ValArray<2, i) = i
Next i

'

ValArray no
For i = 1 To CellCount
For j = i + 1 To CellCount
If ValArrayd, i) > ValArrayd, j) Then
Tempi = ValArrayd, j)
Temp2 = ValArray(2, j)
ValArrayd, j) = ValArrayd, i)
ValArray(2, j) = ValArray(2, i)
ValArrayd, i) = Tempi
ValArray{2, i) = Temp2
End If
Next j
Next i

V
1 = 0
For r = 1 RCount
For = 1 CCount
i = i + 1
V(r, ) = ValArray(2, i)
Next
Next r
RANDOMINTEGERS = V
End Function

11. VBA



RANGERANTJGMIZE, ,
, .
F u n c t i o n KANGERANDOMIZE(rng)
Dim V() As Variant, ValArray() As Variant
Dim CellCount As Double
Dim i As Integer, j As Integer
Dim r As Integer, As Integer
Dim Tempi As Variant, Temp2 As Variant
Dim RCount As Integer, CCount As Integer
Randomize
1

,
CellCount = rng.Count
If CellCount > 1000 Then
RANGERANDOMIZE = CVErr(xlErrNA)
Exit Function
End If


RCount = rng.Rows.Count
CCount = rng.Columns.Count
ReDim V(l To RCount, 1 To CCount)
ReDim ValArrayd To 2, 1 To CellCount)

'

rng
For i = 1 CellCount
ValArrayd, i) = Rnd
ValArray(2, i) = rng(i)
Next i

'

ValArray
For i = 1 To CellCount
For j = i + 1 To CellCount
If ValArrayd, i) > ValArrayd, j) Then
Tempi = ValArrayd, j)
Temp2 = ValArray(2, j)
ValArray(1, j) - ValArrayd, i)
ValArray(2, j) = ValArray(2, i)
ValArrayd, i) = Tempi
ValArray{2, i) = Temp2
End If
Next j
Kext i

, V
i = 0
For r = 1 RCount
For = 1 CCount
i = i + 1
V(r, ) = ValArray(2, i)
Next
Next r
RANGERANDOMIZE = V
End Function

III. Visual Basic for Applications

309

RANGERANDOMIZE RANDOMINTEGERS.
. 11.12 RANGERAXTOOMIZE. F 1 6 : I 2 5
:
(=RANGERANDOMIZE(A16:D25>}
16: D25, .

. 11.12. RANGERRNDQMIZE

Windows API
VBA ,
(Dynamic Link Libraries DDL). Windows API.
API Excel. 32- API 16- Excel 5, . , 16-
32- Excel 95 .
32- Excel.
25.


Windows .
, .
G e t E x e c u t a b l e Windows API
, . , . t x t , R e a d m e , t x t
Windows. G e C E x e c u t a b l e ,
.

310

11. VBA

Windows API VBA.

PrivaCe Declare Function FindExecutableA Lib "shell32.dll"_


(ByVal lpFile As String, ByVal lpDirectory As String,_
ByVal lpResult As String) As Long
Function GetExecutable{strFile As String) As String
Dim strPath As String
Dim intLen As Integer
strPath = Space(255)
intLen = FindExecutableA(strFile, "\", strPath)
GetExecutable = Trim{strPath)
End Function
. 11.13 GetExecutable
: \windows\readme.txt. NOTEPAD. EXE.
. 11.13. ,


, , Windows API .
. .
Private Declare Function GetProfileStringA Lib "kernel32" _
(ByVal lpAppName As String, ByVal lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString As _
String, ByVal nSize As Long) As Long
Sub DefaultPrinterlnfoO
Dim strLPT As String * 255
Dim Result As String
Call GetProfileStringA _
("Windows", "Device", "", strLPT, 2 54)
Result = Application.Trim(strLPT)
ResultLength - Len(Result)
Commal = Application.Find{"
Cornma2 = Application. Find ("

Result, 1)
Result, Comma 1 + 1)


Printer = Left(Result, Commal - 1)

Driver = Mid(Result, Commal + 1, Comma2 - Commal - 1)

Port = Right{Result, ResultLength - Comma2)

III. Visual Basic for Applications

Msg = ":11 & Chr{9) & Printer & Chr{13)


Msg = Msg & ":" & Driver & Chr(13)
Msg = Msg & ":" & Chr(9) & Port
1


MsgBox Msg, vblnformation, " "
End Sub
A c t i v e P r i n t e r A p p l i c a t i o n ( ). , . .
. 11.14 ,
.
. 11.14.
Windows !


Windows API . , , , , ,
.
1
32- API-
Declare Function GetSystemMetries Lib "user32" _
(ByVal nlndex As Long) As Long

Public Const SM_CXSCREEN = 0


Public Const SM_CYSCREEN = 1
Sub DisplayVideolnfo()
vidWidth = GetSystemMetrics(SM..CXSCREEN)
vidHeight = GetSystemMetrics(SM_CYSCREEN)
Msg = " : "
Msg = Msg & vidWidth & " X " & vidHeight
MsgBox Msg
End Sub
. 11.15 ,
1024x768.
I ! 15. Windenvx API


Excel . VBA . API
WAV MIDI.
- , ,
C a n P l a y S o u n d s .

312

11. VBA

Sub CanPlaySound()
If Not Application.CanPlaySounds Then
MsgBox ", ."
End If
End Sub

WAV
API d o g b a r k . w a v (,
, ).
P r i v a t e D e c l a r e F u n c t i o n PlaySound L i b "winmm.dll" _
A l i a s "PlaySoundA" (ByVal lpszName As S t r i n g , _
ByVal hModule As Long, ByVal cVwFlags As Long) As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &20000
Sub PlayWAVO
WAVFile = "dogbark.wav"
WAVFile = ThisWorkbook.Path & "\" & WAVFile
Call PlaySound{WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub
WAV . , , .
,
C a l l PlaySound(WAVFile,

0&,

SND_SYNC Or SND_FILENAME)

MIDI
& MIDI,
API. MIDI PlayMIDI. StopMlDI
MIDI. x f i l e s . m i d .
Private Declare Function mciExecute Lib "winmm.dll" _
(ByVal lpstrCommand AS String) As Long
Sub PlayMIDI()
MIDlFile = "xfiles.mid"
MIDIFile = ThisWorkbook.Path & "\" & MIDlFile
mciExecute ("play " & MIDlFile)
End Sub
Sub StopMIDI{)
MIDlFile = "xfiles.mid"
MIDlFile = ThisWorkbook.Path & "\" & MIDlFile
mciExecute ("stop " & MIDlFile)
End Sub


Alarm, ,
. Windows API, , .

III. Visual Basic for Applications

Declare Function PlaySound Lib "winmm.dll" _


Alias "PlaySoundA" (ByVal lpszName As S t r i n g , _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
Function ALARM(Cell, Condition)
Dim WAVFile As S t r i n g
Const SND_ASYNC = &H1
Const SND_FILKNAME = &H20000
If E v a l u a t e ( C e l l . V a l u e & Condition) Then
WAVFile = ThisWorkbook.Path & "\sound.wav"
C a l l PlaySound(WAVFile, Ob, SND_ASYNC Or SND_FILENAME|
ALARM = True
Else
ALARM = F a l s e
End If
End Function
A l a r m : "" (
). , A l a r m WAV, 13 1000:
-ALARM <13,- " > = 1 0 0 0 " )
VBA E v a l u a t e , , . ( ),
, .


Windows
( 4). VBA
. Windows API.
Private Declare Function RegOpenKeyA Lib nADVAPi32.DLL" _
(ByVal hKey As Long, ByVal sSubKey As String, _
ByRef hkeyResult As Long) As Long
Private Declare Function RegCloseKey Lib "ADVAPI32.DLL" _
{ByVal hKey As Long) As Long
Private Declare Function RegSetValueExA Lib "ADVAPI32.DLL" _
(ByVal hKey As Long, ByVal sVeilueName As String, _
ByVal dwReserved As Long, ByVail dwType As Long, _
ByVal sValue As String, ByVa1 dwSize As Long) As Long
Private Declare Function RegCreatoKeyA Lib "ADVAPI32.DLL"
(ByVal hKey As Long, ByVal sSubKey As String,
ByRef hkeyResult As Long) As Long
Private Declare Function RegQueryValueExA Lib "ADVAPI32.DLL" _
(ByVal hKey As Long, ByVal sVialueMame As String, _
ByVal dwReserved As Long, ByRef lValueType As Long, _
ByVal sValue As String, ByRef IResultLen As Long) As Long
'<-"", : GetRegistry W r i t e R e g i s t r y . Web-
. ,
.

314

11. VBA


G e t R e g i s t r y .
:
RootKey

, , . :
HKEY_CLASSES_ROOT, HKEY_C0RRENT_USER, HKEY_LOCAL_MACHINE,
HKEY_USERS, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA

Path

RegEntry

. , G e t R e g i s t r y ( ,
):
RootKey = "hkey_current_user"
Path = "Control Panel\Colors"
RegEntry = "ActiveTitle"
MsgBox GetRegistry(RootKey, Path, RegEntry), _
vblnformation, Path & "\RegEntry"
, RGB.


W r i t e R e g i s t r y .
, ;
. W r i t e R e g i s t r y ( ).
RootKey

, , .
:
HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE,
HKEY_USERS, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA

Path

. ,

RegEntry

, .

RegVal

, ,
Excel. Excel.
Sub Auto_Open()
RootKey = "hkey_current_user"
Path = "software\microsoft\office\10.O\excel\LastStarted"
RegEntry = "DateTime"
RegVal = Now<)
If WriteRegistryfRootKey, Path, RegEntry, RegVal) Then
msg = RegVal & " ."
Else msg = " "
End If
MsgBox msg
End Sub

III. Visual Basic for Applications

315

,
Excel.

, VBA.
Excel .

316

11. VBA

, ( U s e r F o r m ) .
12 . 13 UserForm, , . 14 15
.

, ,
Windows. Windows
. , . , Excel,
U s e r F o r m .
,
U s e r Form,
Excel,
. .


...
.
:

;
;


, . , , , . I n p u t B o x : VBA, Excel.

InputBox VBA
:
I n p u t B o x ( [, ] [, _:] [, x p o s ] [ rypos] [, ,
])

, ( ).
( ).

^ ,
{ ).

xpos, ypos
( ).

, ( ).

I n p u t B o x . , .
, , 1024
( ). , ,
.
. , , }2.}.
.
VBA InputBox
, , 12.1, VBA I n p u t B o x , ( ). .
Sub GetName()
Dim UserName As String
Dim F i r s t S p a c e As I n t e g e r

Do U n t i l UserName "
UserName = InputBox(" : ", _
"")
Loop
F i r s t S p a c e = InStr(UserName, " ")
If F i r s t S p a c e <> 0 Then
UserName = Left(UserName, F i r s t S p a c e - 1)
End If
MsgBox ", " & UserName
End Sub
: InputBox Do U n t i l .
, .

IV.

319

(Cancel) , UwerName ,
. ( I n S t r ) . , L e f t . , .
, I n p u t B o x . , I n p u t B o x , , ,
VBA Val.
I n p u t B o x , Excel.

InputBox Excel
Excel I n p u t B o x ( VBA I n p u t B o x )
:

, ;

I n p u t B o x :

object.InputBox(,,_,,,,
, )

, ( ).

( ).

_ ,
( ).

,
( ).

, ( ).

, (*
). . 12.1.

12.1. Excel InputBox

()

( )

16

, , # /

64

320

12.

Excel I n p u t B o x .
" . ,
, , 3 (.. 1+2 ""+""). 8,
. , .
E r a s e R a n g e , , I n p u t B o x .
, (. 12.2). , .
I n p u t B o x 8 R a n g e (
S e t ) . ( C l e a r ) .
.
, On E r r o r .
Sub EraseRange()
Dim UserRange As Range
DefaultRange = Selection.Address
On Error GoTo Canceled
Set UserRange = Application.InputBox _
(Prompt:=" :", _
Title:=" ", _
Default:=DefaultRange, _
Type:-8)
UserRange.Clear
UserRange.Select
Canceled:
End Sub
Excel I n p u t B o x
Excel. G e t R a n g e
, , Excel (. 12.3).

. 12.2. . InputBox

. !2.3. Excel InputBox

VBA MsgBox
VBA MsgBox . (
). MsgBox .
MsgBox.
MsgBox { [, ] [, ] [, ,

IV.

])

321

, ( ).

, ,
( ). . 12.2.

( ).
, (
).

. ,
. ( . 12.2 ,
.) ,
, .

12.2. , MsgBox

vbOKonly

OK

vbOKCancel

vbAborCRetrylgnore

vbYesNoCancel

vbYesNo

vbRetryCancel

vbCritical

16

vbQuestion

32

vbExclamation

vbDefaultButtonl

48
64
0

vbDefaultButton2

256

vbDefaultButton3

vbDefaultButton4

512
768

vbSystemModal

4098

, (
>: )

vblnformation

MsgBox { ), .
MsgBox , , . .
Sub MsgBoxDemo{)
MsgBox " "
End Sub
,
MsgBox .
(. 12.3), MsgBox .

322

12.

Sub GetAnswer()
Ans = MsgBox(" ?", vbYesNo)
Select Case Ans
Case vbYes

...[ Ans Yes]...


Case vbNo
1
...[ Ans N o ] . . .
End Select
End Sub

I 12.3. , MsgBox

vboK

vbCancel

vbRetry

2
3
4

vblgnore

vbYea

6
7

vbAbort

vbNo

MsgBox.
. , .
Sub GetAnswer2O
If MsgBox("?", vbYesNo) <> vbYes Then E x i t Sub
1
. . . [ , ] . . .
End Sub
, . (. 12.4). Config.
Private Function ContinueProcedure() As Boolean
Dim Config As Integer
Dim Ans As Integer
Config = vbYesNo + vbQuestion + vbDefaultButton2
Ans = MsgBox(" . ?", Config)
If Ans = vbYes Then ContinueProcedure = True _
Else ContinueProcedure = False
End Function
C o n t i n u e P r o c e d u r e
. , C o n t i n u e P r o c e d u r e ( ).
(.. ), .
:
If Not C o n t i n u e P r o c e d u r e Then E x i t Sub
. 12.4. ,
MsgBox on
,

*
,
.

,
vbCrLf ( vbNewLine)
.
110
. . :
&

IV.

323

Sub

MultiLineO
Dim Msg As String
Msg = " " & vbCrLf
Msg = Msg & " " & vbCrLf
Msg = Msg & " "
MsgBox Msg
End Sub

vbTab. 20x8 (. 12.5). vbTab. vbCrLf.
MsgBox , 1023 .
, .
Sub

ShowRange()
Dim Msg As String
Dim r As Integer, As Integer
Msg = ""
For r = 1 To 2 0
For = 1 To 8
Msg = Msg & Cells{r, c) & vbTab
Next
Msg = Msg & vbCrLf
Next r
MsgBox Msg
End Sub
15 VBA,
MsgBox.


Excel Windows Scripting Host (Wscript)
. Popup s h e l l . :
, ,
.
.
, .
Sub PopupDemo()
Dim WshShell As IWshShell
Dim Msg As S t r i n g
Set WshShell = CreateObject("Wacript.Shell")
Msg = " 5 ."
T i t l e = " ."
WshShell.Popup Msg, 5, T i t l e , 7 + vblnformation
Set WshShell - Nothing
End Sub
set S h e l l WshShell.
Popup .
, .
, . , ( ,
MsgBox).

324

1Z

,
, Windows Scripting Host,
. Windows Scripting Host , .

. 12.5.

Excel GetOpenFilename
,
i n p u t B o x , .
G e t O p e n F i l e n a m e A p p l i c a t i o n ,
, ( ).
(
^), . , , . ,
, ( ):
obj ect.GecOpenFilename(_,
_,
,
_,

_5)

1_ ,
( ).

_ ,
( ).

( ), , .

_ Macintosh.

__ .
, .
.

_ .
, ,
,
. . , :
" {*.*},*.*"

IV.

325

{*.*). ,
. * . * .
F i l t .
_ G e t O p e n F i l e n a m e .
( ).
F i l t , VBA.
.
n

Filt = " (*.txt),*.txt, & _


" !*.prn), *.prn," & _
" (*.csv),*.csv," & _
"ASCII (*.asc),*.asc," & _
" (.*),**"
^ _ . , .
_ ,
, .
.
.
Sub GetImportFileName{)
Dim Filt As String
Dim Filterlndex As Integer
Dim FileName As Variant
Dim Title As String
1


Filt = " (*.txt),*.txt," & _
" {*.prn},*.prn," & _
" (*.csv),*.csv," & _
"ASCII (*.asc),*.asc, & _
" {*.*),*.*"

*.*
Filterlndex = 5

'


Title = " "

FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
Filterlndex:=FiltzerIndex, _
TiCle:=Title)


If FileName = False Then
MsgBox " ."
Exit Sub
End If


MsgBox " " & FileName
End Sub
. 12.6 ,
.

326

12.

. 12.6. GetOpenFilename
. ,
, <Shift> <Ctrl>, . , F i l e N a m e . , . .
Sub GetImportFileName2()
Dim Filt As String
Dim Filterlndex As Integer
Dim FileName As Variant
Dim Title As String
Dim i As Integer
Dim Msg As String
1

Filt = " {*.txt),*.txt," & _
" (*.prn),*.prn," & _
" (*.csv),*.csv," & _
"ASCII (*.asc),*.asc," &_
" (*.*),*.*"
1
*.*
Filterlndex = 5
1


Title = " "


FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
Filterlndex:=FilterIndex, _
Title:=Title, _
MultiSelect:=True)


If Not IsArray(FileName) Then
MsgBox " ."
Exit Sub
End If

IV.

.127

'


For i = LBound(FileName) To UBound(FileName)
Msg = Msg & FileName(i) & vbCrLf
Next i
MsgBox " :" & vbCrLf & Msg
End Sub

, F i l e N a m e
( ). ,
F i I eName , .

Excel GetSaveAsFilename
G e t O p e n F i l e n a m e . ( )
. , .
:
object.GetSaveAsFilename(_,
,
^)

_,

^,

_ ( ).

_ ( ).

_ ,
( ).

( ).

_ Macintosh.


,
G e t O p e n F i l e n a m e , .
, Excel.
.
, Excel 97 . ,
Excel 2002.

Windows API

G e t D i r e c t o r y , , . 12.7. ,
. ,
. Excel 97 .
G e t D i r e c t o r y ,
. ,
.

328

12.

/*. /2.7.
!
Web- ,
.

API,
. BROWSEINFO.
' 32- API
Declare Function SHGetPathFromlDList Lib "shell32.dll"
Alias "SHGetPathFromlDListA" (ByVal pidl As Long, ByVal _
pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowselnf As BROWSEINFO)
As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
API GetDirectory.
Function GetDirectory(Optional Msg) As String
Dim blnfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
1

=
blnfo.pidlRoot = Q&

'


If IsMissing(Msg) Then
blnfo.lpszTitle = " ."
Else
blnfo.lpszTitle = Msg
End If

IV.

'


blnfo.ulFlags = &H1


= SHBrowseForFolder(blnfo)

'


path = Space$(512)
= SHGetPathFromlDList (ByVal :, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
GetDirectory = Left(path, pos - 1)
Else
GetDirectory = ""
End If
End Function
G e t D i r e c t o r y
VBA. .
, MsgBox .
, .
Sub GetAFolderK)
1
Excel 97
Dim Msg As S t r i n g
Dim UserFile As S t r i n g
Msg = " ."
UserFile = GetDirectory(Msg)
If UserFile = "" Then
MsgBox ""
Else
MsgBox UserFile
End If
End Sub
, .

FileDialog
, , Excel 2002, , F i l e D i a l o g .
F i l e D i a l o g Excel 2002. , Excel.
. ( )
MsgBox.
Sub GetAFolder2()
Excel 2002
With Application.FileDialog(msoFileDialogPolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = " "
.Show

330

12.

If .Selectedltems.Count = 0 Then
MsgBox ""
Else
MsgBox .Selectedltems(1)
End If
End With
End Sub
F i l e D i a l o g .
i n i t i a l F i l e N a m e . Excel .

Excel
VBA Excel. VBA
, " "
( ). , VBA ^,
1 : . ( , ).
Application.Goto Reference:=Range("Al:C3")
Excel, .
.

D i a l o g s A p p l i c a t i o n .

Dialogs
D i a l o g s A p p l i c a t i o n 258 , Excel. , ,
. , Excel x l D i a l o g F o r m u l a G o t o .
Show .
, (. 12.8).
Application.Dialogs(xlDialogFormulaGoto).Show
, ,
. ( <F5>).
, , , .
. R e s u l t True,
, F a l s e ,
<Esc>:
. 12.8.
VBA

IV.

131

Result = Application.Dialogs{xlDialogFormulaGoto}.Show
, Range
.
, . .
, ,
Excel VBA , . , , .
, VBA .
. - VBA, :
Application.Dialogs(xlDialogSeltSpecial).Show
. , . VBA
.
(. 12.9):
Application.Dialogs(xlDialogAlignment).Show
, : xlDialogForrnatNumber, x l D i a l o g B o r d e r , x l D i a l o g C e l l P r o t e c t i o n , x l D i a l o g P a t t e r n s x l D i a l o g F o n t P r o p e r t i e s .
.

. 12.9.



Object Browser. D i a l o g s Object Browser.
1. VBA, <F2> Object

Browser.
2. Object Browser E x c e l .

332

12.

3.

xlDialog .

4. .

. , xl Di a logF on t Proper t i e s ,
, .



,
() . , ( x l D i a l o g C e l l P r o t e c t i o n ) : locked hidden.
, .
Application.Dialogs(xlDialogCellProtection).Show True, True

VBE. , Built-in Dialog Box Argument List. , .
(
xlDialogFormulaGoTo) :
r e f e r e n c e corner. r e f e r e n c e
. Corner , ,
. , :
Application.Dialogs(xlDialogFormulaGoto).
_
Show Range("Z100"), True
, D i a l o g s , .


1-
( CommandBar).
, . .
CommandBar 22-23.

:
Application.CommandBars("Worksheet Menu Bar"). _
Controls("").Controls("...").Execute
, . , ( ).

IV.

333

D i a l o g s ,
.
Excel
CommandBar. , Excel. , Excel, F i n d c o n t r o l , id . 22.
D i a l o g s :
.
.
, :
Application.CommandBar^("Worksheet Menu Bar"). _
Controls("").Controls("...").Execute
, E x e c u t e , . . , ( ):
S e l e c t i o n . F o n t . B o l d = Not S e l e c t i o n . F o n t - B o l d

VBA Excel,
.

334

12

Excel .
Excel 97, . ,
.
Excel 97
Excel 5/95. ,
U s e r F o r m .
Excel .


Excel. .

Excel


U s e r F o r m ,
Visual Basic.
, .
1.

U s e r F o r m VBAProject
.

2.

,
U s e r F o r m . VBA ( U s e r F o r m ) .

3. UserForm.
4.

5. " " . UserForm (, ).

UserForm
UserForm, VBE ( <Alt+Fl 1>,
Insert^ UserForm). UserForm
, U s e r F o r m l , UserForm2 ..
, UserForm. Properties Name ( <F4>, Properties ). . 13.1
Properties UserForm.
. UserForm .




, Show UserForm. , VBA, U s e r F o r m l .
Sub ShowForm
UserForml.Show
End Sub
, . UserForm CommandButton., .
UserForm ( Unload),
( Hide
UserForm). . 13.1. Properties
.
UserForm -
, Add
UserFrom UserForms, Show
UserForms. , UserForm MyForm, .
MyForm - "UserForml"
UserForms.Add(MyForm).Show
, U s e r Form , .

13.



UserForm, Toolbox
( VBE , ).
Toolbox , View1^ Tool box. Toolbox . 13.2.
Toolbox,
. ( ).
, , .

. 13.2- Toolbox

, . , CommandButton UserForm, C o m m a n d B u t t o n ! . CommandButton,
CommandButton2 ,
VBA-. (, ProductListBox), ( L i s t B o x l ) . ,
Properties VBE.
.


, Toolbox.
UserForm ActiveX.
" Toolbox" .

CheckBox
CheckBox :
, , .. CheckBox
, T r u e , F a l s e .

ComboBox
ComboBox L i s t B o x . ,
ComboBox , . , , ComboBox.

IV.

337

CommandButton

CommandButton. CommandButton, .

Frame
F r a m e
. ,
. F r a m e
,
O p t i o n B u t t o n .

Image
I m a g e ,
. , I m a g e .
. , ,
.
, . , .

Label
L a b e l .

ListBox
L i s t B o x ,
( ). L i s t B o x . , , . . ,
L i s t B o x VBA.

MultiPage
M u l t i P a g e
, ^ .
M u l t i P a g e . ,
New P a g e .

338

13.

OptionButton
O p t i o n B u t t o n . , . O p t i o n B u t t o n , O p t i o n B u t t o n
.

O p t i o n B u t t o n ,
GroupName. O p t i o n B u t t o n
. O p t i o n B u t t o n Frame, .

RefEdit
R e f E d i t , .

ScrollBar
S c r o l l B a r
S p i n B u t t o n . ,
S c r o l l B a r .
S c r o l l B a r .

SpinButton
S p i n B u t t o n
. ,
. S p i n B u t t o n
T e x t B o x L a b e l , S p i n B u t t o n .

TabStrip
T a b S t r i p M u l t i P a g e ,
. T a b S t r i p , M u l t i P a g e , . , M u l t i P a g e .

TextBox
T e x t B o x .

ToggleButton
T o g g l e B u t t o n : . . T r u e () F a l s e

IV.

339

(). "'" ,
O p t i o n B u t t o n CheckBox
.



, , , .
.
.
<Shift> .

U s e r F o r m , . , . , , VBE Tools ^Options. Options


General Form Grid Settings.
Format VBE , .
, .
, . . 13.3
O p t i o n B u t t o n .


.
( Excel, VBE).
, . ,
,
. , ,
, Linkedcell. , .
, .
, .
, .
-> . ,
Excel 5 Excel 95.
. ActiveX.
, . Excel 97 , Excel 5 Excel 95.
, .
, ActiveX.

340

13.


Excel .
,

. ActiveX,
.

, .
,
{, commandButtonl_click)
.
.
Excel , .
,
( ).
,
, Web- .

. 13.3.
Format^Align

IV.

341

, , .
,
.


,
. .

.
Properties.

, . VBA.

Properties
VBE Properties
( U s e r F o r m ) . ,

Properties (. 13.4).
Properties . Alphabetic
.
Categorized , .
, .
, . , . Properties ,
. ,
. , T e x t A l i g n : 1 - fmTextAlignLeft, 2 - fmTextAlignCenter 3 - fmTextAlignRiglit.
(, F o n t P i c t u r e )
.
.
>
P i c t u r e I m a g e , . . , P i c t u r e
I m a g e <CtrI+V>
.

. 13.4, vtipaeieHiin (OptionButton3j


Properties

342

13.

, Properties , .

UserForm , . ,
,
. , Font UserForm,
, , .


,
, . , Name ,
(Height, Width, L e f t R i g h t ) .
- VBA,
. , O p t i o n B u t t o n ,
, O p t i o n B u t t o n ] . .
:
O p t i o n B u t t o n l . V a l u e = True
O p t i o n B u t t o n (,
obLandscape),
obLandscape.Value = True
, , . ob , OptionButton.
. , O p t i o n B u t t o n .
O p t i o n B u t t o n L e f t Properties. L e f t .



. Properties <Fl>. . 13.5 ,
.


? . <> <Shift+Tab>
. , , :
.

IV.

343

, 13.5.

()
,
<> <Shift+Tab>. ,
,
.
TextBox,
. O p t i o n B u t t o n , , , ,
.
,
View=>Tab Order. , . 13.6. - Tab Order
Tab Order . , Excel
Tab Order,
[) . 13.6. Tab Order ,
. ,
Move Up win Move Down. ( <Shift> <CtrI>).

344

13.

, "
Properties. dex, 0. T a b l n d e x
T a b l n d e x . . ,
T a b l n d e x , , . ,
T a b S t o p F a l s e .
, Frame MultiPage, .
. OptionButton
Frame, , View^Tab Order.


.
<Alt> . A c c e l e r a t o r
Properties .
Accelerator,
caption.
Label.
Label, TextBox.


Form. ,
VBA.
< R u n ^ R u n Sub/UserForm.
- <F5>.
- Run Sub/UserForm Standard.
.
,
.

U s e r



VBA, .


VBA,
, Show U s e r F o r m . U s e r F r o m

IV.

345

, VBA. U s e r Form U s e r F o r m l , .
Sub ShowDialog{)
UserForml.Show
End Sub
VBA, U s e r F o r m .
, VBA Load.
. , Show, .
U s e r F o r m l ,
Load U s e r F o r m l
, , Show. , Load .


U s e r F o r m l , U n l o a d . :
Unload UserForml

U n l o a d Me
VBA U n l o a d , UserForm
, , U s e r F o r m CommandButton, .
. U s e r F o r m .
, , Unload,
.
U s e r F o r m , ,
, . ,
, , , .
, , (
U s e r F o r m ) , P u b l i c ,
VBA. , .
,
( ). Queryclose UserForm,
Terminate UserForm.
U s e r F o r m H i d e .
, ,
. , :
UserForml.Hide

346

13.


Me.Hide
- ,
, H i d e
, DoEvents. ,
UserForm ,
CommandButton]..
.
P r i v a t e Sub CommandButtonl_Click{)
Me.Hide
DoEvents
For r = 1 To 10000
Cells(r, 1} = r
Next r

Unload Me
End Sub

15 , ,
:
, .


, L i s t B o x ,
CommandButton .. , , . , CommandButton
C l i c k CommandButton. , .
.
UserForm. ,
VBA.
VBA , {.. ). , L i s t B o x , L a b e l . .



,
, .
VBA
.

IV.

347

, : .
TextBox, O p t i a n B u t t o n
(, ). , ,
.


. 13.7 . , .
.

. 13.7.

1. <Alt+Fl 1> VBE.
2. Project , insert^UserForm,
.
3. C a p t i o n UserFonrrt U s e r Forml. Properties ,
C a p t i o n ( Properties
, <F4>).
4. L a b e l , .

Accelerator

Caption

Tablndex

5. T e x t E o x .

Name
Tablndex

TextName
1

6. Frame .

Caption

Tablndex

7. O p t i o n B u t t o n F r a m e
, .

348

13.

Accelerator

Caption

Name

OptionMale

Tablndex

8. Frame O p t i o n B u t t o n
, .

Accelerator

Caption

Name
Tablndex

Option Female
1

9. Frame O p t i o n B u t t o n
.

Accelerator U
Caption

Name
Tablndex

OptionUnkrtOWfi
2

Value

true

10. CommandButton
Frame .

Caption

OK

Default

True

Name

OKButton

Tablndex

11. CommandButton
Frame , .

Caption

Default

True

Hame

CancelButton

Tablndex

IV.

349

,
, .
, <Ctrl>
, .
ES .


CommandB u t t o n . ,
U s e r F o r m . .
1. OKHQ.lytcel. ( <Alt+Fll>).
2.
. Excel
. Toolbox VBE.
3. ,
. ,
.
.
CornrnandButton^Edit . , .
4. .
VBE. C o m m a n d B u t t o n (), .
5. C o m m a n d B u t t o n l _ c l i c k (. 13.8).
Show U s e r F o n t i ' l
U s e r F o r m .

Puc. 13,8. CommandButtonl_


Click ,

, .
. ,
, . ,
Excel , ,
. ,
.

350

13.

Excel , (. 13.9).
,
, : , U s e r F o r m .

. 73.9. Click
CoimandButton
.


, , . .
1. <Alt+Fl 1>. VBE.
2. , ,
, . ,
UserForm, C a n c e l B u t t o n _ c l i c k . , , .
3. , ( c l i c k C a n c e l B u t t o n ) .
Private Sub CancelButton_Click()
Unload UserForml
End Sub
.
UserForm .
4. <Shift+F7>, UserForml ( View Object Project Explorer).
5. ( Click, OKButton).
Private Sub OKButton_Click()
'

Sheets("1").Activate

IV.

351

NextRow = _
Application.WorksbeetFunction.CountA(Range!"A:A"}) + 1

Cells(NextRow, 1) = TextMame.Text

If OptionMale Then Cells(NextRow, 2} = ""
I OptionFema1e Then Cells(NextRow, 2) = ""
If OptionUnknown Then Cells{NextRow, 2) = ""

'


TextKame.Text = ""
OptionUnknown - True
TextName.SetFocus
End Sub
6. Excel ,
. .
.
. 13.10 .

. 13.10.

QKButton_Click : . ,
1 . Excel (COUNT)
. T e x t Box
. If
O p t i o n B u t t o n ,
{, , ). , . ,
. ( ) .


. ,
, ( , ).
O K B u t t o n _ c l i c k . , ( ) T e x t B o x . T e x t B o x ,

352

13.

, . , . E x i t Sub .

If TextName.Text = "" Then
MsgBox " ."
TextName.SetFocus
Exit Sub
End If

!
( ).
, .
.
.

UserForm
U s e r F o r m ( U s e r F o r m ) , . Excel. , CommandButton C l i c k CommandButton.
, .
. , "" S p i n B u t t o n
S p i n U p Change. , Show, Excel I n i t i a l i z e
A c t i v a t e U s e r F o r m .
, Excel Sheet, chart ThisWorkbock.
18.


,
, .
1. .
2. ,
UserForm. VBE ,
3. , (. 13.11).
4. , VBE .

IV.

353

. J3.1I.
CheckBox
, . ,
.
,
. , ,

. !
,
,
.

UserForm
UserForm.
I n i t i a l i z e UserForm. He
, UserForm .
A c t i v a t e UserForm,
D e a c t i v a t e UserForm. UserForm.
QueryClose UserForm.
Terminate UserForm.

.
Show i n i t i a l i z e A c t i v a t e (
). Load i n i t i a l i z e . Unload, QueryClose Terminate ( ). Hide
.
Web- , , . UserForm
, , ,
.

: SpinButton
, , SpinButton.

354

13.

Web- ,
, SpinButton UserForm (
).
SpinButton
UserForm. ,
.
. 13.1 , SpinButton.

13.1. SpinButton

Af t e r u p d a t e

,
Bef oreDragOver

Bef o r e D r o p o r P a s t e ,

Bef o r e U p d a t e
change
Enter
Error

Exit
KeyDown
Keypress

spinDown
spinup


v a l u e
, S p i n B u t t o n U s e r F o r m
;

, ,
,
,
,
, { } S p i n B u t t o n
, ( ) S p i n B u t t o n

SpinButton { ) .


SpinButton, .
1. E n t e r ( , ).
2. Change.
3. SpinUp.


<> ,
S p i n B u t t o n .

IV.

355

. ,
.
1. Enter.
2. KeyDown.
3. Change.
4. SpinUp.

, ?
SpinButton VBA,
. ,
Value SpinButton ,
" Change.
, Value .
SpinButtonl.Value = 0
, , EnableEvents A p p l i c a t i o n False. ,
, ""
Excel: Workbook, Worksheet Chart.


SpinButton TextBox
SpinButton Value, . ,
SpinButton ,
.
SpinButton
TextBox,
SpinButton ,
TextBox. , SpinButton
, TextBox.
. 13.12 . Min SpinButton
1, 100. , SpinButton 1 100,
. 13.12.
SpinButton, TextBox
. Web- .

, "'" S p i n B u t t o n TextBox, . ,

356

13.

V a l u e S p i n B u t t o n T e x t T e x t B o x .

C h a n g e S p i n B u t t o n . , ,
S p i n B u t t o n , .
Private Sub SpinButtonl_Change()
TextBoxl.Text = SpinButtonl-Value
End Sub
V a l u e S p i n B u t t o n T e x t T e x t B o x . , ( S p i n B u t t o n l T e x t B o x l ) . T e x t B o x , c h a n g e , :
P r i v a t e Sub TextBox l _ C t i a n g e
NewVal = Val(TextBoxl.Text)
If NewVal >= SpinButtonl.Min And _
MewVal <= SpinButtonl.Max Then _
SpinButtonl.Value = NewVal
End Sub
VBA Val, T e x t B o x ( T e x t B o x , Val 0). , . , V a l u e
S p i n B u t t o n , T e x t B o x .
, (
O K B u t t o n ) S p i n B u t t o n . C l i c k CommandButton :
P r i v a t e sub OKButton_Click()
1

If CStr(SpinButtonl.Value} = TextBoxl.Text Then
ActiveCell = SpinButtonl.Value
Unload Me
Else
MsgBox " ", v b C r i t i c a l
TextBoxl.SetFocus
TextBoxl.SelStart = 0
TextBoxl.SelLength = Len{TextBoxl.Text)
End If
End Sub
: ,
TextBox, S p i n B u t t o n . . ,
T e x t B o x , S p i n B u t t o n , , . , V a l u e S p i n B u t t o n
C S t r . , , .

/V.

357

S p i n B u t t o n TextBox,
. T e x t B o x , ( S e l S t a r t S e l L e n g t h ) . , .
Tag
UserForm Tag.
. Tag
, .
, TextBox .
. . Tag ,
. Tag , , Required.
Tag.
, , ,
TextBox UserForml , "" .
Function EmptyCount{)
Dim c t l As C o n t r o l
Emp tyCount = 0
For Each c t l In UserForml.Controls
If TypeName(ctl) = "TextBox" Then
If c t l . T a g = "Required" Then
If c t l . T e x t a "" Then
EmptyCount = EmptyCount + 1
End If
End If
End If
Next c t l
End Function


Tag.



, U s e r F o r m , VBA U s e r F o n n . , VBA .
, UserForm. , VBA.
, U s e r F o r m l .
Sub GetDataO
UserForml.Show
End Sub

, U s e r F o r m l ( T e x t B o x l ) .
. .

358

13.

Sub

GetDataO
UserForml.TextBoxl.Value
UserForml.Show
End Sub

""


I n i t i a l i z e UserForm. UserForm_Initialize 1
.
Private Sub UserForm_Initialize()
TextBoxl.Value = ""
End Sub
, U s e r F o r m .
:
Auto List Member,
. UserForrr,,
Me. , U s e r F o r m , ( ) .

. , UserForml:
MsgBox UserForml.Controls.Count
. , CoinmandButton. TypeName.
For Each Control. commandButton, UserForml.
Sub CountButtons()
Dim cbCount As Integer
Dim c t l as Control
cbCount = 0

For Each ctl In UserForml.Controls


If TypeName(ctl) = "CommandButton" Then _
cbCount = cbCount + 1
Next ctl
MsgBox cbCount
End Sub

Toolbox
U s e r F o r m VBE, Toolbox
, .
Toolbox.



,
IV.

35

Customize XXX ( XXX ). ,


.


Toolbox . New Page, Toolbox. , , . R e n a m e
.



. , , CommandButton,
. : W i d t h (), H e i g h t (), C a p t i o n (), D e f a u l t
( ) Name (). CommandButton Toolbox.
. ,
.
Toolbox, . , CommandButton,
. , .
Toolbox. Toolbox .
,
. , F r a m e O p t i o n B u t t o n ( ). F r a m e Toolbox, Frame.
Toolbox. , Toolbox Excel.
Toolbox, Export Page.
Web- . PAG .
Toolbox. Import Page. . .
Toolbox . 13.13.

. 13.13- Toolbox,

360

13.

ActiveX
ActiveX,
Microsoft . ActiveX , Toolbox Additional Controls. , . 13.14.

. 33.14. Additional
Controls ActiveX
Additional Controls ActiveX, . , .
.
ActiveX, , Excel. . , .

, .
Excel
Windows .
Excel Excel , .
Excel. , ,
. Excel,
. Excel

.
, Excel,
VBA. ,
Excel 2002 .
.

IV.

361

""
, . ,
CommandButton,
. ,
. "" ,
. "" :
.
,
, . , . File^Export File (
<Ctrl+E>). . File 1 ^Import File,
.



, , , .
, .

? . M u l t i P a g e .

'?

VBA ,
?

? , , VGA ( ).

( )?

S c r o l l B a r S p i n B u t t o n ?

362

L i s t B o x ?

13.



U s e r F o r m . ,
. , , U s e r F o r m VBA.
VB .

IV.

363


,
UserForm.
. ,
, Web- .


UserForm

.
: CommandButton
ListBox,


CommandButton
. 14.1 UserForm, CommandButton .
, , , .
CommandButton
. ,
CommandButtonl:
P r i v a t e Sub ComrnandButtonl_Click()
C a l l Macrol
Unload Me
End Sub

M a c r o l U s e r F o r n
.

. 14.1. CoimandButton,

ListBox
. 14.2 , L i s t B o x . I n i t i a l i z e . , , A d d l t e m L i s t B o x .
Private Sub UserForm_Initialize()
With ListBoxl
.Addltem "Mac:
.Addltem "Mac:
.Addltem "Mac
.Addltem "Mac
.Addltem "Mac
.Addltem "Mac
End With
End Sub

. 14.2.
ListBox
, .
P r i v a t e Sub ExecuteButton_Click()
Select Case L i s t B o x l . L i s t l n d e x
Case -1
MsgBox " ."
Exit Sub
Case 0: Call Macrol
Case 1: Call Macro2
Case 2: C a l l
Case 3: Call Macro4
Case 4: Call Macro5
Case 5: Call
End Select
Unload Me
End Sub
L i s t l n d e x ListBox,
, ( L i s t l n d e x -1,
). .

IV.

365

Excel "" .
22-23.


Excel . , .
,
.
.
R e f E d i t . ,
Excel,
.
, , ( Excel).
. 14.3
. 14.3. R e f E d i t . RefEdit

. O p t i o n B u t t o n .
,
R e f E d i t .
,
. Range.
:
Set UserRange = Range(RefEditl.Text)
RefEdit
. U s e r F o r m _ m i t i a l i z e

RefEditl.Text = ActiveWindow.RangeSelection.Address
R e f E d i t . . ',
. , , .
. , , ,
R e f E d i t ,
.
On Error Resume Next
Set UserRange = Range(RefEditl.Text)
If Err <> 0 Then
MsgBox " "
RefEditl.SetFocus

366

14.

Exit Sub
End If
On Error GoTo 0
,
Ref E d i t . ,
.
, ,
. :
2!5$1:$:4
,
. 3 :
Set OneCell = Range (RefEdit!.. Text) .Range( "Al"
12, VBA inputBox ,
.


. . , Excel, .
Excel
. , .
.
1. .
2. VBE U s e r F o r m .
, U s e r F o r m U s e r F o r m l .
3. U s e r F o r t n l . ,
Image, . . 14.4
.

. 14.4.

IV.

367

4. ThisWorkbook ():
P r i v a t e Sub Workbook_Open()
UserForml.Show
End Sub
5. UserForml (
):
P r i v a t e Sub UserForm_Activate> {)
Application.OnTime Mow + _
TimeValue<"00:00:05"), "KillTheForm"
End Sub
6. VBA:
Private Sub KillTheForm()
Unload UserForml
End Sub
WorkbookIsOpen. UserForm. Activate,
UserForm_Activate. OnTime A p p l i c a t i o n
KillTheForm . A c t i v a t e .
KillTheForm UserForm .
7. CommandButton CancelButton, Cancel True

UserForm.
Private Sub CancelButton_Click()
KillTheForm
End Sub
, ,
. <Esc>. , .
, ,
. ,
, ,
, .



,
( ) UserForm . , . , ,
UserForm CommandButton.
, , , . QueryClose.

368

14.

, U s e r F o r m ,
(.. Q u e r y C l o s e } .
P r i v a t e Sub UserForm_QueryClose _
(Cancel As I n t e g e r , CloseMode As I n t e g e r )
If CloseMode = vbFormControlMenu Then
MsgBox " ."
Cancel = True

End If
End Sub

U s e r F o r m _ Q u e r y C l o s e . C l o s e M o d e
, Q u e r y C l o s e . :
Q u e r y C l o s e v b F o r m C o n t r o l M e n u ( ),
, . ; C a n c e l T r u e , .
, <Ctrl+Break>
, . <Ctrl+Break> UserForm
, . , UserForm:
Application. EnableCancelKey = xlDisabled.
, , , , ,
.


, . , Excel (
^) ,
.
.
W i d t h H e i g h t U s e r F o r m .
. 14.5 , . . 14.6 , , .

. M.S.

. 14.6.

IV.

369

,
. U s e r F o r m _ I n i t i a l i z e ( ).
. , .
, .
Private Sub OptionsButton_Click()
If OptionsButton.Caption = " " Then
Me.Height = 164
OptionsButton.Caption = " "
Else
Me.Height = 12a
OptionsButton.Caption - " "
End If
End Sub
C a p t i o n C o m m a n d B u t t o n H e i g h t U s e r F o r m C a p t i o n ,
,
, ,
, .
<Alt+L> ( ),
. , , ,
.



.
, .
Excel 2000, OKHD UserForm "". ,
, Excel. show UserForm
. , :
UserForml.Show vbModeless
, , , S c r o l l B a r .
. 14.7 ( 100% 400%)
S c r o l l B a r , . S c r o l l B a r .

370

14.

. 14,7.
ScrollBar

:
P r i v a t e Sub UserForm_Initialize{)
LabelZoom.Caption = ActiveWindow.Zoom
1

With ScrollBarZoom
.Min = 10
.Max = 400
.SmallChange = 1
.LargeChange =10
.Value - ActiveWindow.Zoom
End With
'


With ScrollBarColumns
.Min = 1
.Max = 256
.Value = ActiveWindow.ScrollColumn
.LargeChange = 2 5
.SmallChange = 1
End With

'


With ScrollBarRows
.Min = 1
.Max = ActiveSheet.Rows.Count
.Value = ActiveWindow.ScrollRow
.LargeChange = 25
.SmallChange = 1
End With
End Sub
S c r o l l Bar. , .
ScrollBarZoom
ScrollBarZoom__Change ( ). Zoom ActiveWindow Value
ScrollBar. ,
.
P r i v a t e Sub ScrollBarZoom_Change()
With ActiveWindow
.Zoom = ScrollBarZoom.Value
LabelZoom

.zoom

&

"%"

End With
End Sub
. ScrollRow Scrol lColuums ActiveWindow
Value ScrollBar.

IV.

371

Private Sub ScrollBarColumns_Change()


ActiveWindow.ScrollColumn = ScrollBarColuims.Value
End Sub
Private Sub ScrollBarRows_Change()
ActiveWindow.ScrollRow = ScrollBarRows-Value
End Sub

ListBox
L i s t B o x ,
. ,
L i s t B o x .
, , ComboBox.

ListBox
,
L i s t B o x . .
L i s t B o x
( RowSource) VBA (
Add It em).
L i s t B o x
. M u l t i S e l e c t .
L i s t B o x ,
L i s t B o x C o n t r o l S o u r c e .

L i s t B o x ( L i s t l n d e x -1).
, .

L i s t B o x (
ColumnCount) (
ColumnHeads).

ListBox,
, UserForm
.

L i s t Box , , , .
L i s t S t y l e .

L i s t B o x .

372

14.

ListBox
,
ListBox, ListBox . ListBox , .
, VBA
.
.
UserForrn UserForml.
UserForml ListBox, ListBoxl.
, 1, 1:12
, ListBox.

ListBox
ListBox
, . RowSource , ListBox. . 14.8
Properties ListBox. RowSource
1 !1: 12.
UserForm , ListBox
.
ListBox , , RowSource.

. 14.8. RowSoarce
, RowSource .
ListBox
. , . : I !, x l s ] ! 11.-12,

ListBox
L i s t B o x ,
.
RowSource ,
, .
, A d u l t e m L i s t B o x .

IV.

373

RowSource , Properties.
, RowSource L i s t B o x , UserForm.
C a t e g o r i e s Budget.
UserForml.ListBoxl.RowSource = "Budget!Categories"
UserForml.Show
L i s t l i o x .,
VBA L i s t B o x
.
L i s t B o x Addltem.
Sub ShowUserForm2()
'
ListBox
With UserForm2.ListBoxl
.RowSource=""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
.Addltem ""
End With
UserForm2.Show
End Sub
Rowsource
, Properties RowSource
L i s t B o x .
ListBox
RowSource, ("Permission denied").
, Addltem L i s t B o x , . L i s t B o x Al :12 ! .

For Row = 1 12

UserForml.ListBoxl.Addltem Sheets("1"}.Cells(Row,
Next Row

1)

,
. , dData,
50 . 50
L i s t B o x , L i s t B o x l :
L i s t B o x l . L i s t dData

ListBox

L i s t B o x (; . ,

374

14.

, . (. 14.9). L i s t B o x , .

. 14.9. Collection
ListBox

C o l l e c t i o n .
C o l l e c t i o n :
o b j e c t . A d d i t e m , key, b e f o r e , a f t e r
key, , ,
- , , . , .
.
C o l l e c t i o n NoDupes. ,
, D a t a , , .
, NoDupes
. , ( )
key. On E r r o r Resume
Next , VBA , . , , .
NoDupes L i s t B o x . UserForm
, .
Sub P.emoveDuplicatesl ()
Dim AllCells As Range, Cell As Range
Dim NoDupes As New Collection
On Error Resume Next
For Each Cell In Range{"Data")
NoDupes.Add Cell.Value, CStr(Cell,Value)
Next Cell
On Error GoTo 0
1

ListBox
Each Item In NoDupes
UserForml.ListBoxl.Addltem Item
Next Item

IV.

375

UserForml.Labell-Caption = _
" : " KoDupes.Count
'
UserForm
UserForml.Show
End Sub
Web- .


UserForm L i s t B o x , .
: , .
ListBox M u l t i S e l e c t 0.
, ,
Value ListBox. , , ListBoxl:
MsgBox ListBoxl.Value
,
.
( ),
L i s t l n d e x ListBox.
,
ListBox:
MsgBox " #" & ListBoxl.Listlndex
, L i s t l n d e x -1.
ListBox 0, I. , Listlndex 0.
L i s t c o u n t - l .


, MultiSelect ListBox 0 , ListBox.
ListBox (.- M u l t i S e l e c t I 2),
L i s t l n d e x Value ListBox .
Selected, , 0. ,
1
True, ListBox .
MsgBox ListBoxl.Selected(O)

376

14.

Web- ,

ListBox. , .

, , Web- . L i s t B o x .
, Msg. .
Private Sub OKButton_Click()
Msg = ""
For i = 0 To ListBoxl.ListCount - 1
If ListBoxl.Selected(i) Then
Msg = Msg & ListBoxl.List(i) & vbCrLf
Next i
MsgBox " : " & vbCrLf & Msg
Unload Me
End Sub
. 14.10
L i s t B o x .

. 14.10. ,
ListBox


ListBox
L i s t B o x , , O p t i o n B u t t o n
.
. 14.11 U s e r F o r m . L i s t Box . ,
C l i c k O p t i o n B u t t o n , RowS o u r c e L i s t B o x .
:

IV.

377

Private Sub obMonths_Click()


ListBoxl.RowSource = "1!"
End Sub

'

O p t i o n B u t t o n , o b M o n t h s ,
R o w S o u r c e L i s t B o x ,
1 .

. 14.11. Codep.vcu.\iae L i s t B o x , OptionButton

ListBox
. . ,
( ,
^ ^).
. 14.12 L i s t B o x .
, L i s t B o x ,
L i s t B o x . . .
, ,
, , .

. 14-12.

. , ,
.
private Sub AddButton_Click()
If ListBoxl.Listlndex = -1 Then Exit Sub
If Not cbDuplicates Then
1

For i = 0 To ListBox2.ListCount - 1

378

14.

If ListBoxl.Value = ListBox2.List(i) Then


Beep
Exit Sub
End If
Next i
End If
ListBox2.Addltem ListBoxl.Value
End Sub
Delete ;
Private Sub DeleteButton_Click(}
If LiatBox2.Listlndex = -1 Then Exit Sub
ListBox2.Removeltem ListBox2.Listlndex
End Sub
, .
L i s t l n d e x L i s t B o x -1, , . .


ListBox
,
L i s t B o x . VBA
U s e r F o r m .
. 14.13 , L i s t B o x
CommandButton. L i s t B o x . .

. 14.13. Li s tBox
CoinmandButton
.
Private Sub MoveUpButton_Click()
If ListBoxl.Listlndex <= 0 Then Exit Sub
Numltems = ListBoxl.ListCount
Dim TempList()
ReDim TempList(0 To Numltems - 1)
1

For i = 0 To Numltems - 1
TempList(i) = ListBoxl.List(i)
Next i
1

itemNuiti = ListBoxl .Listlndex

IV.

379

Templtem = TempList(ItemNum)
TempList(ItemNum) = TempList(ItemNum - 1)
TempList(ItemNum - 1) = Templtem
ListBoxl-List = TempList
1

ListBoxl.Listlndex = ItemNum - 1
End Sub
Private MoveDownButton__Click()
If ListBoxl.Listlndex = ListBoxl.ListCount - 1 Then Exit Sub
Wumltems = ListBoxl.ListCount
Dim TempList()
ReDim TempList(0 To Numltems - 1)
1

For i = 0 To Nvmltems - 1
TempList(i) = ListBoxl.List(i)
Next i
1

ItemNum = ListBoxl.Listlndex
1

Templtem = TempList(ItemNum)
TempList (ItemNum) = TempList (ItiemNum + 1)
TempList(ItemNum + 1) = Templtem
ListBoxl.List = TempList
1

ListBoxl.Listlndex = ItemNum + 1
End Sub
, , , .
, ,
, D b l C l i c k CommandButton. ,
C l i c k , :
P r i v a t e Sub MoveUpButton_DblClick _
{ByVal Cancel As MSForms.ReturnBoolean)
Call MoveUpButton_Click
End Sub
P r i v a t e Sub MoveDovmB\itton_DblClick _
(ByVal Cancel As MSForms.ReturnBoolean)
Call MoveDownButton_Click
End Sub

ListBox,

, L i s t B o x , . ListBox,
. . 14.14 L i s t B o x ,
.

380

14.

. /4.74. ListBox
,
L i s t B o x , , , .
1.

, C o l u m n C o u n t L i s t B o x ,
.

2. ,
R o w S o u r c e L i s t B o x .
3. , . 14.14, ColuronHeads T r u e . He
, RowSource. VBA , ,
R o w S o u r c e .
4. , C o l u m n W i d t h s ,
(1/72 ) . ,
C o l u m n W i d t h s ypaeListBox:
10040;30
5. BoundColumn. , V a l u e yrrpaBTteHHffListBox.
L i s t B o x , , L i s t L i s t B o x . 1, 2 (. 14.15). ,
C o l u m n C o u n t 2.
Private Sub UserForm_Initialize()
1
ListBox
Dim Data(l To 12, 1 2)
For i = 1 12
D a t a ( i , 1) = F o r m a t ( D a t e S e r i a l ( 2 0 0 1 , i, 1 ) ,

IV.

"mmram")

381

Next i
For i = 1 To 12
Data(i, 2} = Day(DateSerial(2001,
Next i
ListBoxl .ColuimCount = 1
ListBoxl.List = Data
End Sub

. 14,15.
ListBoxc
colmnnHeads,
VBA.

ListBox

, ,
. L i s t B o x , (. 14.16). L i s t B o x . . , .
, L i s t B o x . ,
. <Ctrl>. , , , .

. 14.16.
ListBox

, M u l t i S e l e c t
L i s t B o x 1 - fmMultiSelectMulti. "", L i s t s t y l e
L i s t B o x 1 - fniListStyleOplion.
U s e r F o r m I n i t i a l i z e ; . r n g ,

382

14.

.
RowSource ColumnCount ListBox,
ColumnWidths, ListBox .
P r i v a t e Sub u s e r F o r m ^ l n i t i a l i z e O
ColCnt = ActiveSheet.UsedRange.Columns.Count
Set rng = ActiveSheet.UsedRange
With ListBoxi
.ColumnCount = ColCnt
.RowSource = rng.Address
cw = ""
For - 1 To .ColumnCount
cw = cw & rng.Columns(c).Width & " ; "
Next
.ColumnWidths = cw
.Listlndex = 0
End With
End Sub
( S e l e c t A l l B u t t o n SelectNoneButton) , :
Private Sub SelectAllButton_Click()
For r = 0 To ListBoxl.ListCount - 1
ListBoxl.Selected(r) = True
Next r
End Sub
Private Sub SelectNoneButton_Click()
For r = 0 To ListBoxl.ListCount - 1
ListBoxl.Selected<r) - False
Next r
End Sub
OKButton_Click. Range, RowRange. , ListBox. , S e l e c t e d ListBox.
, Union RowRange.
Private Sub OKButton_Click(}
Dim RowRange As Range
RQwCnt = 0
For r = 0 To ListBoxl.ListCount - 1
If ListBoxl.Selected(r) Then
RowCnt = RowCnt + 1
If RowCnt = 1 Then
Set RowRange = ActiveSheet.Rows{r + 1)
Else
Set RowRange = _
Union(RowRange, ActiveSheet.Rows(r + 1})
End If
End If
Next r
If Not RowRange Is Nothing Then RowRange.Select
Unload Me
End Sub

IV.

383

ListBox

, , , .
L i s t B o x . .

( ,
Excel 5/95).

. 14.17 .

. 14.17.

U s e r F o r m _ I n i t i a l i z e ( )
, .
ListBox.
P r i v a t e Sub UserForm_Initialize()
Dim SheetData() As String
Set OriginalSheet = ActiveSheet
ShtCnt = ActiveVJorkboo*. Sheets. Count
ReDim SheetData(l To ShtCnt, 1 To 4)
ShtNum = 1
For Each Sht In ActiveWorkbook.Sheets
If Sht.Name = ActiveSheet.Name Then
Lis t: = ShtNum - 1
SheetData(ShtNum, 1) = Sht.Name
Select Case TypeName(Sht)
Case "Worksheet"
SheetData(ShtNum,21=""
SheetData{ShtNum, 3] = _
Application.CauntA(Sht.Cells)
Case "Chart"
SheetData(ShtNum, 21 = ""
SheetData(ShtNum, 3) = "N/A"
Case "DialogSheet"
SheetData (ShtMum, 2) = ""
SheetData(ShtNum, 3; = "N/A"
End Select
If Sht.Visible Then
SheetData(ShtNum, 4} = ""
Else
SheetData(ShtNum, 4) = ""
End If
ShtNum = ShtNum + 1
Next Sht
With ListBoxl

384

14.

-ColumnWidths = "100 pt;50 pt;40 pt;50 pt"


.List = SheetData
.Listlndex = ListPos
End With
End Sub
L i s t B o x l _ c l i c k :
P r i v a t e Sub ListBoxl_Click()
If cbPreview Then _
Sheets(ListBoxl.Value)-Activate
End Sub
CheckBox ( cbPreview) , ListBox.
( OKButton) OKButton_
Click, :
P r i v a t e Sub OKButton_Click(}
Dim UserSheet As Object
Set UserSheet = Sheets(ListBoxl.Value)
If UserSheet.Visible Then
UserSheet-Activate
Else
If MsgBox(" ? _
vbQuestion + vbYesNoCancel) = vbYes Then
UserSheet.Visible = True
UserSheet.Activate
Else
OriginalSheet.Activate
End If
End If
Unload Me
End Sub
OKButton_Click , . , . , , . . , . ( OriginalSheet).
ListBox
, . ListBoxl_DblClick, , OKButton_Click:
P r i v a t e Sub ListBoxl_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call OKButton_click
End Sub

MultiPage
M u l t i P a g e UserForm, . .
M u l t i P a g e ,
. T a b O r i e n t a t i o n ,
.

IV.

385

Toolbox , TabStrip,
MultiPage ,
TabStrip.
MultiPage .
, MultiPage.
( ), ,
Value MultiPage. 0 . 1 ..
MultiPage .
,
New Page .
MultiPage ,
. Properties ,
.
MultiPage,
. , . , <>
.
MultiPage Properties.
MultiPage ,
MultiRow True, .
, .
S t y l e 1. S t y l e 2,
MultiPage , .
T a b O r i e n t a t i o n MultiPage.
.
T r a n s i t i o n E f f ect. , ,
"" "" . T r a n s i t i o n P e r i o d
.
, MultiPage.

UserForm.

VBA. , Li stBox.

.

14.



UserForm



Excel . "",
, , .
Excel 97 . . .

,
' U s e r F o r m ( ).

, U s e r Form. U s e r F o r m M u l t i P a g e , .

, U s e r Form. U s e r F o r m , .

,
, .
. ,
), ,
, .


, ,
Excel. ,
,
. .
Excel, :
A p p l i c a t i o n . S t a t u s B a r = ", . . . "
. , Pet, , , ;
Application.StatusBar = ".. . " & Pet & "% "
, , .
:
Application.StatusBar = False
, ,
. , .


U s e r F o r m
, .
Web- .

UserForm
UserForm,
.
1. Ust=rForm Caption
.
2. Frame FrameProgress.
3. Frame Label
LabelProgress.
( BackColor).
.
4. Label , .
5. UserForm , . 15.1,
388

15. UserForm


: , S p e c i a l E f f e e t Frame, . 15.1.
. 15.1.
UserForm


,
UserForm.
I n i t i a l i z e . ,
, . , A c t i v a t e , UserForm , .
UserForm. Main, UserForm
. Main, VBA, ,
, .
P r i v a t e Sub U s e r F o r m _ A c t i v a t e ( )
Call Main
End Sub
Main.
. L a b e l
Frame. . , .
Sub Main()
'

Cells.Clear
Counter = 1
RowMax = 2 0 0
ColMax = 2 5
For r = 1 To RowMax
For = 1 To ColMax
C e l l s f r , c) = IntfRnd * 1000)
Counter = Counter + 1
Next
PctDone = Counter / (RowMax * ColMax)
Call UpdateProgress{PctDone}
Next r
Unload UserForml
End Sub
Main (, ). u p d a t e P r o g r e s s . : 0 J00,
:
Sub UpdateProgress(Pet)
With UserForml
.FrameProgress.Caption = Format(Pct, "0%")

IV.

389

.LabelProgress.Width = Pet * (.FrameProgress.Width - 10)


.Repaint
End With
End Sub


, U s e r Form. VBA.
Sub ShowDialog()
UserForml.LabelProgress.width 0
UserForml.Show
End Sub


S h o w D i a l o g L a b e l 0. Show U s e r F o r m l . U s e r F o r m ( ).
U s e r F o r m , A c t i v a t e ,
Main. M a i n
L a b e l . ,
R e p a i n t U s e r F o r m . L a b e l
.
U s e r F o r m .
, , .
P c t D o n e .


MultiPage
U s e r F o r m .
U s e r F o r m ,
, , . ,
.

U s e r F o r m ( & M u l t i Page) .


Web- ,
.

UserForm
, U s e r F o r m .
M u l t i P a g e . M u l t i P a g e . -

390

15. UserForm

, .
, VBA V a l u e M u l t i P a g e . , .
M u l t i P a g e
U s e r F o r m . U s e r Form Mu 11 i P a g e .
M u l t i P a g e , , . 15.2.
, .
1. F r a m e F r a m e P r o g r e s s .
2. L a b e l F r a m e
L a b e l P r o g r e s s . L a b e l
.
3. L a b e l ,
( ).

. }5,2.
Mul tiPage,
4. M u l t i P a g e ( ,
) S t y l e 2 - fmTabStyltN me
( ). M u l t i P a g e
Properties. ,
M u l t i P a g e , .

UpdateProgress
U s e r F a r m .

Sub UpdateProgress(Pet)
With UserForml
.FrameProgress.Caption = Format(Pet, "0%")
.LabelProgress.Width = Pet * (.FrameProgress.Width - 10)
.Repaint
End With
End Sub
.
.


, . C l i c k O K B u t t o n _ C l i c k . :
MultiPagel.Value = 1

IV.

391

M u l t i P a g e
( , ).
, .
P c t D o n e . , . , :
Call UpdateProgress(PctDone)


, ,
UserForm.
M u l t i P a g e
.


MultiPage
, ,
. ,
M u l t i P a g e . :>
UserForm. , , ,
UserForm .
, UserForm , .
Web- , .

. 15.3 UserForm VBE. H e i g h t


UserForm 170. ,
UserForm, H e i g h t 120 ( ,
, , ). , VBA H e i g h t 170. :
Me.Height = 1 7 0

Pvc. 15.3.
UserForm ,

392

15. UserForm



.
Excel . , .
, , . , , , .
VBA- U s e r F o r m . U s e r F o r m M u l t i P a g e .
. 15.4 .
U s e r F o r m , M u l t i P a g e .
M u l t i P a g e .

. 15.4. , ,
Mul t i Page
, Web-
.
,
,
.

MultiPage
U s e r F o r m .
M u l t i P a g e . . M u l t i P a g e ,

IV.

( ). Web- ,
M u l t i P a g e .
M u l t i P a g e . S t y l e M u l t i P a g e 2 - fmTabStyleNone.
U s e r F o r m ,
M u l t i P a g e .
M u l t i P a g e . .
M u l t i P a g e ,
.


,
. M u l t i P a g e , M u l t i P a g e . , .

: .
: .
.

: .
.

: .

, , . . , , ,
Web- .

CommandButton ()
CancelButton, BackButton, NextButton F i n i s h B u t t o n .



C l i c k . C a n c e l B u t t o n .
MsgBox (. 15.5), ,
. , U s e r F o r m .
.
Private Sub CancelButton_Click()
Msg = " ?"
Ans = MsgBox(Msg, vbQues'tion + vbYesNo, APPNAME)
If Ans = vbYes Then Unload Me
End Sub

394

15. UserForm

. 7.5.5.

.
Private Sub BackButton_Click()
Multipagel.Value = MultiPagel.Value - 1
UpdateControls
End Sub
Private Sub NextButton_Click()
Multipagel.Value = MultiPagel.Value + 1
UpdateControls
End Sub
. Value
MultiPage, , Upd a t e C o n t r o l s ( ).
UpdateControls, 15.1,
BackButton NextButton.

1. ,
Sub UpdateControls
Select Case MultiPagel.Value
Case 0
BackButton.Enabled = False
NextButton.Enabled = True
Case MultiPagel.Pages.Count - 1
BackButton.Enabled = True
NextButton,Enabled = False
Case Else
BackButton,Enabled = True
NextButton,Enabled = True
End Select
1


Me.Caption = APPNAME & " _
& MultiPagel.Value + 1 & " " _
& MultiPagel.Pages.Count


If tbName.Text = "" Then
FinishButton.Enabled = False
Else
FinishButton.Enabled = True
End If
End Sub
UserForm , ( APPNAME
Modulel).
MultiPage (

IV.

395

T e x t B o x , tbName). , .
T e x t B o x , F i n i s h B u t t o n . ,
.



, . ,
Web-, , . ( )
Microsoft. O p t i o n B u t t o n ,
.
, C h a n g e M u l t i P a g e .
M u l t i P a g e ( ), M u l t i P a g e l _ C h a n g e . ( ),
CheckBox , . .
C h e c k Box, ( ),
F r a m e ( ). F o r N e x t
F r a m e , . F r a m e . , ,
, see ,
T e x t B o x ,
(, , ). M u l t i P a g e l _ C h a n g e
15.2.

15.2. ,
Private Sub MultiPagel_Change
'
?
If MultiPagel.Value = 3 Then
CheckBox
Dim ProdCBU To 3) As MSForms .CheckBox
Set ProdCBU) = cbExcel
Set ProdCB{2) cbWord
Set ProdCB(3) = cbAccess
'

Frame
Dim ProdFrame{1 To 3) As MSForms.Frame
Set ProdFrame(1) = FrameExcGl
Set ProdFrame(2) = FrameWord
Set ProdFrame{3) = FrameAccess
TopPos = 2 2
FSpace = 8

396

15. UserForm

AtLeastOne = False
1


For i = 1 3
If ProdCB(i) Then
ProdFrame(i).Visible = True
ProdFrame(i).Top = TopPos
TopPos = TopPos + ProdFrame(i).Height + FSpace
AtLeastOne = True
Else
ProdFrame(i).Visible = False
End If
Next i

?
If AtLeastOne Then
lblHeadings.Visible = True
Image4.Visible = True
lblFinishMsg.Visible = False
Else
lblHeadings.Visible = False
Image4.Visible = False
lblFinishMsg.Visible = True
If tbName = "" Then
lblFinishMsg.Caption =
" 1."
Else
lblFinishMsg.Caption = _
" ."
End If
End If
End If
End Sub


, : U s e r F o r m
. , 15.3, . (). .

15.3.
Private Sub FinishButton_Click()
* Application.WorksheetFunction. _
Coun tA{Range("A:A")) + 1

Cells(, 1) = tbName.Text
'


Select Case True
Case obMale: Cells(r, 2} = ""

IV.

397

Case obFemale: Cells (r, 2) ""


Case obNoAnswer: Cells(r, 2) = ""
End Select
1


Cells(, 3) = cbExcel
Cells(r, 4) = cbWord
Cells(r, 5) = cbAccess

'


If obExcell Then Cells(r, 6) = ""
If obExcel2 Then Cells(r, 6) = 0
If obExcel3 Then Cells(r, 6) = 1
If obExcel4 Then Cells(r, 6) = 2
If obWordl Then Cells(r, 7) =
If Obword2 Then Cells(r, 7) = 0
If obword3 Then Cells(r, 7) = 1
If obWord4 Then Cells(r, 7) = 2
If obAccessl Then Cells(r, 3) ""
If obAccess2 Then Cells(r, 8)
If obAccess3 Then Cells (r, 8) = 1
If obAccess4 Then Cells (r, 8) == 2


Unload Me
End Sub


, , ,
S t y l e M u l t i P a g e .
2 - fmTabStyleNone.

MsgBox
VBA MsgBox ,
. :
, , .
,
VBA MsgBox. ,
. , KsgBox , , , : , MsgBox, .

, U s e r F o r m . , .
. , , ,
..
, MsgBox,
MyMsgBox. , MyMsgBox
.

398

15. UserForm

(
, ).

( (
).

"" ( , ).
MyMsgBox .
MyMsgBox([,

[,

])

MsgBox, , ( ). ,
MyMsgBox , MsgBox: vbOKOnly,
v b Q u e s t i o n , v b D e f a u l t B u t t o n l ..
MsgBox ,

MyMsgBox
MyMsgBox UserForm,
MyMsgBoxForm. . .
I n i t i a l i z e ( U s e r F o r m _ I n i t i a l i z e > .
MyMsgBox
, Web .
Public
Public
Public
Public

PromptI As String
Buttons1 As Integer
Titlel As String
UserClick As Integer

Function MyMsgBox(ByVal Prompt As String, _


Optional ByVal Buttons As Integer, __
Optional ByVal Title As String) As Integer
Prompt1 = Prompt
Buttonsl = Buttons
Titlel = Title
MyMsgBoxForm.Show
MyMsgBox = UserClick
End Function
. 15.6 MyMsgBox (
).
. /5.6.
MsgBox ( )

IV.

, .
Prompt = " ?"
Buttons = vbQuestion + vbYesNo
Title = " "
Ans = MyMsgBox(Prompt, Buttons, Title)


. ( P r o m p t I , B u t t o n s l T i t l e l ) , . ( U s e r C l i c k ) ,
. U s e r F o r m _ I n i t i a l i z e . ( P u b l i c ) .
U s e r F o r m (. 15.7) I m a g e (
), CommandButton, a
T e x t B o x .

. 15,7, UserForm, MyMsgBox


U s e r F o r m _ I n i t i a l i z e .
, ( ).
, ( ).
, .
.
CommandButton.

( API, ).

U s e r F o r m .

CommandButton. , . U s e r C l i c k .
() . :
vbYesNoCancel + V b Q u e s t i o n + VbDef.au! t B u t t o n 3

400

15. UserForm

(, ), .
547 (3+32+512). . :
. , 547 1000100011.
4 6 , 8 10 , 1 2 , .

MyMsgBox
,
MyMsgBoxMod MyMsgBoxForm. .


, , , ..
, ,
. . ,
, .
Excei 2QQ0 Excel, . ,
Excel.
U s e r F o r m ,
:
UserForml.Show vbModeless
v b M c d e l e s s , 0.
, .
UserForml.Show 0
. 15.8 ,
. , .
Web- .


, . : S h e e t S e l e c t i o n C h a n g e S h e e t A c t i v a t e .
ThisWorkbook.
18, .

IV.

401

. 15-8.
,

.
Private Sub Workbook_SheetSelectionChange _
(ByVal Sh As Object, ByVal Target As Range)
Call UpdateBox
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object}
Call UpdateBox
End Sub
UpdateBox, .
Sub UpdateBox()
With UserForml
1

If TypeName(ActiveSheet) <> "Worksheet" Then
.iblFormula.Caption = "N/A"
.lblNumFormat.Caption = "N/A"
.lblLocked.Caption = "N/A"
Exit Sub
End If
.Caption = ": & ActiveCell.Address{False, False)

If ActiveCell.HasFormula Then
.IblFormula.Caption = ActiveCell.Formula
Else
.IblFormula.Caption = "()"
End If
'

.lblNumFormat.Caption = ActiveCell.NumberFormat
'

.lblLocked.Caption = ActiveCell.Locked
End With
End Sub
1

UpdateBox UserForm, . Label


(IblFormula, lblNumFormat lblLocked).
, , .

402

15. UserForm

UserForm "" ,
, .
Excel 97 UserForm .
, .
, Label N/A.
UserForm ( ),
UserForm.
: SheetSelectionChange S h e e t A c t i v a t e .
, , ,
. , . , , , .
.
29.

, ,
. p r e c e d e n t s D e p e n d e n t s .
U s e r F o r m L a b e l . , VBA L a b e l , UserForm,
. 15.9
( Web- ). . ,
Excel, , Info ( Excel ). , , ,
, Web-.

. 15.9. UserForm

IV.

403



CommandButton U s e r F o r m
C l i c k . , CommandButton, :
Private Sub CommandButtonl_Click()
'
End Sub
Private Sub CommandButton2_Click{)
'
End Sub
, , CommandButton. C l i c k CommandButton.
, , .
( C o m m a n d B u t t o n ! C o m m a n d B u t t o n )
E u t t o n C l i c k . B u t t o n C l i c k , .
U s e r F o r m CommandB u t t o n , . ,
, ,
.
.
Web- .

.
1.
C o m m a n d B u t t o n ( Web- 16 CommandButton). , U s e r F o r m l .
2.

( I n s e r t s
Class Module), B t n C l a s s . B u t t o n G r o u p _ C l i c k .
Public WithEvents ButtonGroup As MsForms.CommandButton
Private Sub ButtonGroup_Click(}

404

15, UserForm

Msg = " .
& ButtonGroup.Name & vbCrLf _
& vbCrLf
Msg =. Msg & ":
& ButtonGroup.Caption _
& vbCrLf
Msg = Msg & " :
& ButtonGroup.Left _
& vbCrLf
Msg = Msg & ' : " & ButtonGroup.
MsgBox Msg, vblnformation, ButtonGroup.Name
End Sub
3. YBA . UserForm.
Sub ShowDialogl)
UserForml.Show
End Sub
4. UserForm 15.4.
I n i t i a l i z e UserForm. , "" OKButton. , OKButton
ButtonGroup_Click.

15.4. Button
Dim Buttons() As New BtnClass
Private Sub UserForm_Initialize{J
Dim ButtonCount As Integer
Dim ctl As Control
1

Button
ButtonCount - 0
For Each ctl In UserForml.Controls
If TypeName(ctl) = "CommandButton" Then
If ctl.Name <> "OKButton" Then ' OKButton
ButtonCount = ButtonCount + 1
ReDim Preserve Buttons{1 To ButtonCount)
Set Buttons(ButtonCount).ButtonGroup = ctl
End If
End If
Next ctl
End Sub

Puc. 15.10. ButtanGroup_Clck


IV.

405

ShowPialog,
UserForm.. CommandButton
( ) B\ittonGro\ip_Click.. . 15.L0
, .


. Public WithEvents. ,
CommandButton tionButton, :
Public WithEvents ButtonGroup As MsForms.OptionButton


,
.
UserForm, ( 56 ).
(GetAColor),
UserForm .
Web- .

GetAColor.
Public ColorValue As Variant
Dim Buttons(1 To 56) As Mew ColorButtonClass
Function GetAColor{) As Variant
'
UserForm
'
False,
Dim c t l As Control
Dim -ButtonCount As I n t e g e r
ButtonCount = 0
For Each c t l In UserForml.Controls
1
56 '
1
Tag "ColorButton"
If c t l . T a g = "ColorButton" Then
ButtonCount = ButtonCount + 1
Set Buttons(ButtonCount).ColorButton = c t l
1

Buttons {ButtonCount) . ColorButton , BacJcColor =
ActiveWorkbook.Colors(ButtonCount)
End If
Next c t l
UserForml.Show
GetAColor = ColorValue
End Function

406

15. UserForm

UserForro 56 CommandButton, , .
GetAColor :
UserColor = GetAColor()
UserForm U s e r C o l o r . , .
. 15. UserForm ( ), 56 CommandButton. S a c k C o l o r
. .
. 15.11.

Web- .
(UserForml), 56
CommandButton, .

( C o l o r B u t t o n C l a s s ) , Color/Button.

VBA (Modulel), GetAColor.

, GetAColor.
GetAColor UserForm . , ,
. , GetAColor F a l s e . , , .
, , , . , Web-, .



Excel 5 Excel 95 "" ( vie )',
. , UserForm . , , P i c t u r e Image,
, , .
UserForm ,
Microsoft.

UserForm,

IV,

407

Excel 97
. , .

1:
, Microsoft
UserForm, , ! . 15.12 UserForm, Image.
, U s e r F o r m , , P i c t u r e Image.

. 15.12.
UserForm


U s e r F o r m , .
1. .
2.

U s e r F o r m I m a g e .

3. VBA, GIF.
P i c t u r e Image . VBA L o a d P i c t u r e .
4. . , U s e r F o r m
, . , .

GIF
GIF (
t e m p . g i f ) ( D a t a ) .
Set CurrentChart = Sheets("Data").ChartObjects(1)-Chart
Fname = ThisWorkbook.Path & "\temp.gif"
CurrentChart.Export FileName:=Fname, FilterName:="GIF"
, , , , , .

408

15. UserForm

Picture Image
Image UserForm image 1,
( Fname)
Image:
I m a g e l . P i c t u r e = LoadPicture(Fname)
,
.
.

2:
OWC ChartSpace
13, U s e r F o n n , Toolbox. Microsoft Office XP Office Web Components (OWC).
UserForm. . 15.13
UserForm, C h a r t S p a c e .

userFarm. , ChartSpace.

ChartSpace
Toolbox.
,
Additional Controls. Microsoft Office
Chart 10.0 ( Excel 2000, Microsoft Office
Chart 9.0). , Toolbox.

. 15.13. UserForm
ChartSpace

/V.

409

ChartSpace
UserForm
C h a r t S p a c e U s e r F o r m , .
.
. .


, U s e r F o r m , , .
2:13, 2 :13. , C h a r t S p a c e C h a r t S p a c e l .
Sub CreateChart()
Dim Chartl As ChChart 'WCChart
Dim Seriesl As ChSeries 'WCSeries
Dim r As Integer
Dim XValuesfl To 12)
Dim DataValuestl To 12)
1

ChartSpace
Set chartl = ChartSpacel.Charts.Add

'


With Chartl
.HasTitle = True
.Title.Caption = Range("Bl")
End With
For r = 2 To 13
XValues(r - 1) = Cells(r, 1}
DataValues{r - 1) = Cells{r, 2}
Next r


Set Seriesl = Chartl.SeriesCollection.Add


With Seriesl
.Type = chChartTypeColumnClustered
.SetData chDimCategories, chDataLiteral, XValues
.SetData chDimValues, chDataLi teral, DataValues
End with
End Sub
. Excel 2000,
, -. , WCChart ( c h C h a r t ) . , W C S e r i e s ( c h S e r i e s ) . :
(Xvalu.es), ( D a t a V a l u e s ) .
S e t C h a r t , C h a r t S p a c e . C h a r t C h a r t l .
, 1 . F o r N e x t
.

470

15. UserForm

S e t , S e r i e s
S e r i e s l . W i t h - E n d W i t h ( )
.
OWC . OWC.
Object Browser.
, CWC
Excel. 18
VBA ""
Excel.
. 15.14 . , . ,
GIF.

. 15,14. UserForm
ChartSpa.ce ( Office Web Components)
Office Web Components
Office Web Components Web-. Spreadsheet, Chart Pivot Table. , OWC, OWC .
OWC Microsoft Office 2000 Microsoft Office 2002. . , ,
Microsoft Office 2000 OWC
(, ).
, , Small Business Microsoft Office 2000 OWC.
, OWC Excel . ,
0WC.

IV.

411

Web- .
: Excel 2000 ( OWC 9.0), Excel 2002
( OWC 10.0)



U s e r F o r m ? ?
. 15.15 U s e r F o r m , Microsoft Office Spreadsheet 10.0. , . S p r e a d s h e e t Excel:
18278 262J 44 . 300 ,
Excel.

. 15.!5. UserForm -

Spreadsheet

Spreadsheet
S p r e a d s h e e t
Toolbox. , Additional Control. Microsoft
Office Spreadsheet 10.0 ( Excel 2000,
Microsoft Office S p r e a d s h e e t 9.0 ,
Toolbox.

Spreadsheet
UserForm
S p r e a d s h e e t U s e r F o r m , . U s e r F o r m , , . .

412

15. UserForm


S p r e a d s h e e t ,
. . 15.16. ,
( )
.
.
Spreadsheet . EditBox VBA.

. 15.16. UserForm
S p r e a d shee t
UserForm , . ,
S p r e a d s h e e t Toolbox.
1. UserForm
S p r e a d s h e e t . He , {Spreadsheet].).
, , . , .
2. S p r e a d s h e e t .
.
, . 15.17.
3. 2 .
,
, .
4. , . 15.16.
, .
5. 1 : . 5:
=(2/12;;-1)
6. 1:
. , ( ,
).

/V.

413

7.

.
1 : 5.
, .

8. .
9. , CommandButton. (
C a n c e l B u t t o n ) . ( P a s t e B u t t o n ) , Excel-

. 15.17,

Spreadsheet
VBA.
. , ( ,
Excel). , ,
U s e r F o r m .
( ), ( ,
). , ,
.
:
Private Sub UserForm_lnitialize{)
Spreadsheetl.ActiveSheet.Protect
.End Sub
P a s t e B u t t o n U s e r F o r m
:
Private Sub PasteButton_Click()
ActiveCell.Value = Spreadsheetl.Range("B5")
Unload Me
End Sub
5 ( S p r e a d s h e e t ) Excel. U s e r F o r m
.
, (5 .
-
S p r e a d s h e e t U s e r F o r m . , ,
, .

414

15. UserForm


u s e r F o r m , .
Excel (. 15.18). !
^ .

. 15.18. Excel

, . - .

.
. Excel
32 .
, . Excel .
, ,
, . !
,

. Excel .
, Excel
.
. Excel
.
, . Excel
,
, Excel ,
(*, ? #).
VBA, . Excel
VBA .

IV.

415


( ), , .


, .
1.

d a t a f o r m . x l a V/eb- .

2. Excel ^ .
3. d a t a f o r m . x l a , .



: flaHHbie^JWalk
Enhanced Data Form.
, .


. , , U s e r F o r m
IV.
U s e r F o r m .

416

15. UserForm

, . VBA
. 19 ,
. 20 , 21
.


Excel VBA

Excel. ,
. , Excel .

Excel
(, ,
). ,
(, ). Excel ( ) ,
Excel,
.
Excel, Office, .
. , , , .
,
^. . , ,
, .
.
,
. . , VBA , .
Sub T o g g l e G r i d D i s p l a y ( )
ActiveWindow.DisplayGridlines = _
Not ActiveWindow.DisplayGridlin.es
End Sub

,
. , , .
.
, IV, ( ).

VBA
- Excel 5 VBA,
Excel XLM Excel
, .
VBA Excel, VBA. , , . Power
Utility Pak Excel.
VBA .
VBA , .
VBA.
, .

VBA Excel , Excel, ,


.

Excel . ,
, .
,


Excel , , . , ? .
- Excel. , ,
.

.
, . , , , .

. , .

419

, Excel. ,
,
{ , Excel).

. , ,
.

. VBA. ,
, .

.
, , .

:
Excel,
Power Utility Pak. Text Tools . , .
( , ).
, .
, .

( )

Excel ,
. ,
.
.. ,
, ,
. , Excel . , Excel ,
.
, " ...".

Text Tools
,
. , .
, Excel.
, ,
, Excel.

420

(
)
, .

16. Excel VBA

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


Text Tools,
. .
S t a r t T e x t T o o l s , Excel (, ), .

, .
. .
. .
. .
. 16.1 Text Tools.

. 16.1.
Text Tools

Text Tools
Text Tools .

. .
.

VBA. (modMerms)
. (modMain)

V.

421

. ,
, .
UserForm. (FonciMain) . (FormMenus) .
Text Tools Web- .
, Power Utility Pak.

FormMain
, . .
FormMain M u l t i P a g e , , . . 16.2
M u l t i P a g e .

. 16.2. FormMain
MultiPage,
, M u l t i P a g e ,
, ( ). FormMain M u l t i P a g e .

R e f E d i t . U s e r F o r m _ I n i t i a l i z e . , R e f E d i t .

16. Excel VBA

. CommandButton, .
FormHelp.
. .
. CommandButton
,
: .
. CommandButton UserForm.
. CommandButton ,
M u l t i P a g e .
, ,
.
Excel, FormMain ,
.
Text Tools , .
. . ,
.

modMain
modMain , .


, modMain.
P u b l i c Consc APPNAME As S t r i n g = "Text T o o l s "
"
Type OrigData
OldText As V a r i a n t
Address As S t r i n g
End Type
, , . ( "
" ).
, , OrigData. , .

StartTextTools
StartTextTools:
Sub StartTextTools(}
If ValidContext(True, True, False, False, False, True) Then _
FormMain.Show
End Sub

V.

42

, . (ValidContext), , .
ValidContext True,
FormMain.
( Boolean).
VisWin. True,
.
Wksht. True, .
RngSel. True, .
M u l t i S e l . True,
.
Chart. True,
.
Prot. True, .
, ValidCont e x t . , VisWin True, . Wksht
True, , .
, ValidContext ,
(. 16.3), F a l s e
.

. 16.3, ValidContext
,

Text Tools ValidContext
.
Vi sWin. .
Wksht, .
Prot. .
Text Tools .
RangeSelection U s e r F o r m _ i n i t i a l i z e FormMain. ,
,
ValidContext , . , ,
Text Tools- Power
Utility Pak .
ValidContext 16.1.

424

16. Excel VBA

. 16.1.
Function ValidContext(VisWin, Wksht, RngSel, MultSel, _
Chart, Prot) As Boolean
Dim VisWinCnt As Integer
Dim Win As Window
Const MsgVisWin As String = _
" ."
Const MsgWksht As String = _
" ."
Const MsgRngSel As String = _
" ." & vbCrLf & _
" ."
Const MsgMultSel As String = _
" ." _
& vtaCrLf & " ."
Const MsgChart As String = __
" ."
Const MsgProt As String = _
"He ." _
& vbCrLf & " ."
ValidContext = True
1


If VisWin Then
VisWinCnt =
For Each Win In Application-Windows
If Win.Visible Then VisWinCnt = VisWinCnt + 1
Next
If VisWinCnt = 0 Then
MsgBox MsgVisWin, vbCritical, APPNAMS
ValidContext = False
Exit Function
End If
End If

'


If Wkshc Then
If TypeName(Activesheet) <> "Worksheet" Then
MsgBox MsgWksht, vbCritical,
ValidContext = False
Exit Function
End If
End If


If RngSel Then
If TypeName(Selection) <> "Range" Then
MsgBox MsgRngSel, vbCritical, APPNAME
ValidContext = False
Exit Function
End If
End If

'

V.

425

If MultSel Then
If TypeName(Selection) = "Range" Then
If Selection.Areas.Count > 1 Then
MsgBox MsgMultSel, vbCritical, APPNAME
Exit Function
End If
End If
End If
1

.
If Chart Then
If ActiveChart Is Nothing Then
MsgBox MsgChart, vbCritical, APPNAME
ValidContext = False
Exit Function
End If
End If


If Prot Then
If ActiveSheet.ProtectContents Then
MsgBox MsgProt, vbCritical, APPNAME
ValidContext = False
Exit Function
End If
End If
End Function

ApplyButton_Click
Text Tools VBA,
FormMain. ApplyB\itton_Click 16.2 ,
.

16.2.

Private Sub ApplyButton_Click()
1

Dim i As Integer
Dim WorkRange As Range
1


If Not ValidReference(RefEditl.Text) Then
MsgBox " .", vblnformation, APPNAME
Application.ScreenUpdating = True
With RefEditl
.SelStart = 0
.SelLength = 100
.SetFocus
End With
Exit Sub
End If

426

16. Excel VBA

Application.ScreenUpdating = False
Select Case MultiPagel.Value
Case 0: Call ChangeCaseTab
Case 1: Call AddTextTab
Case 2: Call RemoveTextTab
Case 3: Call RemoveSpacesTab
End Select
Appli ca t i on.ScreenUpda ting = True
End Sub
A p p l y B u t t o n _ C l i c k . V a l i d R e f , , Ref E d i t . , , Ref E d i t .
V a l i d R e f e r e n c e . T r u e , . , VBA
Range.
Function ValidReference(ref As String) As Boolean
' True, RefEdit
'
Dim x As Range
On Error Resume Next
Set x = Range(ref)
If Err = 0 Then ValidReference = True _
Else ValidReference = False
End Function
A p p l y B u t t o n _ C l i c k .
M u l t i P a g e . M u l t i P a g e , . (, M u l t i P a g e
0, 1.) , , .
, A p p l y B u t t o n _ c l i c k
. , , . .
.


, ,
Text Tools.


M u l t i P a g e (. 16.4) , .
, "11 .

V.

427

. 16.4.

ApplyButton_Click ChangeCaseTab ,
MultiPage 0 (.. ). 16.3 ChangeCaseTab.

16.3.
Sub ChangeCaseTab(}
Dim WorkRange As Range
Dim Cell As Range
Dim CellCount As Long
Set WorkRange = CreateWorkRange(Range(RefEditl.Text), _
True)
If WorkRange Is Nothing Then Exit Sub
CellCount = 0
ReDim LocalUndo(CellCount)
1

'


For Each Cell In WorkRange

CellCount = CellCount + 1
ReDim Preserve LocalUndo(CellCount)
LocalUndo(CellCount).OldText = Cell.Value
LocalUndo(CellCount).Address = Cell.Address

Select Case True
Case ChangeCaseProper
Cell.Value = Application.Proper(Cell.Value)
Case ChangeCaseUpper
Cell.Value = UCase(Cell.Value)
Case ChangeCaseLower
Cell.Value = LCase{Cell.Value)
End Select
Next Cell


UndoButton.Enabled = True
UndoButton.Caption = "Undo Case Change"
End Sub
Range, WorkRange. WorkRange -

428

16. Excel VBA

, , . , N o t h i n g .
C r e a t e W o r k R a n g e ( R a n g e )
.
R a n g e . , Ref E d i t .
T e x t Only. T r u e , .
C r e a t e W o r k R a n g e , 16.4,
, .. Text Tools.

16.4. CreateWorkRange
Function CreateWorkRange(r As Range, TextOnly As Boolean) As Range
'
Range,
1
, . TextOnly
'
True,
Set CreateWorkRange = Nothing
Select Case r.Count
Case 1 '
If r.HasFormula Then Exit Function
If TextOnly Then
If IsNumeric{r.Value) Then
Exi t Funct ion
Else
Set CreateWorkRange = r
End If
Else
If Not IsEmptyfr/ Then Set CreateWorkRange = r
End If
Case Else '
On Error Resume Next
If TextOnly Then
Set CreateWorkRange = _
r.SpecialCells(xlConstants, xlTextValues)
If Err <> 0 Then Exit Function
Else
Set CreateWorkRange = _
r .SpecialCells (xlConstants, xlTextValues + xlNumbers)
If Err <> 0 Then Exit Function
End If
End Select
End Function
createworkRange s p e c i a l c e l l . ,
Excel
. , <F5>
.

V.

423

.
. , ,
. , . C r e a t e W o r k R a n g e
, , .
WorkRange , C h a n g e C a s e T a b
WorkRange.
, .
.


M u l t i P a g e (. 16.5) . ,
.

. 16.5.

A p p l y B u t t o n _ _ C l i c k AddTextTab, V a l u e M u l t i P a g e ; 1 (..
M u l t i P a g e ) . 16.5 AddTextTab.

16.5.
Sub AddTextTab{)
Dim WorkRange As Range
Dim Cell As Range
Dim NewText As String
Dim InsPos As Integer
Dim CellCount As Long
Set WorkRange = _
CreateWorkRange(Range(RefEditl.Text), cblgnoreNonTextl)
If WorkRange Is Nothing Then Exit Sub
NewText = TextToAdd.Text
If NewText = "" Then Exit Sub

If OptionAddToLeft And Left(NewText, 1) Like [=+-]"
MsgBox " .", __

430

IB. Excel VBA

vblnfonnation, APPNAME
With TextToAdd
.Selstart = 0
.SelLength = Len(.Text)
.SetPocus
End With
Exit Sub
End If
1

?
If OptionAddToMiddle Then
InsPos = Val(InsertPos.Caption)
If InsPos = 0 Then Exit Sub
End If

'


CellCount =
ReDim LocalUndo(CellCount)
For Each Cell In WorkRange
With Cell

CellCount = CellCount + 1
ReDim Preserve LocalUndo(CellCount)
With LocalUndo(CellCount)
.OldText = Cell.Value
.Address = Cell.Address
End With
If OptionAddToLeft Then .Value = NewText & .Value
If OptionAddToRight Then .Value = .Value & NewText
If OptionAddToMiddle Then
If InsPos > Len{.Value) Then
.Value = .Value & NewText
Else
.Value = Left(-Value, InsPos) & NewText & _
Right(.Value, Len<.Value) - InsPos)
End If
End if
End With
Next Cell

Undo
UndoButton.Enabled = True
UndoButton.Caption = " "
End Sub
C h a n g e C a s e T a b .
, , ,
+,- = . , Excel .


M u l t i P a g e (. 16.6)
. ,
.

V.

431

. 16.6,
ApplyButton_Click Remove Text Tab ,
Value MultiPage 2 (..
MultiPage). 16.6
Remove t Tab.

16.6.
Sub RemoveTextTab()
Dim WorkRange As Range
Dim Cell As Range
Dim NumToDel As Integer
Dim CellCount As Long
Set WorkRange = _
CreateWorkRange (Range (Ref Edit].. Text) , cbIgnoreNonText2 )
If WorkRange Is Nothing Then Exit Sub
NumToDel = Val(CharstoDelete.Caption)
If NumToDel = 0 Then Exit Sub
1


ReDim LocalUndo(0)
CellCount
For Each Cell In WorkRange
With Cell

CellCount = CellCount + 1
ReDim Preserve LocalUndo(CellCount)
LocalUndo {CellCount) .OldText. = .Value
LocalUndo(CellCount).Address = .Address
If LenfCell .Value) <= NumToDel Then __
NumToDel = Len(.Value)
Select Case True
Case OptionDeleteFroxnLef t
.Value = Right(.Value, Len(.Value) - NumToDel)
Case OptionDeleteFromRight
.Value = Left(.Value, Len(.Value) - NumToDel)
Case OptionDeleteFromMiddle
.Value = RemoveChars{-Value, _
CInt(BeginChar.Caption), NumToDel)
End Select
End With
Next Cell

432

16, Excel VBA

'

Undo
UndoButton.Enabled = True
UndpButton.Caption = "
End Sub

"

RemoveTextTab , A p p l y B u t t o n _ C l i c k . , RemoveChars, .
R e m o v e C h a r s , (),
() ( t ) .
Private Function RemoveChars{t, b, n) As String
Dim k As Integer
Dim Temp As String
Temp = ""
For k = 1 To Len(t)
If k < b Or k >= b + n Then
Temp = Temp & Mid{t, k, 1)
End If
Next k
RemoveChars = Temp
End Function


M u l t i P a g e (. 16.7) .

. 16.7.

A p p l y B u t t o n _ C l i c k Remove S p a c e s Tab , M u l t i P a g e 3 (..
M u l t i P a g e ) . 16.7 RemoveSpacesTab.

16.7.
Sub RemoveSpacesTab()
Dim WorkRange As Range
Dim Cell As Range
Dim CellCount As Long
Set WorkRange = CreateWorkRange _
(Range(RefEditl.Text), True)
If WorkRange Is Nothing Then Exit Sub

V.

433


CellCount = 0
ReDim LocalUndo(CellCount)

For Each Cell In WorkRange


With Cell

CellCount = CellCount + 1
ReDim Preserve LocalUndo(CellCount)
LocalUndo(CellCount).oldText - .Value
LocalUndo(CellCount).Address = .Address
Select Case True
Case OptionRemoveExcess
.Value = _
Application.WorkEiheeCFunction.Trim( .Value)
Case OptionRemoveLeft
.Value = LTrim(.Value)
Case OptionRemoveRight
.Value = RTrim{.Value)
Case OptionRemoveBoth
.Value = Trim(.Value)
Case OptionRemoveAllSpaces
.Value = RemoveSpaces(.Value)
End Select
End With
Next Cell

Undo
UndoButton.Enabled = True
UndoButton.Caption = " "
End Sub
, Excel. VBA.
, .
Private Function RemoveSpaces(t) As String
1

Dim NumChars As Integer
Dim i As Integer
NumChars = Len(t)
RemoveSpaces = ""
For i = 1 To NumChars
If Mid(t, i, 1) <> " " Then _
RemoveSpaces = RemoveSpaces & Mid(t, i, 1)
Next i
End Function


, Excel, , Text Tools, . , .
" VBA" .

434

16. Excel VBA

Text Tools modMain OrigData


.
Type OrigData
OldText As Variant
Address As String
End Type
OrigData : OldText ( ) Address ( ).
( LocalUndo)
OrigData.
'


CellCount = CellCount + 1
ReDim Preserve LocalUndo(CellCount)
LocalUndo(CellCount).OldText = .Value
LocalUndo(CellCount).Address = .Address

FormMain. , RemoveTextTab :
'
Undo
UndoButton.Enabled = True
UndoButton.Caption = " "
, LocalUndo .
, UndoButton_Click.
.
Private Sub UndoButton_Click()
Dim i As Integer
Application.ScreenUpdating = False
1


For i = 1 To UBound(LocalUndo)
Range(LocalUndo(i).Address).Value a LocalUndo{i).OldText
Next i
Application.ScreenUpdating = True


UndoButton.Caption = "Undo"
UndoButton.Enabled = False
End Sub
LocalUndo .
Web- , ^ VBA.

ShowStats
, . . 16.8 .

V.

435

. 16.8.
*

VBA
.
, a Excel, .
Excel 97 .
VBA , . : . , , .
VBA .
- ,
^. , . . , .
A p p l i c a t i o n OnUndo,
, ^, ,
^. , .
^ , undaMyMacra.
Application.OnUndo " ", "UndoMyMacro"
16.8 .

16.8.
Private Sub StatsButton^Click()
'

Dim WorkRange As Range
Dim NumWords As Integer
Dim NumChars As Integer
Dim CellLength As Integer
Dim NonBlanks As Integer
Dim Cell As Range
Dim Msg As String
Dim Words As Integer
Dim Contents As String
Dim i As Integer
1

436


If Not ValidReferencefRefEditl.Text) Then
MsgBox " .", vblnformation, APPNAME
With RefEditl
.SelStart = 0
.SelLength = 100
.SetFocus
End With

16. Excel VBA

Exit Sub
End If
Set WorkRange = CreateWorkRange(Range(RefEditl.Text), _
True)
If WorkRange Is Nothing Then
MsgBox _
11
.", _
vblnformation, APPNAME
Exit Sub
End if
NonBlanks = WorkRange.Count
NumWords = 0
NumChars = 0
For Each Cell In WorkRange
CellLength = Len(Cell.Value)
NuinChars = NumChars + CellLength
Contents = Application.Trim(Cell.Value)
Words = 1
For i = 1 To Len(Contents)
If Mid(Contents, i, 1) = " " Then _
Words = Words + 1
Next i
If Len(Contents) = 0 Then Words = 0 '
NumWords - NumWords + Words
Next Cell
Msg = " " & vbCrLf & vbCrLf
Msg = Msg & " :" & Chr(9) & NonBlanks _
& Chr(13)
Hsg = Msg & ":" & Chr(9) & Chr{9) & NumWords _
& Chr(13)
Msg = Msg & ":" & C h r ( 9 ) & NumChars & Chr(13)
Msg = Msg & " : " & C h r ( 9 ) & Format(NumChars _
/ NonBlanks, " # . 0 0 " )
MsgBox Msg, v b l n f o r m a t i o n , APPNAME
E x i t Sub
End Sub
S h o w S t a t s , . , ( Ref E d i t ) , .


.
Text Tools , ,
, U^erForm.
, .
ComboBox,
L a b e l .
24 ( )
.

V.

437

. 16.9 , .
(FormHelp) ,
- FormMain.


Text Tools, , , Text Tools. Workbook_Open ( T h i s W o r k b o o k ) .
:
Private Sub Workbook_Open()
Call GreateMenu
End Sub

. 16.9.
ComboBox Label
Workbook^Bef oreClose .
, :
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call DeleteMenu
End Sub
CreateMenu DeleteMenu VBA modMenus. CreateMenu , DeleteMenu . .
23.


Text Tools.
, . .

,
Excel. , , . Excel. , Text Tools ,
.
,
Excel .

438

1. Excel VBA

. .

/ (
) ,
. .

, ,
' . .

,
. .


,
. .

, .
.

. ,
, , .
.

.
, .


. , .

Text Tools
,
. ,
. , , , .
, ,
.
Text Tools
. 21 .

Excel VBA. ,
VBA, Text Tools.
Excel
. "" .

V.

439


Excel. Excel 5.
Excel (
). ,

,
. , . Excel
VBA,
.
Excel 2000
. ,
.
,
, ,
Excel 2000 .

. ,
,
.


VBA .
. 17.1 . : , ,
. .


. 17.2 , .
. .

, Sum ().

. 17.1.

. 17.2. , , . 17.1

. , .
Sub Macrol()
1

Range("Al").Select
ActiveWorkbook.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:="flHCTl!RlCl:R13C4").
CreatePivotTable _
TableDestination:="", _
TableName:="1" ,
DefaulCVersion:=xlPivotTableVersionlO
AcCiveSheet.PivotTableWizard _
TableDestination:=ActiveSheet.Cells(3f 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet. PivotTables ("1") . _,
PivotFields{"")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables(1").
PivotFields(""}
.Orientation = xlRowField
.Position = 1
End With

With

V.

441

With ActiveSheet.PivotTables("1"). _
PivotFields{"")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("1") . _
AddDataField ActiveSheet.PivotTables("1"). _
PivotFields(""), " ", xlSum
End Sub

. , ,
. :
.
, , , . , .


VBA, . ,
, .

PivotCaches PivotCache Workbook.

P i v o t T a b l e s P i v o t T a b l e Worksheet.

P i v o t F i e l d s P i v o t T a b l e .
P i v o t l t e m s .
C r e a t e P i v o t T a b l e PivotCache, , -.

Pivot Table Wizard Worksheet,


( . ).


,
, . , .
17.1 , , .

17.1. VBA
Sub CreatePivotTable()
1

Dim PTCache As PivotCache
Dim PT As PivotTable
Set PTCache = ActiveWorkbook.Pivotcaches.Add _
{SourceType:=xlDatabase, _
SourceData:=Range{"Al").CurrentRegion.Address)

442

17.

Set PT = PTCache.CreatePivotTable _
(TableDestination:=", _
TableName:-"!")
With PT
.PivotFields("").Orientation = xlPageField
.PivotFields("").Orientation = xlColumnField
.PivotFields("").Orientation = xlRowField
.PivotFields("").Orientation = xlDataField
End With
End Sub
C r e a t e P i v o t T a b l e ( ),
: PivotCache .
A c t i v e S h e e t . PivotCaches A c t i v e S h e e t . P i v o t T a b l s s .
PivotCache Add.
PivotTable, C r e a t e P i v o t T a b l e PivotCaches.
(
, , ).
, , PivotCache (
"1! RICl :R13C4"). CreatePivotTable
, Al. ,
, .
P i v o t F i e l d s . , , .
, P i v o t F i e l d s (1) P i v o t F i e l d s ("" ).
,
, .
, , . "" . ,
.


VBA,
.

. 17.3 . 15840
.
, . , .
.

V.

443

Fuc. /7.3.

. 17,4. , , . 17.3

444

17.

Web- .


. 17.4 ,
. , ,
, ( 1 , 2 , 4 ) , .


, 17.2, , .

17.2.
Sub CreatePivotTable()
Dim PTcache As PivotCache
Dim PT As PivotTable
On Error Resume Next
Sheets("1")-DrawingObjects{"TextBoxWait").Visible = True
On Error GoTo 0
Application.ScreenUpdating = False
,
On Error Resume Next
Application.DisplayAlerts m False
Sheets(""1-Delete
On Error GoTo 0
1

PivotCache
Set PTcache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=Range("Al").CurrentRegion.Address)

'


Worksheets.Add
ActiveSheet.Name = ""

-
Set PT = PTcache.CreatePivotTable( _
TableDestination:=Sheets(""}.Range("Al"), _
TableName:="BudgetPivot")

'

With PT

.PivotFields("").Orientation = xlRowField
.PivotFields("").Orientation = xlColumnField
.PivotFields("").Orientation = xlPageField
.PivotFields("").Orientation = xlDataField

V.

445

.PivotFields("")-Orientation = xlDataField
1


.CalculatedFields.Add "", "=-"
.PivotFields("").Orientation = xlDataField

'


.PivotFields("").Calculatedltems.Add "1",
+ - "
.PivotFields("").Calculatedltems.Add "2",
- + "
.PivotFields("").Calculatedltems.Add "",
+ + "

.PivotFields("MecHU").Calculatedltems.Add 4",
- - "

'


.PivotFields("").Pivotltems("1").Position
.PivotFields{"").Pivotltems("2").Position
.PivotFields("").Pivotltems("").Position
.PivotFields(""}.Pivotltems("4").Position

"= _
"= _
"= _
"= _

=
=
=
=

4
8
12
16


.PivotFields(" ").Caption = " (S)"
.PivotFields{" ").Caption = _
" (S)"
.PivotFields (" 11) .Caption = " ($)"

End With
Application.ScreenUpdating = True
On Error Resume Next
Sheets{"1").DrawingQbjects("TextBoxWait").Visible = False
End Sub


C r e a t e P i v o t T a b l e ( 17.2)
P i v o t s h e e t , PivotCache, , , .
.
.
.
.
.
.
Add C a l c u l a t e d F i e l d s ,
.
. .
, -

446

17.

(, 1 ). , , . , ( $ ) .
, ,
. , .



.
, Excel
. VBA, , Access.
, , .

, , 17.3. , b u d g e t .mdb ,

17.3.

Sub CreatePivotTableFromDB()
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim DBFile As String
Dim ConString As String
Dim QueryString As String
'

,
On Error Resume Next
Application.DisplayAlerts = False
Sheets("").Delete
On Error GoTo 0
Application.DisplayAlerts = True

PivotCache
Set PTCache = ActiveWorkbook.Pivotcaches.Add _
{SourceType:=xlExternal)


DBFile = ThisWorkbook.Path & "\budget.mdb"
ConString = HODBC;DSN=MS Access Database;DBQ=" & DBFile
QueryString = "SELECT * FROM BUDGET"
With PTCache
.Connection = ConString

V.

447

.CommandText = QueryString
End With
1


Worksheets.Add
ActiveSheet.Name = ""

'


Set PT = PTCache.CreatePivotTable( _
TableDestination:=Sheets("").Range*"Al"),
TableName:="Budgetpivot"}

Add fields
With PT
.PivotFields("")-Orientation = xlRowField
.PivotFields("").Orientation = xlColumnField
.PivotFields("").Orientation = xlPageField
.PivotFields{""}.Orientation = xlDataField
.PivotFields("")-Orientation = xlDataField
End With
End Sub
, SourceType Add PivotCaches
x l E x t e r n a l . (
) SourceType xlDatabase.
PivotCache .
. .
ODBC,
Microsoft Access budget. mdb.
. SQL, ,
. Budget.
PivotCache
Connection CommandText. , C r e a t e p i v o t T a b l e .
SQL . .
,
SQL.


, ,
. (. 17.5) 100 .
, .
. 17.6 ,
.
.

448

17.

. 17.5.

. 17.6.
,
VBA

V.

449

VBA, , 17.4.

17.4.
(
Sub MakePivotTables()
1
14
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim SummarySheet As Worksheet
Dim ItemName As String
Dim Row As Integer, i As Integer
Application.ScreenUpdating = False
1

,
On Error Resume Next
Application.DisplayAlerts = False
Sheets("").Delete
On Error GoTo 0

'


Set SuminarySheet = Worksheets .Add
ActiveSheet.Name = ""

PivotCache
Set PTCache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=Sheets(""}.Range("Al"). _
CurrentRegion.Address 1

'

Row = 1
For i = 1 To 14
ItemName = Sheets("").Cells(1, i + 2)

Set PT = PTCache.CreatePivotTable _
(TableDestination:=SummarySheet.Cells(Row, 1 ) , _
TableName:=ItemName)
Row = Row + 1 1

With PT.PivotFieldsdtemNamei
.Orientation = xlDataField
.Name = "-"
End With
With PT.PivotFields{ItemName)
.Orientation = xlDataField
.Name = ""
.Calculation = xlPercentOfTotal
End With
PT.AddFields RowFields:=Array(ItemName, "")
PT.PivotFields("").Orientation = xlColumnField
PT.PivotFields("").Orientation = xlColumnField
Next i

450

17.


SummarySheet.Activate
With Columns{"A:A")
.Replace "1", " "
-Replace "2", "He "
.Replace "3", " "

.Replace 4 " , ""


.Replace "5", " "
End With
1


Columns("A:G").EntireColumn.AutoFit
End Sub
,
P i v o t C a c h e . Row .
(, 1 ) . ,
.


Excel
. ,
,
. , . ,
, , t i o n B u t t o n s CheckBox (. 17.7).
<1, 2 ,
4), . VBA, O p t i o n B u t t o n l , 17.5. .
O p t i o n B u t t o n .

. 17.7.

451

17.5.
Private Sub OptionButton2_Click()
1

Application.ScreenUpdating = False
With ActiveSheet.PivotTables(1).PivotFields("")
.PivotltemsC'KBl") .Visible = True
.Pivotltems("2")-Visible = True
.PivotIterns("")-Visible = True
.Pivotltems("4"}.Visible = True
.Pivotltems("").Visible = False
.Pivotltems("").Visible = False
.Pivotltems{"").Visible = False
.Pivotltems{"").Visible = False
.Pivotltems("").Visible = False
.Pivotltems("").Visible = False
.Pivotltems("").Visible = False
.Pivotltems(""}.Visible = False
.Pivotltems("").Visible = False
.Pivotltems("").Visible = False
.Pivotltems("").Visible = False
.Pivotltems(""\-Visible = False
End With
End Sub
C h e c k B o x . .
Private Sub CheckBoxl_Click()
1

Application.ScreenUpdating = False
ActiveSheet.PivotTables(1)-ColumnGrand = CheckBoxl.Value
End Sub
Private Sub CheckBox2_Click{)
1

Application.ScreenUpdating = False
ActiveSheet.PivotTables(1).RowGrand = CheckBox2.Value
End Sub
;/ . ,
VBA, , . .

VBA.
npi VB .

452

17.

Excel . , . Excel 100 ,



.


, , ,
. , VBA . , VBA,
.
. Excel.


Excel .

. .

. .

,
Excel 5/95. (.
" "), .

. VBA. , VBA
.
<F11>. Excel , .
" ".
, C h a r t . VBA A c t i v e C h a r t C h a r t (
). , ( Workbook, ActiveWorkbook).
. , Name C h a r t ;
MsgBox A c t i v e C h a r t .
, .
,
VBA.


,
, , . ,
Excel, .
Chart , , . , , .
, Chart .
, , . Application (Excel). Application Workbook, Worksheet. Worksheet ChartObject, Chart. Chart
C h a r t T i t l e , C h a r t T i t l e C h a r a c t e r s . Text
C h a r a c t e r s , .
, C h a r a c t e r s .
,
Application
Workbook
Worksheet
ChartObject
Chart
ChartTitle
Characters
Excel
Excel, "".

454

18.

VBA , , . , ,
VBA:
Worksheets("1).ChartObj ects(1).Chart.ChartTitle.
__
Characters.Text = " "
Text C h a r t T i t l e :
Worksheets("1").ChartObjects(1).Chart.ChartTitle.
Text = " "

C h a r a c t e r s , .
,
Workbook. ChartObj e t s
1. Chart Chart. C h a r t T i t l e C h a r t T i t l e , C h a r a c t e r s C h a r a c t e r s .
Text C h a r a c t e r s .
,
Worksheet ChartObj e c t . ,
.
Workbook
Chart
ChartTitle
Characters
VBA,
;
Sheets("1").ChartTitle.Characters.Text = " "
Text C h a r t T i t l e :
Sheets("1").ChartTitle.Text = " "
, Chart ChartObj ect. , ,
ChartObj ect, workbook,
, Chart.
MsgBox TypeName{Sheets("1").ChartObjects(1).Chart)
Msgbox TypeName(Sheets("1"))

ChartObjects,
( charts). charts sheets .


, C h a r t
. ,

V.

4S5

, ,
VBA.
Excel ,
, C h a r t A c t i v e C h a r t .
Excel ( ) , () VBA. , ,
. ,
, ,
(, ).


(
. 18.1). ( ) .

. 18,1. Excel
, .

Sub MacrolO
1

456


Range("Al:F2"}.select
Charta.Add
ActiveChart.ChartType * xlColumnCluetered
ActiveChart.SetSourceData _
Source . Sheets 1 .Range ("A1-.F2") , _
PlotBy:=xlRows
ActiveChart.LocaCion _
Where:=xlLocationAsObject,
Name:= 1"
ActiveChart.HasLegend = False
ActiveChart.ApplyDataLabels _
Type:=xlDataLabelsShowValue, LegendKey;=False
ActiveChart.HasDataTable = False
ActiveChart.Axes(xlCategory).Select
Selection.TickLabels.Orientation = xlHorizontal

18.

ActiveChart.ChartTitle.Select
Selection.Font.Bold = True
Selection.AutoScaleFont = True
With Selection.Font
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
-Colorlndex = xlAutomatic
.Background = xlAutomatic
End With
ActiveChart.PlotArea.Select
Selection.Top = 18
Selection.Height = 162
ActiveChart.ChartArea.Select
ActiveChart.Axes(xlValue).Select
With ActiveChart.Axes(xlValue)
.MinimumScalelsAuto = True
.MaximumScale = 0.6
.MinorUnitlsAuto = True
.MajorUnitlsAuto = True
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
End With
End Sub


, , , . ,
.
. , ,
. S c r e e n U p d a t i n g F a l s e
.
Sub CleanedMacro()
Application.ScreenUpdating = False
Charts.Add
ActiveChart.Location _
Where:=xlLocationAsObj ect, Name: ="1"
With ActiveChart
.SetSourceData Range("Al:F2")
.HasTi tie = True
.ChartType = xlColumnClustered
-HasLegend = False
ApplyDataLabels Type:=xlDataLabelsShowValue
-Axes(xlCategory).TickLabels.Orientation = xlHorizontal
-ChartTitle.Font.Bold = True
.ChartTitle.Font.Size * 12
.PlotArea.Top = 1 8
.PlotArea.Height = 162

V.

457

.Axes(xlValue).MaximumScale = 0.6
.Deselect
End With
Application.ScreenUpdating = True
End Sub
Add c h a r t s
. L o c a t i o n .
L o c a t i o n C h a r t , , . , .
Sub Test()
Charts.Add
MsgBox ActiveChart.Name
ActiveChart.Location _
Where: =xlLocationAsObject, Name: = "J]MCTl"
MsgBox ActiveChart.Name
End Sub
( ) ,
. L o c a t i o n
. ,
. C h a r t C h a r t ,
C h a r t O b j e c t .
, ( ), Web- . ,
.


VBA
, .


, . VBA
A c t i v a t e . :
ActiveSheet.ChartObjects("1"}.Activate
,
:
Sheets("!"}.Activate

458

18.

,
A c t i v e C h a r t . , . , .
MsgBox ActiveChart.Name
VBA, . , , (
1 ). , .
Sub ModifyChartlO
ActiveSheet.ChartObjects{ "].") .Activate
ActiveChart.Type = xlArea
ActiveChart.Deselect
End Sub
Sub ModifyChart2()
ActiveSheet.ChartObjects{"1").Chart.Type = xlArea
End Sub
, , .
,
^,
. Chart ( ChartObject)
, ChartObj e c t .
,
VBA. , ChartObject
1 , .
Sub ConvertChartl()
Sheets{"1).ChartObjects(I).Chart.
Location xlLocationAsNewSheet, ""
End Sub
, : 1.
Sub ConvertChart2()
Charts{"") _
.Location xlLocationAsObject, "1"
End Sub
Location
.

, C h a r t O b j e c t ,
, . , . ,
(. 18.2).
, ( ). , . , .

V.

459

. 18.2.
VBA C h a r t O b j e c t :
ActiveSheet.ChartObjects(1)-Activate
ActiveChart.ShowWindow = True
, " "
.


, , :
ActiveWindow.Visible = F a l s e
, , .
Deselect, :
ActiveChart.Deselect
-. , Deselect .
V i s i b l e ActiveWindow F a l s e , .


(
). , ,
.
: VBA , ?

460

18.

. TypeName S e l e c t i o n :
TypeName(Selection) = "Chart"
True, .
, True. , , ,
Chart. , Series, Chart T i t l e ,
Legend, PlotArea ..
C h a r t l s S e l e c t e d , , True, .
False:
Private Function ChartlsSelected() As Boolean
ChartlsSelected = Not ActiveChart Is Nothing
End Function
ActiveChart Nothing.
, .

ChartObject
ChartObject ,
D e l e t e c h a r t O b j e c t s :
ActiveSheet.ChartObjects.Delete
,
ActiveWorkbook.Charts.Delete
Excel , . 18.3. , . , :
Application.DisplayAlerts = False
ActiveWorkbook.Charts-Delete
Application.DisplayAlerts = True

. 18.3.
,


:
Sub ChartModslO
With ActiveChart
.Type = xlArea
.ChartArea.Font.Name = "Arial"
.ChartArea.Font.Fontstyle = "Regular"
.ChartArea.Font.Size = 9

V.

4f>1

.PlotArea.Interior.ColorIndex xlNone
.Axes(xlValue).TickLabels.Font.Bold = True
.Axes(xlCategory).TickLabels.Font.Bold = True
.HasLegend = True
.Legend.Position = xlBottom
End With
End Sub
, . ,
HasLegend True. , P o s i t i o n Legend, .
ChartMods.
, ChartObj e c t
1 1. ,
:
Sub ChartMods2()
With Sheets{"1").ChartObjects("!").Chart
.Type * xlArea
.ChartArea.Font.Name = "Arial"
.ChartArea.Font.FontStyle = "Regular"
.ChartArea.Font.Size = 9
.PlotArea.Interior.ColorIndex = xlNone
.Axes(xlValue).TickLabels.Font.Bold = True
.Axes(xlCategory).TickLabels.Font.Bold = True
.HasLegend = True
.Legend.Position = xlBottom
End With
End Sub


. .
For Next
ChartObj e c t s . ChartType c h a r t .
xlArea.
VBA, ,
.
Sub ChangeChartType()
Dim chtobj as ChartObject
For Each chtobj In ActiveSheet.ChartObjects
chtobj.Chart.ChartType = xlArea
Next chtobj
End Sub
, ,
.
Sub ChangeChartType2()
Dim cht as Chart
For Each cht In ActiveWorkbock.Charts
cht.ChartType = xlArea
462

18.

Next cht
End Sub
. ChartObj e c t For Next.
Sub LegendModO
Dim chtobj as ChartObject
For Each chtobj In ActiveSheet.ChartObjects
With chtobj.Chart.Legend.Font
.Name - "Arial"
.FontStyle = "Bold"
.Size a 12
End With
Next chtobj
End Sub


ChartObject
ChartObject . ,
VBA. ChartObject 1,
ChartObject !..
, ChartObject ,, .
Sub

ResizeAndArrangeChartObjects()
Dim W As Double, H As Double
Dim TopPos As Double
Dim chtobj As ChartObject

W = ActiveSheet.ChartObjects("!").Width
H = ActiveSheet.ChartObjects ( "!.") .Height
TopPos = 0
For Each chtobj In ActiveSheet.ChartObjects
With chtobj
.Width = W
.Height = H
.Left = 0
.Top = TopPos
End With
TopPos = TopPos + H
Next chtobj
End Sub

Excel 2000 ( Pivotcharc).
,
( pivotTable). p i v o t c h a r t
(PivotTable). ,
. . .
(', ).

V.

463

^,
.

Microsoft Excel, Excel, VBA.


, pivotLayout chart.

. , , VBA.
T o p P o s .
( )
( C h a r t O b j e c t ) .

. ,
VBA , .


. ,
, (SERIES). "
".

, VBA. :
= (; ! ! $$1: $$ ,-! ! $$1: $$ ; 1)

464

18.


, ,
.
. "" :
. .
:
=(; _; ;, )
- , { ).
,
;
<~ _ ,
( ). , Excel , 1.
-> , .
> , ( , ).
. :
=(!?$1;;!$$2;$$7;1)
. ,
, .
2: 5: 7.
=(;j (1!$$2:$$3;1!$55:$$7);1)
. , Excel
, .
=(1!$$1;jbudget.xls(MyData;1)
(, C a t e g o r i e s Data), , . :
=(;!Categories;!Data;l)
, , VBA
, .
, "" Data:
Range("Bl:B12").Name = "Data"
.
Resize Range . , Data
, :
With Range("Data")
.Resize(.Rows.Count + 1, 1).Name = "Data"
End With

V.

45

,

, , VBA
, .

, 18.4.


. 18.4 , .
.
W o r k s h e e t .
S e l e c t i o n C h a n g e , , . (
! ) .
Private Sub worksheet_Selectionch.a.nge(ByVal Target _
As Excel.Range)
Call UpdateChart
End Sub
, , ,
W o r k s h e e t _ S e l e c t i o n C h a n g e . U p d a t e C h a r t :
Sub UpdateChart()
Dim TheChartObj As ChartObject
Dim TheChart As Chart
Dim UserRow As Long
Dim CatTitles As Range
Dim SrcRange As Range

465

18.

Dim SourceData As Range


If Sheets(1").CheckBoxl Then
Set TheChartObj = ActiveSheet.ChartObjects(1)
Set TheChart = TheChartObj.Chart
UserRow = ActiveCell.Row
If UserRow < 3 Or IsEmptyfCells(UserRow, 1)) Then
TheChartObj.Visible = False
Else
Set CatTitles = Range("A2:F2)
Set SrcRange = Range(Cells(UserRow, 1 ) , Cells{UserRow, 6))
Set SourceData = Union(CatTitles, SrcRange)
TheChart.SetSourceData _
Source:=SourceData, PlotBy:=xlRows
TheChartObj.Visible = True
End If
End If
End Sub

. , . UserRow . If
, ( 3). , , C h a r t O b j e c t . Range ( C a t T i t l e ) ,
, Range ( SrcRange),
. R a n g e VBA U n i o n Range S o u r c e D a t a . , S o u r c e D a t a
S e t S o u r c e D a t a C h a r t .


ComboBox
ComboBox
.
(. 18.5).
ComboBox, , (
). , Excel ActiveX.
Web- .

D r o p D o w n l _ C h a n g e ComboBox.
ComboBox, .
Sub DropDownl_Change()
L i s t l n d e x = Charts(1).DropDovms(1}.Value
Call TjpdateChart (Listlndex)

End Sub

V.

467

. 18.5. ComboBox

U p d a t e C h a r t ,
, . U p d a t e C h a r t ( U p d a t e C h a r t ).
Sub UpdateChart{Item)
1

Dim TheChart As Chart
Dim Datasheet As Worksheet
Dim CatTitles As Range, SrcRange As Range
Dim SourceData As Range
Set TheChart = Sheets("!")
Set Datasheet = Sheets("1")
With Datasheet
Set CatTitles = .Range("A2:F2")
Set SrcRange = .Range(.Cells(Item + 2, 1 ) , _
.Cells(Item + 2 , 6))
End With
Set SourceData = Union(CatTitles, SrcRange)
With TheChart
.SetSourceData Source:=SourceData, PlotBy:=xlRows
.ChartTitle.Left = TheChart.ChartArea.Left
.Deselect
End With
End Sub

:

, , ,
. 18.6. .
VBA,

468

18.

. VBA, ""
, .
, VBA
.

. 18.6. )
?

Series
, Excel : ,
. .
S e r i e s Chart. S e r i e s C o l l e c t i o n S e r i e s Chart.
, .
s e r i e s . ,
S e r i e s :
Set MySeries = A c t i v e C h a r t . S e r i e s C o l l e c t i o n ( l )
S e r i e s ,
.
Formula. S e r i e s . , .
Formula .
Values. .
,
.
XValues. X . XValues
, .
, , , ,

V.

469

V a l u e s S e r i e s . XValues ,
X ( ). , .
XValues V a l u e (.. ).


, V a l u e
, , . 18.7. 1:
V a l u e s ( XValues ).
VBA .
Sub

Test 10
Dim DataRange As Range
Set DataRange = ActiveSheet.Range("A1:A2")
ActiveSheet.ChartObjects(l). _
Chart.SeriesCollection(l).Values = DataRange
End Sub

Puc. IS.7. ,
, Values
XValues ,
1: 2.
,
.
.
Sub T e s t 2 ( )
Dim DataRange As Range
Set DataRange = Sheets("!").ChartObjects(1). _
Chart.SeriesCollection(1).Values
End Sub
Range, DataRange. .
: .
, V a l u e s S e r i e s ( S e r i e s C o l l e c t i o n )
Range . .

470

18.

, VBA isArray ,
:
MsgBox IsArray(Sheets("1"),ChartObjects. _
Chart.SeriesCollection(l).Values)
, Values . ,
Range S e r i e s .
:
Sub Test3()
Dim ChartData As Variant
ChartData = Sheets("1") .chartObjects(1) . _
Chart.SeriesCollection(1).Values
For i = 1 To UBound(ChartData}
MsgBox ChartData(i)
Next i
End Sub
ChartData . For
Next .
Values Series ,
. , ,
.


, Excel ,
VBA . . VBA, Range
S e r i e s .
, .
, GetChartRange, .
Cht. Chart.
s e r i e s . , S e r i e s
SeriesCollection,
ValsOrX. , v a l u e s xvalues ( ).
GetChartRange.
Set MyChart = ActiveSheet.ChartObjects(1).Chart
Set DataRange = GetChartRange(MyChart, 1, "values")
MsgBox DataRange.Address
. Range, GetChartRange.
DataRange Range , S e r i e s . .

V.

471

GetChartRange ,
, . GetChartRange
: , . ,
. GetChartRange , . , .
.
SERIES,
Excel. , , ( Excel).
. . .
GetChartRange Web- . , (, 18.11).

. 18.S. GetChartRange ,

GetChartRange 18.1.

18.1.
Function GetChartRange(cht, series, ValOrX) As Range
'
cht: Chart
'
series: , Series
'
ValOrX: , "values" "xvalues"
Dim Sf As String

472

18.

Dim
Dim
Dim
Pim
Dim

CommaCnt As Integer
Commas() As Integer
ListSep As String * 1
Temp As String
i As Integer

Set GetChartRange = Nothing


On Error Resume Next
'

SERIES
Sf = cht.SeriesCollection(series).Formula



CommaCnt =
ListSep = Chr(44)
For i = 1 Len(Sf)
If Mid{Sf, i, 1) = ListSep Then
CommaCnt = CommaCnt + 1
ReDim preserve Commas{CommaCnt)
Commas(CommaCnt) = i
End If
Next i
If CommaCnt > 3 Then Exit Function

XValues Values?
Select Case UCase(ValOrX)
Case "XVALUES"
1- and 2-
Temp = Mid(Sf, Commas(l) + 1, Commas(2) - Commas(l) - 1)
Set GetChartRange = Range(Temp)
Case "VALUES"
1
2- and 3-
Temp = MidfSf, Commas(2) + 1, Commas(3) - Commas(2) - 1)
Set GetChartRange = Range(Temp)
End Select
End Function



GetChartRange, , , , , . , C h a r t S e r i e s . GetChartRange ,
(SERIES). C h a r t S e r i e s ,
: , SERIES, .

ChartSeries
C h a r t S e r i e s , C h a r t S e r i e s VBA
.

Veen V.

473

Chart (/);
C h a r t S e r i e s (/);
SeriesName (/);
XValues (/);
Values (/);
PlotOrder (/);
SeriesNameType ( );
XValuesType ( );
ValuesType ( );
PlotOrderType ( ).
C h a r t S e r i e s . ,
.
, .
ChartSeries ,
. ,
ChartSeries, Web- . , .
, . .

ChartSeries
, C h a r t S e r i e s , ,
. , ,
C h a r t S e r i e s . ,
ChartObject.
Sub ChartSeriesDemof)
Dim MySeries As New ChartSeries
MySeries.Chart = Sheets(1).chartObjects(1).Chart
MySeries.ChartSeries = 1
If MySeries.ValuesType = "Range" Then
MsgBox MySeries.Values.Address
Else
MsgBox MySeries.Values
End If
End Sub
MySeries, ChartSeries. Chart.
(1) C h a r t S e r i e s .
S e l e c t Case ValuesType. , .
Range, Array. ,
. , . . 18.9
ChartSeriesDemo.

474

18.

. 18.9. ,

ChartSeries
. 18.10 , "". , ,
,
. C h a r t S e r i e s .

. 18.10. . 6.

V.

475

, .
Private Sub ConnnandButtonl_Click ()
'

Dim TheChart As Chart
Dim MySeries A3 New ChartSeries
Set TheChart = ActiveSheet.ChaxtObjects(1).Chart
With MySeries
'
.Chart = TheChart
'
.ChartSeries = 1
'
If .XValuesType = "Range" Then
'
numrows = .XValues.Rows.Count
If numrows <> 1 Then _
Set MewRange .XValues.Resize(numrows - 1, 1)
If numrows > 1 Then .XValues = NewRange
End If
1

If .ValuesType = "Range" Then
'
numrows = .Values.Rows.Count
If numrows 1 Then
Set NewRange = .Values.Resize(numrows - 1, 1)
If numrows > 1 Then .Values * NewRange
End If
End With
End Sub

C h a r t S e r i e s .
XValues ( R a n g e ) R e s i z e R a n g e . ,
, X V a l u e s . V a l u e s C h a r t S e r i e s .
. , X V a l u e s V a l u e s ,
.
29
.



Excel. , , . 18.11.
.
, Excel,
( ).
, , , .

476

18.

. 18. .
18.2 , .
P o i n t s , T e x t , .

18.2.
Sub DataLabelsFromRange()
Dim DLRange As Range
Dim Cht As Chart
Dim i As Integer, Pts As Integer
1


Set Cht = ActiveSheet-ChartObjecCs(1).Chart


On Error Resume Next
Set DLRange = Application.InputBox _
(prompt:=" ". Type:=8)
If DLRange Is Nothing Then Exit Sub
On Error GoTo 0


Cht.SeriesCollection{l).ApplyDataLabels _
Type -. ^xlDataLabelsShowValue, _
AutoText:=True, _
LegendKey:=False


Pts = Cht.SeriesCollection(l).Points,Count
For i = 1 To Pts
Cht.SeriesCollection(l). _
Points(i).DataLabel.Text = DLRange(i)
Next i
End Sub

V.

477

Web- .

. 18.12 D a t a L a b e l s F r o m R a n g e
2 : 9.

. 18.12-
( VBA)
. ,
. Series, Power Utility Pak .



J5
. GIF, Image, U s e r F o r m .
, , .
: " " (. 18.13).
U s e r F o r m .
Image C o m m a n d B u t t o n ( ). , , ,
:
Sub S h o w C h a r t ( )
Dim UserRow As Long
Us.erRow = ActiveCell .Row

478

18.

If UserRow < 2 Or IsEmpty(Cells(UserRow, 1)) Then


MsgEox " .
Exit Sub
End If
CreateChart (UserRowJ
UserForml.Show
End Sub

. 18.13. " "



,
, . , S h o w C h a r t C r e a t e C h a r t . .
C r e a t e C h a r t , 18.3, ,
. , .

18.3.
Sub CreateChart(r)
Dim TempChart As Chart
Dim CatTitles As Range
Dim SrcRange As Range, SourceData As Range
Application.Screenupdating = False
Set CatTitles

= ActiveSheet.Range '2:F2")

V.

47

Set SrcRange = ActiveSheet.Range(Cells{r, 1 ) , Cells(r, 6))


Set SourceData = UnionfCatTitles, SrcRange)
1


Set TempChart = Charts.Add

'


With TempChart
.ChartType = xlColumnClustered
.SetSourceData Source:=SourceData, PlotBy:=xlRows
.HasLegend = False
-ApplyDataLabels Type:=xlDataLabelsShowValue, LegendKey:=False
.ChartTitle.Font.Size = 14
-ChartTitle.Font.Bold = True
.Axes(xlValue) .MaximumScale = 0.6
.Axes(xlCategory).TickLabels.Font.Size = 10
-Axes(xlCategory).TickLabels.Orientation = xlHorizontal
.Location Where:=xlLocationAsObj ect, Name:="1"
End With

'


With ActiveSheet.ChartObjects(1)
.Width =3 00
Height = 150
End With
End Sub
C r e a t e C h a r t
ChartObj e c t .
ChartObject ScreenUpdating Applicat i o n .
ShowChart UserForm.
U s e r F o r m _ I n i t i a l i z e . GIF, ChartObj e c t GIF
Image.
Private Sub UserForm_Initialize()
Dim CurrentChart As Chart
Dim Fname As String
Set CurrentChart = ActiveSheet.ChartObjects(1).Chart
'

GIF
Fname = ThisWorkbook.Path & Application.PathSeparator & "temp.gif"
CurrentChart.Export FileName:=Fname, FilterName:="GIF"
ActiveSheet.ChartObjGets(1).Delete


Imagel.Picture = LoadPicture(Fname)
Application.ScreenUpdating = True
Kill ThisWorkbook.Path & Application.PathSeparator & "temp.gif"
End Sub
Web- .

480

18.


Excel , . ,
, A c t i v a t e . C a l c u l a t e
, , VBA, .
19 .

. 18.1 , Excel 97
.

i 18.1. ,

Activate

B e f o r e D o u b i e c i i c k .
,
BeforeRightciick

calculate

. ,

Deactivate

DragOver

Dragpiot

MouseDown

MouseMove

Mouseup

Resize

Select

SeriesChange

Chart
, , VBA C h a r t . , C h a r t .
C h a r t Object
Procedure, (. 18.14).
, ,
, . , , , .
" ".

V.

481

/'. V3, /4.


, , : ,
. , .
C h a r t _ A c t i v a t e , .
C h a r t _ D e a c t i v a t e , .
C h a r t _ S e l e c t , .
Chart_Activate.
Private Sub Chart__Activate()
Dim msg As String
msg = ", " & Application.UserName & vbCrLf & vbCrLf
msg = msg &. " "
msg = msg & " 6 3 ." & vbCrLf & vbCrLf
msg = msg & " ."
MsgBox msg, vblnformation, ActiveWorkbooK.Name
End Sub
(. 18.15).
Chart_Deactivate. , , , .
Private Sub Chart_Activate{)
Dim msg As String
msg = "! "
msg = msg & " "
msg = msg & " 6 3 ." & vbCrLf & vbCrLf
msg = msg & " - ."
MsgBox msg, vblnformation, ActiveWorkbook.Name
End Sub
Chart_Select, , , .
Private Sub Chart_Select(ByVal ElementID As Long, _
ByVal Argl As Long, ByVal Arg2 As Long)
Dim Id As String
Select Case ElementID
Case xlChartArea: Id = " "
Case xlChartTitle: Id = " "

482

18.

Case xlPlotArea: Id = " "


Case xlLegend: Id = ""
Case xlFloor: Id = ""
Case xlWalls.- Id = ""
Case xlCorners: Id = ""
Case xlDataTable: Id = " "
Case xlSeries: Id = ""
Case xlDataLabel: Id = " "
Case xlTrendline: Id = ""
Case xlErrorBars: Id = ""
Case xlXErrorBars: Id = " "
Case xlYErrorBars: Id = " "
Case xlLegendEntry: Id = " "
Case xlLegendKey: Id = " "
Case xlAxis: Id = ""
Case xlMajorGridlines: Id = " "
Case xlMinorGridlines: Id = " "
Case xlAxisTitle: Id = " "
Case xlShape: Id = ""
Case xlNothing: Id = ""
Case Else: Id = " "
End Select
MsgBox ": " & Id
End Sub

. 18.15. Chart_Activate
, . S e l e c t E l e m e n t l D , . A r g l Arg2 (
). S e l e c t C a s e .

V.

483


, Chart , , . , , .


VBE Project si Insert^Class Module.
. , Properties.

Chart
, . Chart
WithEvents. withEvents,
. :
Public WithEvents myChartClass As Chart


, , .
C l a s s l ( ).
, VBA ( ).
:
Dim MyChart As New MyChartClass
, . :
Set myChart.myChartClass = ActiveSheet-ChartObjects(1).Chart
myChartClass
. ,
.


,
. , :
Public WithEvents myChartClass As Chart
WithEvents
Object . Object Procedure (. 18.16).
, .
, "" Chart (
ChartObject).

484

18.

Puc. 18.16. Procedure CharC


Web- , , .

Chart

, , , .
. 18.17 , .
.
,
.

. IS. 17. ,

V.

485

. .
, . Bl i B4
, .
, ,
.
, EmbChartClass, a
VBA Modulel.
: EnableChartEvents, DiaableChartEvents ( Modulel). ,
, ReturntoMain,
.
Modulel .
Dim SummaryChart As New EmbchartCILass
Sub EnableChartEvents{)
1

Range("Al").Select
Set SummaryChart.myChartClass = _
Worksheets(1).ChartObjectsU).Chart
End Sub
Sub
DisableChartEvents
1

Set SurranaryChart.myChartClass = Nothing
Range("Al").Select
End Sub
Sub ReturnToMain()
1

Sheets("").Activate
End Sub
SummaryChart EmbChartClass
( ). ,
SummaryChart, , ,
. 18.4 EmbChartClass.
MouseDown, myChartClass_JtfouseDown.
GetChartElement , . GetChartElement , X Y ( myChartclass_MouseDown).

18.4.
Public WithEventa myChartClass As Chart
Private Sub myChartClass_MouseDown{ByVal Button As Long, _
ByVal Shift As Long, ByVal X As Long, ByVal As Long)
Dim IDnum As Long

486

18.

Dim a As Long, b As Long


'
1


IDNum, b
myChartClass.GetChartElement X, Y, IDnum, a, b

?
If IDnum s xlSeriea Then
Select Case b
Case 1
Sheets("").Activate
Case 2
Sheets("").Activate
Case 3
Sheets ("") -Activate
End Select
End If
Range("Al").Select
End Sub
Web- .


, . .
.


. (
), . , .
Sub PrintEinbeddedCharts ()
For Each chtObj In AcCiveSheet.ChartObjects
chtObj.Chart.Print
Next chtObj
End Sub

""
, Excel , .
. ""
"" ( ). , "-", , .

V.

487

. ^. <Shift> ^ ( ,
<Shift>). .

. , . <F9>,
. .

x l 8 g a l r y . x l s . , Excel
. , 20 .
"" , .
"" VBA,
Values xvalues Series .



. 18.18 , 365
. , . , ,
Excel .

fuc. /S./A

488

18.

, . Excel , . . 1 * ,
. . .
, , . ,
. 18.19, , , .
. 18.20.
Web- .

/. 18.19.


. 18.20.

, .
, ^,
.
,
. ^ ,

V.

489



Excel,
, , .
, . Excel .
,
Excel .
( ), , .
. ,
<F11>.
, => . . 18.21 , .

. 18.21.

Web- .

490

18.


Excel .
. , , .
, .
, , (
) ..
(. 18.22).

. 18.22.
.
.
1. , .
2.

3.

4. <Shift> ^ ,
.
5. , , .
.
.
,
.
V.

4&1

Web- .


Excel ( ). , . , . 18.23.

. 18.23. VBA

X ( ) , Al.
Al.
, SIN
. . Al,
X Y.
Web- , , .


, ,
, ( ).
, . 18.24, .

492

18.

, ,
.
"".

Puc. 18.24.
Web- .

. X Y
, . S c r o l l b a r
,
. B l , B2 .
ActiveX.
, , .
( ).
. 1 250 .
Sub R a n d o m _ C l i c k ( )
Randomize
Range("Bl") = Int{Rnd * 250)
Range("B2") = Int{Rnd * 250)
Range("B3") = Int(Rnd * 250)
End Sub

V.

493

""
, 18.25 , , .
, , , . , , .
Web- .

, ,
, . 18,26. , , .

. 38.25. ,

. 18.26, , ,

, Web-,
.
ChartObject ClockChart DigitalClock
.
. (StartClock StopClock).
Checkbox () ( cbClockType) , . cbClockType_Click, v i s i b l e ClockChart.
, .

494

18.

S e r i e s . , ,
, 12 .
U p d a t e C l o c k .
.
U p d a t e C l o c k OnTime A p p l i c a t i o n . . U p d a t e C l o c k OnTime,
. , U p d a t e C l o c k U p d a t e C l o c k .
, . VBA V a l u e s
X V a l u e s S e r i e s .


, . (
ActiveX).
Web- .

. 1S.27.

V.

, paiMep "", S p i n n e r .
( ), (. 18.27).
, : X Y.
(XRange YRange), SERIES .
. .
"" .

-
VBA, .
, .
.

496

18.


Excel

VBA,
.
,
. C a m m a n d B u t t o n l _ C l i c k ,
,
CommandButton, .
Excel
. Excel . , Excel:

;
;

Excel
, , ,
. ,
, Excel
.

Excel
Excel , . .
Workbook . Open ( ), BeforeSave ( ) NewSheet ( ).
Worksheet . Change (
), SelectionChange ( ) C a l c u l a t e {
).
Chart .
S e l e c t ( ) SeriesChange ( ). , 18.
Application Excel. NewWorkbook ( ), WorkbookBeforeclose
( ) sheetChange (
). A p p l i c a t i o n .
UserForm.
UserForm . ,
UserForm I n i t i a l i z e ( UserForm). CommandButton, UserForm, Click (
).
, .
( Application), "On-":
OnTime OnKey. , .
.
, .

,
,
.


. , A p p l i c a t i o n .
WorkbookNewSheet: .
S h e e t D e a c t i v a t e : , .
Sheet A c t i v a t e : , .

498

19. Excel

, .
A p p l i c a t i o n . { Workbook
Worksheet).
, .
. ,
( " A p p l i c a t i o n " ) .


VBA- ,
.
, .
VBE
Projects. , . 19.1.
:
(, 1, 2 ..);
(.. );

3TaKHwra(ThisWorkbook);

VBA : ( );

,
. , ,
, WorkbookSetup, - jgjt VBA .
VBA Private sub Workbook_open ()
Call WorkbookSetup
End Sub

Projects


.
, VBA:
Application.EnableEvencs = False

Appli c a t ion.EnableEven s = True
, ?
.

V.

,
UserForm. Click,
CommandButton UserForm.
, Al ,
12. ,
,
. Change Worksheet, , Worksheet_Change. , 12, ,
. ,
VBA Change,
.
, . ,
.
( S t a t i c B o o l e a n ) . :
S t a t i c AbortProc As Boolean
Excel
Excel 97 ,
,
, Auto_open, VBA
, . Excel 97, Auto_Open Workbook_open,
. Auto_open.
Excel 97 . , , , :
Sheets ("1") .OnEntry = "ValidciteEntry"
Excel, ValidateEntry , . Excel 97 ,
Worksheet_change 1.
Excel 97 { ).
, Excel 97
, , .
, , A b o r t P r o c True ( ,
F a l s e ) . .
If

AbortProc Then
AbortProc = F a l s e
E x i t Sub
End i f

500

19. Excel

, T r u e A b o r t P r o c .
, A b o r t P r o c F a l s e .

, " " .
Excel
. , , workbook_Open,
.


. :
Worksheet_SeltionChange
Workbook_Open
Chart_Activate
Class_Initialize
,
VBE.
. 19.2 . , Workbook . . "" , End Sub.

. 19.2. VBE
Haiiptrivf, Workbook, Open,
VBE () ;
P r i v a t e Sub VJorkbook_Open {)
End Sub
.

V.

501

,

. ,
S h e e t A c t i v a t e
. , , VBE :
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
End Sub
(Sh),
. Sh O b j e c t , W o r k s h e e t .
, .
, , .
.
TypeName
Name , .
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
MsgBox TypeName(Sh) & vbCrLf & Sh.Name
End Sub
C a n c e l
B o o l e a n . , B e f o r e P r i n t
:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
C a n c e l , , F a l s e . T r u e , . .
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Msg = " ?"
Ans = MsgBox{Msg, vbYesNo, " ...")
If Ans = vbNo Then Cancel = True
End Sub
W o r k b o o k _ B e f o r e P r i n t ,
. , . 19.3. , C a n c e l T r u e ,
.
B e f o r e P r i n t .

. 19.3.
,
Cancel

502

19. Excel

, Excel Bef o r e P r i n t . , , .

Workbook
Workbook .
. 19.1 , . Workbook
ThisWorkbook

().

19.1. Workbook

Activate

Addinlnstall

AddinUninstall

BeforeClose

BeforePrint

BeforeSave

Deactivate

NewSheet

Open

PivotTableCloseConnection*

PivotTableOpenConnection*

SheetActivate

SheetBeforeDoubleClick

SheetBeforeEightClick

SheetCalculate

( )

SheetChange

SheetDeactivate

SheetFollowHyperlink

SheetPivotTableUpdate

SheetSelectionChange

WindowActivate

WindowDeactivate

WindowResize

" Excel 2002 .

V.

503

, ,
A p p l i c a t i o n (. "
A p p l i c a t i o n " ). workbook. ,
,, . , .

Open
Open .
( ). Workbook_Open. Workbook_Open , :

;
(,
);
(, . " " );

S c r o l l A r e a (
);

U s e r I n t e r f a c e O n l y ,
. P r o t e c t

<Shift>
, Workbook_ppen .

Workbook_Open. Weekday . ,
, . ,
.
P r i v a t e Sub Workbook_Open{)
If Weekday(Now) = vbFriday Then
Msg = " . "
Msg = Msg & " ?"
MsgBox Msg, vblnformation
End If
End Sub

Activate
, :

504

19. Excel

Private Sub Workbook_Activate()


ActiveWindow.WindowState = xlMaximized
End Sub

SheetActivate
, . ,
1. , .
VBA TypeName , , ( ).
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If TypeName(Sh) = "Worksheet" Then _
Range("Al").Select
End Sub
.
P r i v a t e Sub W o r k b o o k _ S h e e t A c t i v a t e { B y V a l Sh As O b j e c t )
On Error Resume Next
Range("Al").Select
End Sub

NewSheet
,
. . , ,
. ,
1.
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If TypeName(Sh) = "Worksheet" Then _
Range("Al") = " " & Now ()
End Sub

BeforeSave
B e f o r e S a v e .
, ^
. ,
" ".
Workbook_Bef o r e S a v e
(SaveAsUI),
. .
Private Sub Workbook_BeforeSave __
{ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox " OK ."
End If
End Sub

V.

505

, W o r k b o o k _
B e f o r e S a v e .
, S a v e A s U l T r u e . SaveAsUl,
, .
C a n c e l T r u e .

Deactivate
D e a c t i v a t e .
. . D e a c t i v a t e .
Private Sub Workbook_Deactivate()
Me.Windows(1).Activate
MsgBox " , !"
End Sub
.
"" Excel,
. .
. , ,
, . D e a c t i v a t e
:
;

..

, , .
, ,
. , .

BeforePrint
B e f o r e P r i n t ,
( ) .
. C a n c e l ,
T r u e . ,
, B e o r e P r i n t :
.
Excel 2002 , . Excel 2002 . (
) .

506

19. Excel

Excel,
, .
B e f o r e P r i n t .
Workbook_BeforePrint, , .
P r i v a t e Sub Workbook_BeforePrint(Cancel As Boolean)
For Each sht In ThisWorkbaok.Sheets
sht.PageSetup.LeftFooter =
"&8" & ThisWorkbook.FullName
Next sht
End Sub
LeftFooter PageSetup FileName (
, ). , 8 .
Excel.
,
. , ,
"&8" 8 .
Font.
, (
, ).
BeforePrint { ),
.

BeforeClose
B e f o r e C l o s e .
Workbook_Open. ,
Workbook_open ,
, W o r k b o o k _ B e f o r e C l o s e
.
.
, Excel (. 19.4).

. 19.4- ,
, Workhook_BeoreClose

, : BeforeClose , workbook_BeforeCios(j.

V.

507

: . W o r k b o o k _ O p e n , Workbook_Bef o r e C l o s e .
. , .
P r i v a t e Sub Workbook_Open()
Call CreateMenu

End Sub

P r i v a t e Sub Workbook_BeforeClose(Cancel As Boolean)


Call DeleteMenu

End Sub

, Excel . . . , Workbook_Bef o r e C l o s e . ,
, , !

W o r k b o o k _ B e f o r e C l o s e . ,
.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Msg As String
If Me.Saved Then
Call DeleteMenu
Exit Sub
Else
Msg = " ?"
Msg = Msg & Me.Name & "?"
Ans = MsgBox{Msg, vbQuestion + vbVesNoCancel)
Select Case Ans
Case vbYes
Me.Save
Call DeleteMenu
Case vbNo
Me.Saved = True
Call DeleteMenu
Case vbCancel
Cancel = True
End Select
End If
End Sub
S a v e d Workbook, . , D e l e t e M e n u , .
, ,
Excel. , , , . , S a v e d Workbook T r u e { ) .
, Bef o r e C l o a e
.

508

19. Excel

Worksheet
W o r k s h e e t . ,
.
, , .
XLM
Excel 5/95.

! 19.2. Worksheet

Activate

BeforeDoubleClick
B e f o r e R i g h t C l i c k
Calculate

{ )

change

Deactivate

FollowHyperlink

P i v o t T a b l e U p d a t e *
SeleclionChange

Excel 2002 .
, .
,
.

Change
Change
. Change , .
W o r k s h e e t _ C h a n g e T a r g e t
Range. , . ,
, T a r g e t .
P r i v a t e Sub Worksheet_Change(ByVal T a r g e t A s E x c e l . R a n g e )
MsgBox " " & T a r g e t - A d d r e s s & " . "
End Sub
, C h a n g e ,

V.

509

W o r k s h e e t . Excel , .
C h a n g e , .
:
, , ,
, , Change!
(] Change, ^ , , .
, C h a n g e .
<Del> C h a n g e ,
.
, Excel, ,
c h a n g e . , ^ 1 *
Change. *
^ .

VBA ,
Change.

, , Change
.
, change Excel. 60 Excel
2002 ^ change.
^, .


C h a n g e .
,
. W o r k s h e e t _ C h a n g e ,
Range.
, C h a n g e . , I n p u t R a n g e ,
, . C h a n g e
Range,
Worksheet_Change:
P r i v a t e Sub Worksheet__Change (ByVal Target As Excel.Range)
Dim VRange As Range
Set VRange = Range("InputRange")
If Not I n t e r s e c t f T a r g e t , VRange) Is Nothing Then _
MsgBox " ."
End Sub
Range, VRange. , . VBA I n t e r s e c t , T a r g e t ( ) VRange.

510

19. Excel

I n t e r s e c t , , . I n t e r s e c t Nothing, . Not ,
True ,
VRange. . , .


,
( Change). CheckBox, , . . 19.5 ,
.

. 19.5. Worksheet_Change
Web- .

Private Sub Worksheet_Change(ByVal Target As Excel.Range)


Dim cell As Range
Dim OldText As String, NewText As String
If CheckBoxl Then
For Each cell In Target
With cell
On Error Resume Next
OldText = .Comment.Text
If Err <> 0 Then .AddComment
NewText = OldText & " " & cell.Text & _
". " & Application.UserName & ", " & Now & vbLf
.Comment.Text NewText
.Comment.Visible = True
.Comment.Shape.Select

V.

511

Selection.AutoSize = True
.Comment.Visible = False
End with
Next cell
End If
End Sub
, Worksheet_Change, , T a r g e t .
, . { ).
. , Excel ^. ,
.


Excel ,
. ,
, , , , ! ,
.
C h a n g e W o r k s h e e t .
Web- .
EnableEvents
Change. (
" " ).
19.1 , . ,
I n p u t R a n g e . 1 12.

19.1.
Private
Dim
Dim
Dim
Set
For

572

Sub Worksheet_Change{ByVal Target As Excel.Range)


VRange As Range, cell As Range
Msg As String
ValidateCode As Variant
VRange = Range("InputRange")
Each cell In Target
If Union{cell, VRange).Address = VRange.Address Then
ValidateCode = EntrylsValid(cell)
If ValidateCode = True Then
Exit Sub
Else
Msg = " " & cell.Address(False, False) & " : "
Msg = Msg & vbCrLf & vbCrLf & ValidateCode
MsgBox Msg r vbCritical, " "
Application.EnableEvents = False
cell.ClearContents
cell.Activate
Application.EnableEvents = True

19. Excel

End If
End If
Next cell
End Sub
W o r k s h e e t _ C h a n g e Range. VRange .
T a r g e t , . , * , , ( E n t r y l s V a l i d ) , T r u e .
,
E n t r y l s V a l i d , . (. 19.6). , . , . , C h a n g e
.

. 19.6-
, .
E n t r y l s V a l i d .

19.2.
Private Function EntrylsValid(cell) As Variant
'
True, 1
1
12. ,
'

If Not WorksheetFunction.IsNumber(cell) Then


EntrylsValid = " ."
Exit Function
End If

?
If Clnt(cell) <> cell Then
EntrylsValid = " ."
Exit Function
End If

'

?
If cell < 1 Or cell > 12 Then
EntrylsValid = " 1 12."
Exit Function
End If

'

EntrylsValid = True
End Function

V.

51

SelectionChange
S e l e c t i o n C h a n g e .
, .
Private Sub Worksheet_SelectionChange(ByVal Target _
As Excel.Range)
Cells.Interior.Colorlndex = xlNone
With Activecell
.EntireRow.Interior.Colorlndex = 27
.EntireColumn.Interior.ColorIndex = 27
End With
End Sub
,
. ,
- . . 19.7 (, ).

.. 19.7.
Web- .

,
, .
.

514

19. Excel

BeforeRightClick
, Excel
. , BeforeRightClick. Cancel True,
BeforeRightClick. , .
P r i v a t e Sub Worksheet__BeforeRightClick _
{ByVal Target As Excel.Range, Cancel As Boolean)
Cancel = True
MsgBox " ."
End Sub
24 .

Chart
,
. , .
18 chart.
, 18 .
. 19.3 , , .

19.3. ,

Activate

Bef o r e D o u b i e C l i c k . ,
B e f o r e R i g h t C l i c k .
,
calculate
Deactivate

DragOver
Dragpoint

MouseDown

MouseMove
Mouseup

V.

515

. 19.3

Resize

select

Serieschange

Application
Workbook W o r k s h e e t .
.

, A p p l i c a t i o n .


Object Browser , , . , Object Browser
, . , , MouseMove. VBE <F2>, Object Browser.
, <AII Libraries>, MouseMove
.

Object Browser . , ,
MouseMove. , , , MSForms.

516

19. Excel

UserForm , MouseMove
c h a r t .
: Library, Class Members. , ,
:
.
,
Object Browser, , . ,
-.
A p p l i c a t i o n .

. 19.4 A p p l i c a t i o n

{ 19.4. , Application

Newworkbook

SheetActivate
sheetBeforeDoubleclick

sheetBeforeRightciick

sheetcaicuiate
sheetchange
sheetDeactivate
sheetFollowHyperlink
SheetPivotTableUpdate
SheetSelectionChange
windowActivate
windowDeactivate
windowResize
workbookActivate
workbookAddininstaii
workbookAddinUninstall
workbookBef oreciose
workbookBeforeprint
WorkbookBef oreSave


.
,

.
,
( )





,

V.

517

. 19.4

workbookDeactivate

WorkbookNewSheet

workbookopen

w o r k B o o k P i v o t T a b l e c l o s e c o n n e c t i o n *

WorkbookPivotTableOpenConnection*

Excel 2002 .

Application
A p p l i c a t i o n , .
1. .
2. Properties. VBA , C l a s s l , Class2 .. .
3. A p p l i c a t i o n ,
WithEvents. :
Public WithEvents XL As Application
4. ,
A p p l i c a t i o n . , VBA ( ). :
Dim X As New Classl
5. A p p l i c a t i o n .
Workbook_Open.
Set X.XL = Application
6. .

. 18.


, ,
. , AppClass.
.

518

19. Excel

Public WithEvents AppEvents As Application


Private Sub AppEvents_WorkbookOpen _
(ByVal Wb As Excel.Workbook)
Call UpdateLogFile(Wb)
End Sub
A p p l i c a t i o n , AppEvents . AppEvents_WorkbookOpen
.
UpdateLogFile Wb, .
VBA, .
Sub UpdateLogFile{Wb}
On Error Resume Next
cxt = Wb.FullName
t x t = t x t & " , " & Date & ", & Time
t x t = t x t & " , " & Application.UserName
Fname = ThisWorkbook.Path & " M o g f i l e . t x t "
Open Fname For Append As #1
Write #1, t x t
Close #1
End Sub
AppClass (
). I n i t Workbook_Open, . Workbook_Open .
Private Sub Workbook_Open()
Call Init
End Sub
UpdateLogFile ( ,
) :
, , , .
WorkbookIsOpen I n i t . ,
I n i t .
Web- . , .
, .

Application
"" ,
, .
Web- ,
A p p l i c a t i o n , .
. Excel 2000
UserForm (. 19.8). Excel 97 (Excel 97 UserForm).

V.

519

. 19.8.
Application
20- (
, Application). :
Private Sub XL_NewWorkbook(ByVal Wb As Excel-Workbook)
LogEvent " : " & Wb.Name
End Sub
LogEvent
. LogEvent.
Sub LogEvent(txt)
EventNum = EventNuin + 1
With UserForml
With . 1Events
.AutoSize = False
.Caption = .Caption & vbCrLf & t x t
.Width = UserForml.FrameEvents.Width - 20
.AutoSize = True
End With
.FrameEvents.ScrollHeight = .lblEvents .Height + 20
.FrameEvents.ScrollTop = EventNum * 20
End With
End Sub
LogEvent UserForm, Caption
Label, lblEvents. ,
ScrollHeight ScrollTop Frame,
FrameEvents. Label Frame.

UserForm
U s e r F o r m , , , . . 19.5 UserForm, VBA.

520

19. Excel

19.5. , UserForm

Activate

Addcontroi

BeforeDragOver

Bef o r e D r o p O r P a s t e (.. )
click

Dblclick

Deactivate

initialize

KeyDown

Keypress
KeyUp

Layout
MouseDown

MouseMove

Mouseup

Queryclose
Removecontrol

Resize

scroll
Terminate

zoom


13-15 .

, ,
(Application, Workbook, Sheet ..).
, : OnTime OnKey.
A p p l i c a t i o n .

V.

121

, ,
OnTime ,
VBA .

OnTime
OnTime . Excel , 15:00 .
Sub SetAlarm{ )
Application.OnTime TimeValue("15:00:00 " ) ,
End Sub
DisplayAlarm()
Beep
MsgBox ".
End Sub

"DisplayAlarm"

Sub

!"

S e t Al a r m OnTime
A p p l i c a t i o n OnTime. :
( 3:00 p.m. 15:00) , ( D i s p l a y A l a r m ) . S e t A l a r m D i s p l a y A l a r m 15:00,
, . 19.9.

. 19.9.


(, 20 ), :
Application.OnTime Now + TimeValue("00:20:00"),

"DisplayAlarm"

OnTime , . D i s p l a y A l a r m 1 2002 12:01,


Application.OnTime DateSerial{2002, 4, 1) + _
TimeValue ("00:00:01lt)J
"DisplayAlarm"
OnTime . ,
.
, 18. OnTime
.

522

19. Excel


Excel .
, .
OnKey OnKey.
<PgUp> <PgDn>.
S e t u p _ O n K e y <PgDn> PgDn_Sub, <PgUp> PgUp_Sub.
<PgDn> , <PgUp>
.
Sub Setup_OnKey()
Application. OnKey " {PgDn} ", " PgDn__Sub"
Application.OnKey "{PgUp}",
"PgUp_Sub"
End Sub
Sub PgDn_Sub()
On Error Resume Next
ActiveCell.Offset(1, 0).Activate
End Sub
Sub PgUp_Sub{)
On Error Resume Next
ActiveCell.Offset(-1, 0).Activate
End Sub
, ,
.
. OnKey.
On E r r o r Resume N e x t ,
. ,
, .
, , ,
, .

.
Sub Cancel_OnKey()
Application.
"{PgDn}"
Application. "{PgUp}"
End Sub

. Excel
. ,
Excel,
<Alt+F4> ( <Alt>):
Application. "%{F4}",

""

,
, . 9.

V.

523


, .
VBA, Excel
.

524

19. Excel



,

. (,
)
.

. Windows
, . Windows
DDE (Dynamic Data Exchange
), . ,
Excel . , .



Excel. , DOS.
.
VBA s h e l l
. 20.1 , Windows Character Map ( ), .

20.1. Windo
Sub RunCharMapO
On Error Resume Wext
Program = "Charmap.exe"
TaskID = Shell(Program, 1)
If Err <> 0 Then
MsgBox "
End If
End Sub

& Program, vbCritical, ""

, , . 20.1.
Excel 2002, Windows Character Map ( )
, Excel ^ .

. 20.1. Windows Character


1
Map ( , Excel
S h e l l . . S h e l l
( 1).
S h e l l , . , On E r r o r , .
, VBA , S h e l l . , S h e l l . S h e l l , . (, ),
Excel , .

S h e l l , VBA ,
, VBA. , , , ,

20.

, . 20.2 , , , S h e l l , .

' 20.2.
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, _
ByVal dwProcessld As Long) As Long
Declare Function GetExitCodeProcess Lib "kerne232" _
iByVal hProcess As Long, _
ipExitCode As Long) A3 Long
Sub RunCharMap2()
Dim TaskID As Long
Dim hProc As Long
Dim lExitCode As Long
ACCESS_TYPE = &H400
STILL_ACTIVE = &H103
Program = "Charmap.exe"
On Error Resume Next
1


TaskID = Shell(Program, 1)

'


hProc = OpenProcess {ACCESS__TYPE, False, TaskID)
If Err <? 0 Then
MsgBox "
Exit Sub
End If

& Program, vbCritical, ""

'

GetEacitCodeProcesa hProc, lExitCode

DoEvents
Loop While lExitCode = STILL_ACTIVE
DO

,
MsgBox Program & " "
End Sub
1


GetExitCodeProcess Do Loop.
lExitCode. , lExitCode ,
, VBA .
Web- .

V.

527

Windows S t a r t ,
S h e l l . S t a r t Windows NT. , Windows DOS. S t a r t
( ).
, . , Web- (
. htm) HTML homepage. htm.
WebPage = "c:\web\homepage.htm"
Shell "Start " & WebPage
, :
Email = " m a i l t o : b g a t e s @ m i c r o s o f t . c o m "
S h e l l " S t a r t " & Email
Windows NT (
Windows, S t a r t ) , Windows API
S h e l l E x e c u t e . API (
VBA).
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hWnd As Long, _
ByVal lpoperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal ipDirectory As String, _
ByVal nShowCmd As Long) Aa Long
S h e l l E x e c u t e .
Sub OpenFile()
File = "http://www.microsoft.com"
Call ShellExecute(O&, vbNullString, File, _
vbNullString, vbNullString, vbNonnalFocus)
End Sub
URL . ,
(JPG BMP)
.
, , Microsoft, ActivateMicrosoftApp A p p l i c a t i o n .
, Word.
Sub StartWordU
Application.ActivateMicrosoftApp xlMicrosoftWord
End Sub
Word , .
, , : xlMicrosof tPower Point, x l M i c r o s o f t M a i l , xlMicrosoftAccess, xlMicrosoftFoxPro,
x l M i c r o s o f t P r o j e c t xlMicrosoftSchedulePlus.


: ,
S h e l l . . , .

528

20,

S t a r t C a l c u l a t o r AppActiv a t e , (
Microsoft Calculator ()). AppActiv a t e . AppActivare
, Calculator () . , .
Sub S t a r t C a l c u l a t o r ( }
AppFile = "Calc.exe"
On Error Resume Next
AppActivate "Calculator"
If Err <> 0 Then
Err = 0
CalcTaskID = Shell(AppFile, 1)
If Err <> 0 Then MsgBox " "
End If
End Sub
Web- .



Windows
, .
Excel
. , :
, . 20.2.
S h e l l . , : .

. 20.2. VBA

. 20.3. Excel

V.

529

, 50 ,
. 20.3 ( Web- ).

Excel, Microsoft Word.


, Excel Word . Excel , a Word
. , Visual Basic,
Excel.
OLE-, ActiveX- (, Microsoft
).
. , , , C h a r t . , .
Access .
( Excel ) ,
.
.
VBA , . Microsoft Word,
,
( ).


, Excel ^,
(, Word) . , VBA. (
.) .
, . , , , .
VBA, ,
, .


.
: . , , , .

530

20.



ToolS^References VBE.
, . 20.4.
Excel 2002 Project Explorer , .
Object Browser
( . 20.5) , .

. , Microsoft Word 8.0 Object Library, Microsoft
Word 9.0 Object Library Microsoft Word 10.0 Object Library. :
Dim WordApp As New W o r d , A p p l i c a t i o n

, . , . , ,
.

. 20.4.

, 20.5,. Object Browser

, . , Word ( Excel) ,
VBA. .
, , . Object Browser Auto List Members VBE . , .

V.

531


CreateObject
, GetObject .
Object.
.
,
, . , , Word 97 , Word:
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
Word, . Word 2000:
Set WordApp = CreateObject("Word.Application.9")
Windows Word Automation,
A p p l i c a t i o n VBA : Word. A p p l i c a t i o n .
. As Word. A p p l i c a t i o n
As New Word. Application, A p p l i c a t i o n Word. CreateObject ("Word. A p p l i c a t i o n " ) , , Word Windows. ,
Office XP. Word
2000 Word 2002, CreateObject ("Word.Application") . Word 2002,
CreateObject ("Word. A p p l i c a t i o n . 10 " ) , Word 2002,
.
CreateObj ect, , Word.
Application Excel .Application, , ..
. , , , .
, GetObj ect.
Office,
, . , Word 97, Word 2000
Word 2002, Word 97, .
, Word.
GetObject CreateObject
VBA GetObject createObjed: , -.
CreateObject . , .
, . , Excel XLApp
Excel. A p p l i c a t i o n :
Set XLApp = C r e a t e O b j e c t ( " E x c e l . A p p l i c a t i o n ' )

532

20,

Getobject , ,
, , ,
Excel M y f i l e . x l s .
workbook ( M y f i l e . x l s ) :
Set XLBook = GetObject(":\Myfile.xls")


Word . , , Word ( ).
Sub GetWordVersionl)
Dim WordApp As Object
Set WordApp tt CreateObject {"Word. Application")
MsgBox WordApp.Version
WordApp.Quit
Set WordApp = Nothing
End Sub
word . , word ,
V i s i b l e True :
WordApp.Visible = True
.
Toolso References
Word. .
Sub GetWordVersionf)
Dim WordApp As New Word.Application
MsgBox WordApp.Version
WordApp.Qui t
Set WordApp - Nothing
End Sub

Word Excel
, , Word
Excel. MakeMemos Word, . , , . 20.6.

. 20.6. Word
Excel

V.

533

MakeMemos, 20.3,
WordApp. 1 , Word . M e s s a g e ( 6), ,
.

20.3. Word Excel


Sub MakeMemos{)
1
Word ( )
Dim WordApp As Object
Dim Data As Range, message As String
Dim Records As Integer, i As Integer
Dim Region As String, SalesAmt As String, SalesNum As String
Dim SaveAsName As String
1

Word
Set WordApp = CreateObject("Word.Application")


Set Data = Sheets("1").Range{"Al")
message = Sheets("1").Range("Message")

1
1
Records = Application.CountA(Sheets(" ) .Range("A:A"))
For i = 1 To Records

Application.StatusBar = " " & i

'


Region = Data.Cells(i, 1).value
SalesNum = Data.Cells(i, 2).Value
SalesAmt = Format(Data.Cells{i, 3).Value, "#,000")


SaveAsName = ThisWorkbook.Path & "\" & Region & ".doc"

'

Word
With WordApp
.Documents.Add
With .Selection
.Font.Size = 14
.Font.Bold = True
.ParagraphFormat.Alignment = 1
.TypeText Text:="M "
.TypeParagraph
.TypeParagraph
.Font.Size = 12
.ParagraphFormat.Alignment = 0
-Font.Bold = False
..TypeText Text : = ": " & vbTab & _
Format(Date, "mmmm d, yyyy")
.TypeParagraph
.TypeText Text:=": " & vbTab & Region

534

20.

.TypeParagraph
.TypeText Text:="OT:" & vbTab & _
Application.UserName
.TypeParagraph
.TypeParagraph
.TypeText message
.TypeParagraph
.TypeParagraph
.TypeText Text: =" :" & vbTab & SalesNuin
.TypeParagraph
.TypeText Text:=" :" & vbTab & _
Format(SalesAmt, "$#,## 0 ">
End With
.ActiveDocument.SaveAs FileName:=SaveAaName
End With
Next i
1


WordApp.Quit
Set WordApp = Nothing


Application.StatusBar = ""
MsgBox Records & " " &
ThisWorkbook.Path
End Sub
. 20.7 , MakeMemos.

Fwc, 20.7. Excel


.
Word. , , . Word Word.
Excel. , w i t h - E n d w i t h .

V.

535

With End With . ,


Word ( ) :
Documents.Add
:
With WordApp
.Documen t s.Add
1

End With
, Word, .
, ,
. Immediate VBE Word.

Excel
Excel ( Visual Basic
Word) , Excel,
Word.
Excel .

A p p l i c a t i o n CreateObject ("Excel . A p p l i c a t i o n " ) Workbook CreateObject ("Excel . S h e e t " ) .

Chart CreateObject ("Excel . C h a r t " ) .


20.4 , VBA
Word 2002. Excel Worksheet (
Excel. Sheet) .

20.4. Excel Word 2002


Sub MakeExcelChart()
Dim XLSheet As Object
'


Documents.Add


StartVal = lnputBox(" ")
PctChange = InputBox(" ")

Sheet
Wbook = ThisDocument.Path & "Nprojections.xls"
Set XLSheet = GetObject(Wbook, "Excel.Sheet").ActiveSheet


XLSheet.Range("StartingValue") = StartVal
XLSheet.Range{"PctChange") = PctChange
XLSheet.Calculate


Selection.Font.Size = 14
Selection.Font.Bold = True
Selection.TypeText " : " & _
Format(PctChange, - 0.0%")

536

20.

Selection.TypeParagraph
Selection.TypeParagraph
'


XLSheet.Range("data").Copy
Selection.Paste


XLSheet.Chartobjects(l).Copy
Selection.PasteSpecial _
Link:=False,
DataType:=wdPasteMetafilePicture, _
Placement:=wd!nLine, DisplayAsIcon:=False

'

Set XLSheet = Nothing
End Sub
. 20.8. M a k e E x c e l C h a r t .
_

. 20.8. VBA Word


. Excel . . 20.9.

ADO
ADO (ActiveX Data Object) , , . ,
. . DAO (Data Access Objects).
ADO
Access.
ADO . Excel , . ,
.

V.

537

. 20,9. VfiA Word Excel



Access, budget .mdb. ( Budget).
. , , .
. Recordset. (, 20.10).
Sub ADO_Demo()
1

'
Microsoft ActiveX Data Objects 2.x
Dim
Dim
Dim
Dim
Dim

DBFullName As String
Cnct As String, Src As String
Connection As ADODB.Connection
Recordset As ADODB.Recordset
Col As Integer

Cells.Clear
MsgBox " _
= = . ."
1


DBFullName = ThisWorkbook.Path & "\budget.mdb"


Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "

538

20.

Cnct = Cnct & "Data Source=" & DBFullName & ",- "
Connection.Open ConnectionString:=Cnct
'
1

'

RecordSet
Sec Recordset = New ADODB.Recordset
With Recordset

Src = "SELECT * FROM Budget WHERE = '' "


Src = Src & "and = '. '"
.Open Source:=Src, ActiveConnection:=Connection

For Col = 0 To Recordset.Fields.Count - 1
Range{"Al").Offset(0, Col).Value = Recordset. _
Fields(Col).Name
Next


Range("Al").Offset(1, 0).CopyFromRecordset Recordset
End With
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
End Sub
, Access, Web- ,

/'. 20. 0. Access

V.

539

SendKeys
.
. Excel SendKeys, .
, S e n d K e y s , . ,
.
, , SendKeys, . , S e n d K e y s
.
S e n d K e y s Windows Calculator (), S e n d K e y s T o C a l c u l a t o r
Calculator (),
.
Suto SendKeysToCalculator()
Shell " c a l c . e x e " , vbNormalFocus
AppActivate " C a l c u l a t o r "
SendKeys "60*12=" , True

End Sub

S e n d K e y s , (,
<Alt>).
C e l l T o D i a l e r 20.5 SendKeys.

20.5. Excel
Sub CellToDialer()
Dialer
1

'

.
CellContents = ActiveCell.Value
If CellContents = "" Then
MsgBox " . ."
Exit Sub
End If
( ) Dialer
Appname = "Dialer"
AppFile = "Dialer.exe"
On Error Resume Next
AppAc t iva t e (Appname)
If Err <> 0 Then
Err = 0
TaskID = Shell(AppFile, 1)
If Err <> 0 Then MsgBox " " & AppFile
End If

'

540

Dialer
Application.SendKeys "%n" & CellContents, True

20.

Application.SendKeys "%d"
'
Application.SendKeys "{TAB}-", True
End Sub
Windows Dialer (), . Dialer ,
. S e n d K e y s
Dialer, .

Excel . ,
S e n d K e y s , .

Excel.

. V.

541

Excel,
, .

.
, Excel.


,
. ,
Excel . . , Excel.
(, ) ,
.
, ( ).



Excel XLS. . XLS , .
Excel ,
.

I s A d d i n T h i s W o r k b o o k T r u e .
, . ,
(
).
Workbooks
A d d l n s . Workbooks ( "
" ).
^.
, ,
, , .
.!. . .


, XLS .
. , . ,
, ,

. , , ,
, .
XLS, .

.. , , . , Mewfuncs
MOVAVG, .
=Wewfuncs.xls!MOVAVG{Al:A50)
,
, :
MOVAVG(AX:A50)

. ,
.

.
Excel, ,
.

V.

543


Excel 2000 COM (Component
Object Model), DLL . , Office, .
, . XLA, Excel. Visual Basic 5 ( ) Office Developer Edition.
, .

. ' ?'\

Excel

Excel ,
^. ,
. 21.1. , Excel. , .
.
.
Excel 2002 .
, , Excel
2000 .
Excel 2000 .
?
. , 1^ .
Excel VBA,
. ^ , "".
, . , : Excel
. ,
,
: .
EuroVafue.
, .

. 21.1.

544

21.


, ,
.
"" , , ,
. ,
, ,
. , .
.
.
1. , .
( ),
. , . 23 , 22
.
2. , , .
, (
).
3. VBE Project. ToolS^xxx Properties (^ ),
Protection (). Lock project for viewing ( ) (). .
, .


Microsoft Excel ,
. Excel
. Excel 2002 ,
,
. , - , Excel .
4. Excel ^ .
.
.
, ,
.
5.

^ .

6. Microsoft Excel
.
7. .
* . x l a . .

V.

545

, , . ,
Excel 5/95,
Microsoft Excel. , ,
1* .


{ Text Tools, 16).
XLS- Text Tools Web- , .


, .
, , , .
F o r m H e l p .
. .
FormMain.
. .
VBA, modMenus. ,
(^ ).

VBA, modMain.
, FormMain.

, T h i s W o r k b o o k
(Workbook_Open Workbook_Bef o r e C l o s e ) ,
.
16
Text Tools.


. , , ,
.

546

21.

Text
Tools. , .
,
. , .


-
, .
, . 21.2.
.
.

.

.

. 21.2.
,


, .
1. VBE .
2. Debug "^Compile (11* ).
VBA, .
Excel , .
3. Tools^Text Tools Properties (=^ Text Tools)
Protection () . Lock
project for viewing ( ) (). . , .
VBA V B P r o j e c t . T e x t T o o l s .
4. .
5. ^ . Excel
.
6. Microsoft Excel.
7. . , .

V.

547

Excel
Excel ^,
.
. , , .
VBA T i t l e Addin Addins, , ,
i n s t a l l e d True.
, .
, . , . .
-> XLA: , XLS.
- XLL: DLL, .
( Excel 2002),
. , ,
, Excel.
Addins Add , VBA . ,
, i n s t a l l e d Addin True. False .
Excel
. , , MO J
Excel, Excel.


, XLS , .
,
.
1. ^. Excel .
2. , .

. . 21,3, , .
3. ,
.
Text Tools - 2 1 } .
, cmpo(jKUt S t a r t T e x t T o o l s , .

54

21.


Excel,
XLA ( XLS ). . Office XP Developer Edition,
S e t u p . e x e , .
Text Tools ( ).
,
. , .


, ,
. ,
VBE Project.
. ,
VBE ( F i l e ^ S a v e ( )).
, , i s A d d l n F a l s e .
Excel. Properties
ThisWorkbook.. , I s A d d l n T r u e . F a l s e , ( . x l a ) .

.
,

.
Excel?
VBA ?
VBAProject. .
?
?
VBA, , P r i v a t e ? , .
,
?
? , , , .
?
"
".

V.

549

XLA XLS
, XLA XLS .
,
.


, XLS, , . VBA XLA ,
.


A d d l n s , ""
Workbooks. Workb o o k s , , .
, M y a d d i n . x l a :
Set TestAddin = Workbook{"Myaddin.xla")
Workb o o k s . Workbooks,
M y a d d i n . x l a :
For

Each w in A p p l i c a t i o n . W o r k b o o k s
MsgBox w.Name
Next w
, F o r Next Myaddin.
11
x l a (, , Excel " ) :
For Each a in Application.Addlns
MsgBox a.Name
Next a

XLS . ,
:
MsgBox ActiveWorkbook.Windows.Count
XLS
^ V i s i b l e .
:

For Each Win In ActiveWorkbook.Windows


Win.Visible = F a l s e
Next Win
, (). , . M y a d d i n . x l a , 0:
MsgBox Workbooks{"Myaddin.xla").Windows.Count

550

21.


XLS, XLS, . , , XLS
, .
VBA , , . , Workbooks, , . , Al
M y a d d i n . x l a , .
MsgBox Workbooks("Myaddin.xla").Worksheets{1) _
.Range("Al"}.Value
, , .
, ( ).
Sub CopySheetFromAddin()
Sec AddinSheet = Workbooks("Myaddin.xla").Sheets(1)
NuraSheets = ActiveWorkbook.Sheets.Count
AddinSheet.Copy After:=ActiveWorkbook.Sheets(NumSheets)
End Sub
.
Sub CopySheetFromAddin()
Workbooks ("Myaddin.xla 11 ) . Sheets (1) .Copy

End Sub

, ,
. VBA Th:.sworkbook, . ,
VBA . Al :
MsgBox ThisWorkbook.Sheets 1"!"),Range("Al") .Value

VBA
VBA, ,
XLS.
, , . Excel .
,
. sub ,
.
, , . : , :
1, - OnTime, .

V.

551

Run A p p l i c a t i o n
, . :
Application.Run "Myaddin.xlaIDisplayNames"
Tools^>References VBE, .
VBA, . Run : , P r i v a t e ,
D i s p l a y N a i n e s , , Tools^References:
Call DisplayNames

.


, .
, ,
.
Object Browser.
, ^ . Excel,
, .
1. VBE Lookup. x l a Project.
2. <F2> Object Browser.
3. Libraries () lookup. .
4. (5 Classes (), .

552

21.

, , Lookup_common
, , .
DoLookupCommand , .
, Excel
^^. DoLookupCommand
. , .
VBA,
.
, XLS. , Excel
( ).
, P r i v a t e . .
P r i v a t e
, , .
, ^
P r i v a t e , .
. , , .
, .
, , , . , N e w f u n c s . x l s
MOVAVG,
:
=Newfuncs.xls!MOVAVG(Al:A50)
.
:
=MOVAVG(A1:A50)

VBA
,
VBA, .

Addlns
A d d l n s , Excel.
, .
,
A d d l n s . , .

V.

553

Addlns
, Addlns, .
Excel , ,
Windows. Excel 2002 :
HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Excel\Add-in Manager
Windows ( r e g e d i t . e x e )
. , "" , Excel, .
, , " ,
( ).
Windows\Application data\Microsoft\Addins
Addln Addlns , VBA. ,
,
.
Addlns VBA ,
Add Addlns.
Application.Addlns.Add " : \ f i l e s \ n e w a d d i n . x l a "
Addlns
, .
, ,
.
(,
-), Excel Add. Myaddin.xla : Addlns. ( True)
. ,
:
Application.Addlns.Add "a:\Myaddin.xla", True
Addlns .
, I n s t a l l e d
True.
, Excel
. Excel
(.. ), , Addlns Excel.

Addlns
,
Addlns. A d d l n s D e l e t e Remove.
( r e g e d i t . exe). Excel. , .

554

21.

A d d l n s
, XLA,
. , . 21.4. A d d l n s .

. 21.4. Addlns

Addln
A d d l n A d d l n s . ,
, ,
A d d l n s , .
MsgBox A d d l n s ( 1 ) - N a m e
A d d l n 14 ,
. "". , .

Name
. Name ,
, Name.

Path
, , . .

FullName
, ,
. ,
Name P a t h . :
MsgBox Addlns(I).Path & "\" & Addlns(1)-Name
MsgBox Addlns(1).FullName

Title
, . T i t l e
. , ^
( ). XLS , .
, Name. A d d l n s . Name

V.

5J5

T i t l e .
View Manager (.. V i e w . x l s ) , T i t l e View Manager.
Sub ShowName{)
MsgBox Addlns("View Manager").Name
End Sub
, , , .

Comments
, ,
. Comments .
. 255 ,
100.

Installed
I n s t a l l e d T r u e , (.. ).
I n s t a l l e d T r u e . F a l s e .
(.. ) MS Query VBA.
Sub InstallQueryO
Addlns{"MS Query Add-In").Installed = True
End Sub
MS Query. ,
I n s t a l l e d T r u e - . (), I n s t a l l e d
F a l s e .
^, . , i n s t a l l e d False.

,
A d d l n s , , . ,
,
^.
Sub CountlnstalledAddlns()
Dim Count As Integer
Dim Item As Addln
Dim Msg As String
Count = D
For Each Item In Addlns
If Item.Installed Then Count = Count + 1
Next Item
Msg = ": " & Addlns.Count & Chr(13)
Msg = Msg & ": " & Count
MsgBox Msg
End Sub

556

21.

Addlns , . , ^.
Sub I n s t a l l A l l O
Dim Count As I n t e g e r
Dim Item As Addln
Count = 0
For Each Item In Addlns
If I t e m . I n s t a l l e d Then
I t e m . I n s t a l l e d = False
Count = Count + 1
End If
Next Item
MsgBox " " & Count & " "
End Sub
isAddin ,
. , ,

isAddin True.


, :
^ ^. . : ^ I n s t a l l e d T r u e . ,
.
VBA. ,
Workbooks("Myaddin.xla").Close
close , I n s t a l l e d False. ,
, .
i n s t a l l e d False.
, , Excel
. .

Addln
Addln : A d d l n l n s t a l l (
) A d d l n U n i n s t a l l ( ).
,
ThisWorkbook, .
.
P r i v a t e Sub Workbook_Addinlnstall()
MsgBox ThisWorkbook.Name & _
" ."
End Sub

V.

557

A d d i n i n s t a l l Open. A d d i n i n s t a l l
. , Workbook_Open.
19.


. .


,
, ,
.
,
.

A p p l i c a t i o n . S c r e e n U p d a t i n g F a l s e .

,
. O p t i o n E x p l i c i t
, ,
, . ,
S e r i e s ,
:
Dim Si As Series
Set SI = ActiveWorkbook.Sheets(1).ChartObjects(1).
Chart.SeriesCollection{1)
. As
Object.
With-End With , .
, .
, VBA, . ,
. .
.
,
.
. ,
.

558

21.


Excel ( ) . , ,
. , , .
.
( ), , .
1. .
2. VBE , VBA (, , UserFonm , ,
T h i s W o r k b o o k , ). .
3.

4.

.
( ).

5. , . 2.
6. .
7. , ,
.
8.

9. , ,
.
, , . , , , XLA 55%.

,

. . "" , .
,
.


( ^, ^).
. , VBA ( A d d l n s )

V.

559

21.1 (
ThisWorkbook). > A d d l n l n s t a l l
Open.

21.1.
Dim InstalledProperly As Boolean
Private Sub Workbook_Addinlnstall()
InstalledProperly = True
End Sub
Private Sub Workbook_Open()
If Not ThisWorkbook.IsAddin Then Exit Sub
1

'

If Not InstallProrerly Then


Addlns
If Not InAddlnCollectionfThisWorkbook) Then _
Addlns.Add FileName:-ThisWorkbook.FullName

AddlnTitle = GetTitle(ThisWorkbook}
Application.EnableEvents = False
Addlns(AddlnTitle).Installed = True
Application.EnableEvents = True

'


Msg ThisWorkbook.Name & _
" ."
Msg = Msg " ."
MsgBox Msg, vblnformation, AddlnTitle
End If
End Sub
, Workbook_
Addlnlnstall. I n s t a l l P r o p e r t l y True. ^,
Workbook_AddInI:nstall , I n s t a l l P r o p e r l y , False.
Workbook_Open ,
. , .
, I n s t a l l e d P r o p e r l y . . , Addlns. , .
.
^.
, ^
.
, 21.2. InAddlnCollection True,

21.

, , Addlns. G e t T i t l e
T i t l e .

21.2. Excel
Function InAddInCollection{wb) As Boolean
Dim Item As Addln
For Each Item in Addlns
If Iten.Name = wb.Name Then
InAddlnCollection = True
End If
Next Item
End Function
Function GetTitle(wb) As String
Dim Item As Addln
GetTitle ""
For Each Item In Addlns
If Item.Name = wb.Name Then
GetTilte = Item.Title
End If
Next Item
End Function


,
. , .
. P a t h , .
, , , , . .
Sub G e t H e l p O
Dim P a t h As S t r i n g
P a t h = ThisWorkbook.Path
A p p l i c a t i o n . H e l p P a t h & "\USER.HLP"
End Sub
API Windows,
, Windows . DLL, win-dows\System ( -).
System Windows API G e t S y s t e m D i r e c t o r y .

Excel
, Excel 2002..
,
Excel. "" :

V.

561

Sub CheckVersionO
If Val(Application.Version) < 10 Then
MsgBox " Excel 2002 . "
ThisWorkbook.Close
End if
End Sub
V e r s i o n A p p l i c a t i o n . ,
10.0. Val,
.
26.

: VBA.
, VBA .

562

21.


. 22
23
. 24 . 25 .


' . . Excel .
. , ( , VBA).

.


Excel 97, Microsoft

. ,
CommandBar.
, , .

,
. .

.
Worksheet Menu Bar ( ) Chart Menu
Bar { ) ( 23).

,
( 23).

, ,
. 23 .


,
Excel,

.
, ,
.

.
.
, ,
. ,
, Excel.

. . .

. .

, . Excel , . .


. Excel:
^ ^, ^. .
, VBA.
. , .
. .

Excel

.
.


(XLS)
(XLA),
( "

VI.

565

" ).
. , . , .
. Excel XLB,
Windows (\Windows \WINNT}. Excel.
XLB , , Excel, .
. , .
Excel XLB.
(
), Excel XLB
. , XLB Excel, Excel XLB. , . , , .
^ ^.
Excel 2002 Excel, XLB
. , .


Excel . , ,
.
, . Excel
XLB . - (, ),
.
,
. , , , , , .
.
, VBA,
. , .
" "
.
XLIJ . ,
" " . , VBA .

566

22.



Excel
. VBA, .
, ,
( , ), "". , Excel, , ,
.
, ().


( ), Excel . Excel
.

^.
^.
.
Excel , . , (. 22.1).
.
.
. ,
. : .
, .
. , .
,
.
. , . , , .

.
, . Microsoft ,
. , .
, .

VI,

567

. 22. J.


( , ),
, .
.
.



, Excel . ( ).
. 22.2 , .

. , .
. 22.2. ,


.
.
.


. .

568

22.


. . , .
, , . , .


Excel (.., ), . ,
. .
, <Ctrl> . ,
.


,
( 22.3).

. 22.3.

17 . , ,
. ,
.


,
.
. ( ).

, . 22.4. , .

VI.

56!'

42 . , . " ",

ic. 22.4.


. , , .


,
^ ^ .
,
, . 22.5. , T o o l b a r s , . , , .
, .
,
. .

570

22.

. 22.5.

,
VBA. .

.
,
. , .
, , , , ,
. " Excel " .


21, .
. .
: , .
VBA .
, .
. XLS , ,
XLS!
, ,
, \ XLS, .
,

XLS. , XLS,
. ( VBA)
O n A c t i o n , , , XLS. , " ", . .

VI.

571

CommandBars
CommandBars, A p p l i c a t i o n ,
CommandBar. CommandBar C o n t r o l .
, VBA.

( , ).
Excel ( )
, CommandBars. '
.

54 Excel.

. , .

. ,
.

58 , Excel.
Excel 2002
Excel 2002 Excel, , . Excel 2002 , Excel 2000:
;
;
;
;
;
;
;
;
.
, , , (
). .


, , .
CommandBars . VBA , .

572

msoBarTypeNormal

msoBarTypeMenuBar

msoBarTypePopup

22.

CommandBar
CommandBars, . (. 22,6),
CommandBar, CommandBars.
Excel 2002 114
, , .
I n d e x , Type Name ( , ). , ,
.

- 22.6. VBA CommandBar


Sub ShowConimandBarNames {)
Dim Row As Integer
Dim cbar As CommandBar
Cells.Clear
Row = 1
For Each cbar In CommandBars
Cells(Row, 1} = cbar.Index
Cells{Row, 2) = cbar.Name
Select Case cbar.Type
Case msoBarTypeNormal
Cells(Row, 3) = "Toolbar"
Case msoBarTypeMenuBar
Cells(Row, 3) = "Menu Bar"
Case msoBarTypePopUp
Cells{Row, 3) = "Shortcut"
End Select
Cells(Row, 4) = cbar.Builtln
Row = Row + 1
Next cbar
End Sub

VI.

573

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


VBA Add CommandBars.
, ,
(Custom 1):
CommandBars.Add
, , ( V i s i b l e F a l s e ) .
. .
Sub CreateAToolbar()
Dim TBar As CommandBar
Set TBar = CommandBars.Add
With TBar
-Name = "MyToolbar"
,Top = 0
.Left = 0
.Visible = True
End With
End Sub
C r e a t e T o o l b a r Add CommandEars Tbar,
.
, . Left
, ,
Excel.
CommandBars
UserForin ThisWorkbook, Sheet, c h a r t A p p l i c a t i o n . :
Application.CommandBars.Add
VBA ,
.


CommandBar
i n d e x Name. , Standard ()
I n d e x , 3. :
CommandBars(3)
CommandBars("Standard")

574

22.

,
. , , S t a n d a r d , s t a n d a r d STANDARD.
Excel
index ( ). , Excel 2002 3-0 Settings { ) index, 56. Excel 2000 53 index,
WordArt.
Excel, Name, index.


, ,
( ) .
, MyToolbar:
CommandBars ("MyToolbar"} .Delete
, .
, ,
. MyToolbar, . , .
On Error Resume Next
CommandBars("MyToolbar").Delete
On Error GoTo 0
, CommandBars. CommandBar ( ) True, . CommandBars
, , .
Function CoimandBarExists (n) As Boolean
Dim cb Aa CommandBar
For Each cb In CammandBars
If UCase(cb.Name) - UCase(n) Then
CorranandBarExis ts = True
Exit Function
End If
Next cb
CommandBarExists = False
End Function


CommandBar.
B u i l t l n . True, Excel .
Left ( ).

VI.

575

Name .
P o s i t i o n , . .
msoBarLef t .
msoBarTop .
msoBarRight .
msoBarBottom .
msoBarFloating .
msoBarPopup .
P r o t e c t i o n , .
.
msoBarNoProtection ( ) . .
msoBarNoCustomize .
msoBarNoResize .
msoBarNoMove .

msoBarNoChangeVisible .

msoBarNoChangeDock
.
msoBarNoVerticalDock
)1 ,
msoBarNoHorizonCalDock
.
( ).
, .
.
msoBarTypeNormal .

msoBarTypeMenuBar .

msoBarTypePopUp .
V i s i b l e True .
VBA, , .


,
. CommandBars , B u i l t True.
Function CustomToolbars()
Dim cb As CoimandBar

576

22.

Dim Count As Integer


Count = 0
For Each cb In CommandBars
If cb.Type = msoBarTypeNormal Then
If Not cb.Builtln Then
Count = Count + 1
End If
End If
Next cb
CustomToolbars = Count
End Function


P r o t e c t i o n CommandBar
. P r o t e c t i o n M y T o o l b a r :
C o m m a n d B a r s ( " M y T o o l b a r " ) . P r o t e c t i o n = msoBarNoCustomize
.
, P r o t e c t i o n , , . , M y T o o l b a r :
Set cb = CommandBars("MyToolbar")
cb.Protection = msoBarNoCustomize + msoBarNoMove

""
Excel " " ( ).
. , Chart () , . , . Microsoft
(toolbar autosensing).
. , ,
(autosensing) .
, , . , .

. , ,
. Excel .
, 22.1,
. S e l e c l i o n C h a n g e
T o o l b a r R a n c j e . . . .

VI.

577

, ,
.
, WorkbookjDpen, Autosense. , ,
Buttonl, Button2, Button3 Button4. , (
).

22.1. ,

Sub CreateToolbar()
'
AutoSense
Dim AutoSense As CommandBar
Dim Button As CommandBarButton
Dim i As Integer
1


On Error Resume Next
CoxranandBars!"AutoSense").Delete
On Error GoTo 0

'


Set AutoSense - CommandBars.Add
For i = 1 To 4
Set Button = AutoSense.Controls.Add{msoControlButton)
With Button
.OnAction = "Button" & i
.Faceld = i + 37
End With
Next i
AutoSense.Name = "AutoSense"
End Sub
SelectionChange (
s h e e t l ) :
Private Sub Worksheet_SelectionChange(ByVal Target As _
Excel.Range)
If Union(Target, Range("ToolbarRange")}.Address = _
Range("ToolbarRange").Address Then
CommandBars("AutoSense").Visible = True
Else
ComnandBars("AutoSense").visible = False
End If
End Sub
. , ToolbarRange, V i s i b l e AutoSense True. V i s i b l e False.
, Workbook_BeforeClose,
AutoSense . , , .

578

22.

, Excel, 19.

( )
Excel . , , .
, .
, , . H i d e A l l T o o l b a r s HI w o r k b o o k _ O p e n .
R e s t o r e T o o l b a r s W o r k b o o k _ B e o r e C l o s e .
, ,
TBSheet. , R e s t o r e T o o l b a r s
.

, VBA (
). 22-2.

22.2,

Sub HideAllToolbarsO
Dim CommandBar
Dim TBNum As Integer
Dim TBSheet As Worksheet
Set TBSheet = Sheets{"TBSheet")
Application,ScreenUpdating = False
1


TBSheet.Cells.Clear

TBNum =
For Each In CommandBars
If .Type = msoBarTypeWormal Then
If .Visible Then
TBNum = TBNuia + 1
.Visible = False
TBSheet.Cells(TBNum, 1) = .Name
End If
End If
Next
Application.ScreenUpdating = True
End Sub
Sub RestoreToolbars{)
Dim TBSheet As Worksheet
Dim cell As Range
Set TBSheet = Sheets("TBSheet")

VI.

57$

Application.ScreenUpdating

False

'


On Error Resume Next
For Each cell In TBSheet.Range("A:A") _
.SpecialCells(xlCellTypeConstants)
CommandBars(cell.Value).Visible = True
Next cell
Application.ScreenUpdating = True
End Sub
, .
.
Enabled False
, .


CommandBar, , C o n t r o l .
, , .
T e s t C a p t i o n
C o n t r o l , Standard ().
c o n t r o l 3.
Sub T e s t ( }
MsgBox
End Sub

CommandBars(3).Controls(l).Caption

,
. 22.7 (, Standard () ).
, ( ).

. 22.7.
Ca.pt ion
,
C a p t i o n .
, .
Sub Test2
MsgBox CorrjnandBars ("Standard") .Controls ( " ") .Caption

End Sub


. , , , Excel.
FindControl, i d .
23.

580

22.

C a p t i o n , , (&). ,
, (, &New).
C a p t i o n
.
Control Central.
, Drawing () ( ). c o n t r o l ,
c o n t r o l , 23, .



C a p t i o n C o n t r o l , CommandBar. Standard ().
Sub ShowControlCaptions()
Dim Cbar as CommandBar
Set CBar = CommandBars("Standard"}
Cells.Clear
Row = 1
For Each ctl In CBar.Controls
Cells(Row, 1) = ctl.Caption
Row = Row + 1
Next ctl
End Sub
S h o w C o n t r o l C a p t i o n s . 22.8.



.
(.. I), C a p t i o n .
S h o w A l l T o o l b a r C o n t r o l s . 22.9.
Sub ShowAllToolbarControls()
Dim row As Integer
Dim Cbar As CommandBar
Dim ctl As CommandBarControl
Cells.Clear
row = 1
For Each Cbar In CommandBars
If Cbar.Type = msoBarTypeNormal Then
Cells(row, 1) = Cbar.Name
For Each ctl In Cbar.Controls

VI.

581

Cells(row, 2) = ctl.Caption
row = row + 1
Next ctl
End If
Next Cbar
End Sub

. 22.5.
,
Standard ()

. 22.9.



CommandBar, Add C o n t r o l s .
MyToolbar.
, msoControlButton,
.
CommandBars("MyToolbar").Controls.Add _
Type:=msoControlButton
, ,
. . ,

. ,
Faceld,
OnAction :
Sub AddButtonO
Dim NewBtn As CommandBarButton
Set NewBtn = CommandBars("MyToolbar").Controls.Add _
(Type:=msoControlButton)

582

22.

With NewBtn
.Faceld = 300
.OnAc tion = "MyMacro"
.Caption = " "
End With
End Sub
A d d B u t t o n (NewBtn),
. W i t h - W i t h End .


CommandBar, D e l e t e C o n t r o l s . MyToolbar;
CommandBars{"MyToolbar").Controls(1).Delete
, , . , g o r t B u t t o n :
CommandBars("MyToalbar").Controls("SortButton").Delete


, ,
.
.

B e g i n G r o u p T r u e ,
.

B u i l t l n T r u e ,
Excel ( ).

C a p t i o n
. ,
.

E n a b l e d T r u e , .

F a c e l d , ,
.

Id Excel ( ).

O n A c t i o n VBA,
.
S t a t e "'" . C o m m a n d B a r B u t t o n .

S t y l e . CommandB a r B u t t o n CommandBarComboBox.

T o o l T i p T e x t ,
.

, .

VI

583

Style
s t y l e ( C o m m a n d B a r B u t t o n CommandBarComboBox). . ,
, S t y l e
m s o B u t t o n l c o n A n d C a p t i o n . S t y l e
CommandBarButtorK

msoButtonAutomatic;

msoButtonCaption;

msoButtonlcon;

msoButtonAndCaption;

msoButtonlconAndCaptionBelow;

msoButtonAndlconAndWrapCaption;

msoButtonAndlconAndWrapCaptionBelow;

msoButtonWrapCaption.

CommandBarComboBox
msoComboLabel HmsoComboNormaX.
. 22.10 ,
( ). .
, , Web .

. 22.10, Style ()
, ,
c a p t i o n .
Faceld.


Excel ,
.
42 ,
. , . VBA.
( ),
, F a c e l d . , s t y l e , m s o B u t t o n C a p t i o n .

584

22.

F a c e l d M y T o o l b a r 45, :
CommandBars ("MyToolbar") .Controls {1) .Faceld = 45
, ,
F a c e l d .
F a c e l d .
Face ID Identifier. , . , F a c e l d (. 22.).

. 22.11. FacetD Identifier


Faceld

FacelD Identifier Web- .

Visible
V i s i b l e . F a l s e .
, v i s i b l e ,
. , . , .
Sub ToggleAllToolbars()
Dim cb As CommandBar
On E r r o r Resime Next
For Each cb In ComraandBars
If cb.Type = msoBarTypeNormal Then
cb.Visible = Not cb.Visible
End If
Next cb
End Sub
On E r r o r Resume Next ) 2002 . ,
,
, .

V/.


22.3 ,
.
(. 22.12). W o r k s h e e t
. S e l e c t i o n C h a n g e , ,
.

. 22.12.

1

22.3.

Sub MakeNumberFormatDisplay()
Dim TBar As CommandBar
Dim NewBtn As CommandBarButton
1

.
On Error Resume Next
CommandBars(" ").Delete
On Error GoTo 0


Set TBar = CommandBars.Add
With TBar
.Name = " "
.Visible - True
End with

'

()
Set NewBtn = CommandBars(" ").Controls.Add _
(Type:=msoControlButton)
With NewBtn
.Caption = ""
.OnAction = "ChangeNumFormat"
.TooItipText = " "
. Style - msoButtonCaption
'.Width = 100
End With
Call UpdateToolbar
End Sub
19.

586

22.

UpdateToolbar, ,
NumberFormat A c t i v e C e l l Caption (
):
Sub UpdateToolbar()
On Error Resume Next
CommandBars{" "). _
Controls(1).Caption = ActiveCell.NumberFormat
If Err <> 0 Then CommandBars(" ")- _
Controls(l).Caption = ""
End Sub
O n A c t i o n () C h a n geNumFormat, . Excel (. 22.13).

. 22.13. '

Sub ChangeNumFormat()
Application.Dialogs(xlDialogFormatNumber).Show
Call UpdateToolbar
End Sub
, , , :
Formatting (),
, , .


Excel . .
O n A c t i o n . Sort
Ascending ( ) :
CommandBars("Standard")-Controls{"Sort Ascending") _
.OnAction = "ShowMsg"
Sort Ascending ( ) . VBA ShowMsg.
, O n A c t i o n
:

VI.

587

CommandBars(H Standard").Controls("Sort Ascending") _


.OnAction = ""

""
E x e c u t e .
, . , Sort
Ascending ( ) Standard ():
CommandBars("Standard").Controls("Sort Ascending").Execute
E x e c u t e no
, O n A c t i o n .



, .
.
23- . .
, Excel. , , .
, . . , :

m s o C o n t r o l B u t t o n ;

m s o C o n t r o l E d i t ;

m s o C o n t r o l C o m b o B o x ;

m s o C o n t r o l D r o p d o w n ;

m s o C o n t r o l B u t t o n P o p u p , . , .
C o n t r o l . ,
.

. 22.14.
( ),
M o n t h L i s t 22.4 ,

22.

. , OnAction,
( ) , .
, , . 22. L4.

22.4.
Sub MonthListO
Dim TBar As CommandBar
Dim NewDD As CommandBarControl
'


On Error Resume Next
CommandBars(" ").Delete
On Error GoTo 0


Set TBar = CommandBars.Add
With TBar
.Name = " "
.Visible = True
End With
DropDown
Set NewDD = CommandBars(" ").Controls.Add _
(Type:=msoControlDropdown)
With NewDD
.Caption = "DateDD"
.OnAction = "PasteMonth"
.Style = msoButtonAutomatic


For i = 1 12
.Addltem Format(DateSerial(1, i, 1) , "mmrnrn")
Next i
-Listlndex = 1
End With
End Sub
PasteMonth.
Sub PasteMonth()

On Error Resume Next
With CommandBars(" ").Controls("DateDD")
ActiveCell.Value = .List(.Listlndex)
End With
End Sub
:
, Worksheet_SelectionCh,ange. ,
, ,
. , . , L i s t l n d e x
.
P r i v a t e Sub Worksheet_SelectionChange(ByVal Target _
As Excel.Range)

VI.

589

For i = 1 To 12
Set ActCell = Target.RangeV'Al")
If ActCell.Value = Format(DateSerial(1, i, 1), _
"iminm") Then
CommandBars(" ")-Controls("DateDD") _,
.Listlndex = i
Exit Sub
End If
Next i
End Sub


Excel .
: .

590

22.

Windows ,
. Windows

. , : . , .
Excel ,
, . , Excel.


Excel
22, ,
( )
CommandBar, , 22, .
?

Excel, .


. ( ,
), ,
.
.

. , .
.

Excel
Excel . , , . . , , .
, Excel, , . , Excel
. Excel :
VBA.
Excel , .
Excel. , , XLB, Windows.
XLB 22.

,
. , VBA,
, ,
{ ) . , VBA,
, VBA, .

, .

. , ,
. CommandBar
Microsoft Office.

. , . Excel : ,
, .

. . , Excel
.

. , , . , . .

. ,
.
.

592

23.

. , . ,
.
. , .
, ^ : ,
, .
. ,
. .
. , . , . .
. , , ,
.
. , . VBA , F a c e l D .
. , .
. , <Ctrl+S>
, ^ .


Excel: ,
. ,
, . ,
.
, . , Excel .
.
, ,
.
, , , , , . , 8
, , <Ctrl+N>, ( Excel 2002)
.
Excel 5/95
Excel 5 Excel 95, . Excel 97,
. , ,
- .
, 8
.
, . (VBA) (command bar).

VI.

593

$ Excel 5/95. , ^ ^. , Excel 5/95, ,


, Excel 97 - . Excel 5/95,
- VBA
. Excel 5/95 . VBA.
.
- Excel 2000 , . ,
Microsoft. , . ,
.


. ,
, . , , Excel, , , . Excel
.


Excel , . , . , .
,
(, (?) , ).
CommandBars
CoittmandBara A p p l i c a t i o n .
VBA A p p l i c a t i o n ( ). , (
VBA ) CoimandBars:
MsgBox ComrciandBars (1} .Name
CommandBars A p p l i c a t i o n , :
MsgBox Application.CommandBars(1).Name
Excel . Excel , , . ,
, ,
. , Findcontrol
.
VBA, .

594

23.

22 .

VBA
VBA, Excel.


L i s t M e n u I n f , , . (, ,
), .
Sub ListMenuInfo()
Dim row As Integer
Dim Menu As CommandBarControl
Dim Menultem As CommandBarControl
Dim SubMenultem. As Camm.and3arCon.trol
row = 1
On Error Resume Next
For Each Menu In CommandBars(1).Controls
For Each Menultem In Menu,Controls
For Each SubMenultem In Menultem.Controls
Cells(row, 1) = Menu.Caption
Cells(row, 2) = Menultem.Caption
Cells(row, 3) = SubMenultem.Caption
row = row + 1
Next SubMenultem
Next Menultem
Next Menu
End Sub
. 23 ListMenuInfo.

/*. 23.1. ,
s pfsv.ibtnaine
ListMenuInfo

V/.

595

, , Web- .

on E r r o r Resume Next , ,
.


VBA . ContmandBars,
:
CommandBars("Worksheet Menu B a r " )
CommandBars (1)
VBA Add C o n t r o l s . "";
m s o C o n t r o l P o p u p . .
, .
.
1. Add , . Add , (
), ,
( Excel).
2. . , C a p t i o n O n A c t i o n .

, Windows
. ,
, ,
, . .
> , ,
.
<- .
.
< .
, .
< .
> "" ( ),
.
, , {...).
- . .
,
, .

23.

, , . VBA, ( ) , Enabled False.


. ,
.

:

. .
Sub AddNewMenu()
Dim Helplndex As Integer
Dim NewMenu As CommandBarPopup
i
1

?
Helplndex = CommandBars(1).Controls{"Help"}.Index


Set NewMenu = CommandBars (1) . Controls - Add __
(Type:=msoControlPopup, _
Before:=HelpIndex, _
Temporary:=True)

'

NewMenu.Caption = "^"
End Sub
. , , .
, ?.
.

, ? Help. Excel
( ).

:
23.1 . F i n d C o n t r o l , .
, -

23,1. Excel
Sub AddNewMenu{)
Dim HelpMenu As CommandBarControl
Dim NewMenu As CommandBarPopup
Dim. Menultem As CammandBaEControl
Dim Submenuitem As CommandBarButton
1

,
Call DeleteMenu


Set HelpMenu = CommandEars(1).FindControl(Id:=30010)

'

If HelpMenu Is Nothing Then


V/.

597

'

Set NewMenu = CornmandEars (1) .Controls .Add .


(Type:=msoControlPopup, _
temporary:=True}
Else
Help
Set NewMenu = CommandBars(1}.Controls.Add
{Type:=msoControlPopup, _
Before:=HelpMenu.Index, _
temporary:=True)
End If


NewMenu.Caption = "&"
End Sub
( ). "
" .
F i n d C o n t r o l ,
ID . Excel
CommandBar ID. ID
:
MsgBox C o m m a n d B a r s ( 1 ) . C o n t r o l s ( " " ) . I D
30010 ID, F i n d C o n t r o l . . 23.1 ID Excel.

23.1. / Excel

File ()

30002

Edit ()

30003

View ()

30004

Insert ()

30005

Format ()

30006

Tools ()

30007

Data ()

30011

Chart ()

30022

Window ()

30009

Help ()

30010


, D e l e t e . .
On E r r o r ReEsume N e x t , , .

598

23.

Sub DeleteMenuO
On Error Resume Next
CommandBars(1).Controls("").Delete
End Sub
:
NewMenu.Caption = "&"
.


" " ,
. 23.2 , .

23.2.
Sub CreateMenu()
Dim HelpMenu As CommandBarControl
Dim NewMenu As CommandBarPopup
Dim MenuItern As CcrnimandBarControl
Dim Submenuitem As CommancffiarButton
1

,
Call DeleteMenu

'


Set HelpMenu = CommandBars(1).FindControl{Id:=30010)

If HelpMenu Is Nothing Then



Set NewMenu = CommandBars(1).Controls.Add _
(Type:=msoControlPopup, _
temporary:=True)
Else

Set NewMenu = CommandBars(1).Controls.Add _
(Type:=msoControlPopup, _
Before:=HelpMenu.Index, _
temporary:=True)
End If

'


NewMenu.Capt ion = "^"


Set Menultem = NewMenu.Controls-Add _
(Type:=msoControlButton)
With Menultem
.Caption = "^ ..."
.Faceld = 162
.OnAction = "Macrol"
End With


Set Menultem = NewMenu-Controls.Add _
(Type:=msoControlButton)

V/.

599

With Menultem
.Caption = "^ ..."
.Faceld = 590
.OnAc t i on = 'Macro2"
End With

Set Menultem = NewMenu.Controls.Add _
(Type:=msoControlPopup)
With Menultem
.Caption = " ^"
,BeginGroup = True
End With
1


Set Submenuitem = Menultem.Controls-Add _
(Type:=msoControlButton)
Wi th Submenui tern
,Caption = " "
.Faceld = 420
.OnAction = ""
End With


Set Submenuitem = Menultem.Controls.Add _
(Type:=msoControlBut ton)
With Submenuitem
.Caption = " &"
.Faceld = 4 2 2
.OnAction = "Macro4"
End With

End Sub
C r e a t e M e n u , . 23.2. , .

. 23.2. VBA -jmo , .


,
. .
,
( ). , , , .

23.

C r e a t e M e n u .

m s o C o n t r o l B u t t o n , . m s o C o n t r o l P o p u p ,
. , M e n u l t e m
C o m m a n a B a r C o n t r o l .

B e g i n G r o u p T r u e ,
.
.

F a c e l D ( ),
. F a c e l D .

, C a p t i o n , (&) "" .
.


23.2 .
Excel, , .
Excel 5 Excel 95 . * Excel 97.
VBA, Excel .
23.3 . C l e a r A l l B u t F o r m u i a s .

23.3.
Sub AddMenultem()
Dim ToolsMenu As CommandBarPopup
Dim NewMenuItem As CommandBarButton
'

,
Call DeleteMenuItem


Set ToolsMenu = CommandBars(1).FindControl(Id:=30Q07)
If ToolsMenu is Nothing Then
MsgBox " ."
Exit Sub
Else
Set NewMenuItem = ToolsMenu.Controls.Add _
(Type:=msoControlButton)
With NewMenuItem.
.Caption = & "
.Faceld = 348
.OnAction "ClearAllButFormulas"
.BeginGroup = True
End With
End If
End Sub

VI.

601

. 23.3 . , .
ID ( 30007).

. 23.3.


, D e l e t e C o n t r o l s .
. , F i n d C o n t r o l ,
, .



Excel . , .
, S h o r t c u t T e x t . , S h o r t c u t T e x t
. . , VB.
23.4 . , S h o r t c u t T e x t
" < C t r l + s h i f t+C> ". M a c r o O p t i o n s ,
.

23.4.
Sub AddMenultem()
Dim ToolsMenu As CommandBarPopup
Dim NewMenuItem As CommandBarButton

602

23.

'

,
Call DeleteMenuItem

Set ToolsMenu CommandBars(1).FindControl(Id:=30007}
If ToolsMenu Is Nothing then
MsgBox " - _
Ctrl+Shift+C."
Exit Sub
Else
Set NewMenuItem = ToolsMenu.Controls.Add __
(Type:=msoControlButton)
With NewMenuItem
.Caption = " & "
.Faceld = 348
.ShortcutText = "Ctrl+Shift+C"
.OnAction = "ClearAllButFormulas"
.BeginGroup = True
End With
End If


Application.MacroOptions _
Macro:="ClearAllButFormulas", _
HasShortcutKey:=True, _
ShortcutKey:="C"

End Sub
,
. 23.4.

. 23.4. ,

VI.


: VBA,
. , , .
." ,
.
.
Workbook_Open,

.
, , .
, , Excel,
. Power Utility Pak , PUP 2000
. ,
.
<CtrJ+Sniff+U>, PUP 2000. CreateMerm <Ctrl+Shift+U>:
Application.MacroOptions Macro:="CreateMenu", _ HasShortcutKey:=True,
ShortcuCKey:="U"


, .
, , , , Excel. ,
. ,
, Excel.
, , , .
19.


, , Open Workbook. , , C r e a t e M e n u .
P r i v a t e Sub Workbook_Open{}
C a l l CreateMenu
End Sub
, ,
. ,
D e l e t e M e n u .

604

23.

Private Sub Workbook_BeforeClose(Cancel As Boolean)


Call DeleteMenu
End Sub
, . . . . ? ,
B e f o r e C l o s e , W o r k b o o k _ B e f o r e C l o s e
. , , , !
Excel , . Workbook_Bef o r e C l o s e .
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not Me.Saved Then
Msg = " "
Msg = Msg & Me,Name & "?"
Ans = MsgBox(Msg, vbQuestion -t- vbYesNoCancel)
Select Case Ans
Case vbYes
Me.Save
Case vbWo
Me .Saved = True
Case vbCancel
Cancel = True
Exit Sub
End Select
End If
Call DeleteMenu
End Sub
, . ,
. D e l e t e M e n u , . , , , Excel. , , ,
. , S a v e d
T r u e ( )
. , B e f o r e C l o s e
, .


, - . . Excel , . ,
, .
VBA, , . , . .
.
P r i v a t e Sub Workbook_Open
C a l l AddMenu

VI.

60S

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call DeleteMenu
End Sub
Private Sub Workbook_Activate()
Call UnhideMenu
End Sub
Private Sub Workbook_Deactivate()
Call HideMenu
End Sub
, AddMenu.
, D e l e t e M e n u . , . UnhideMenu , , HideMenu
, .
HideMenu V i s i b l e
F a l s e , . UnhideMenu
. , . , , C a p t i o n
, .
Sub UnhideMenu
CommandBars (1} . C o n t r o l s (":") . V i s i b l e = T r u e
End Sub
Sub HideMenu()
CommandBars(1).Controls("").Visible
End Sub

False

, , V i s i b l e
E n a b l e d .
Web- .


Excel
. , ^ ,
. ,
. , .
. . 23.5
, ,
. ,
.
S t a t e .

606

23.


. , ,
, . .


AddMenuItem, 23.5, . .

. 23.5. ,

23.5. Excel
Dim AppObject As New XLHandler
Sub AddMenuItem()
Dim ViewMenu As CommandBarPopup
Dim NewMenuItem As CommandBarButton
'

,
Call DeleteMenuItem

'


Set ViewMenu = CommandBars(1)-FindControl(ID:=30004)
If ViewMenu Is Nothing Then
MsgBox " ."
Exit Sub
Else
Set NewMenuItem = ViewMenu.Controls-Add _
(Type:=msoControlButton)
With HewMenuItem
.Caption = "& "
.OnAction = "ToggleGridlines"
End With
End If

VI.

607

Set AppObject.AppEvents = Application


End Sub
AddMenuItem ,
. ,
( ).
, AddMenuItem
, . , XLHandler, .
P u b l i c WithEvents AppEvents As A p p l i c a t i o n
Private Sub AppEvents_SheetActivat.e{ByVal Sh As Object)
Call CheckGridlines
End Sub
Private Sub AppEvents_Work>ookActivate (EyVal Wb As Excel .Workbook)
Call CheckGridlines
End Sub
Private Sub AppEvents_WindowActivette _
(ByVal Wb As Workbook, ByVal Wn As Window}
Call CheckGridlines
End Sub
: .


C h e c k G r i d l i n e s . .
Sub CheckGridlines()
Dim TG As CommandBarButton
On Error Resume Next
Set TG = CommandBars(1).FindControl(ID:=30004). _
Controls( ")
If ActiveWindow.DisplayGridlines Then
TG.State = msoButtonDown
Else
TG.State = msoButtonUp
End If
End Sub
S t a t e . ,
. , .


, OnAction , ToggleGr i d l i n e s , .

608

23.

Sub ToggleGridlines()
If TypeName(ActiveSheet) = "Worksheet" Then
ActiveWindow.DisplayGridlines = _
Not ActiveWindow.DisplayGridlines
Call CheckGridlines
End If
End Sub
.
I f - T h e n , .



Excel 97, , . , . , . 23.6.
. VBA
, .

Puc. 23.6.
, .

. . 1, 2 3. 1 . 2 . 3 . ,
. ,
( ).

. , . ,
(&).

/. , .

VI

609

, .
,
.

. , .

FacelD. , , .

. 23.7 , , .

. 23.7. , ,
, , Web- . VBA, . , , .
1. , Web-.
2. M o d u l e l .
3. , , .
P r i v a t e Sub Workbook_Open(}
Call CreateMenu
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call DeleteMenu
End Sub
4. . .
5. , .
, .

610

23.


Excel
.
MakeMenuBar 23.6 , . . ( ).
: (?).

23.6. Excel
Sub MakeMenuBar(}
Dim NewMenuBar As CommandBar
Dim NewMenu As ommandBarControl
Dim Newltem As CommandBarControl
1

,
Call DeleteMenuBar

'


Set NewMenuBar = CoimandBars . AddfMenuBar: =True)
With NewMenuBar
.Name = " "
.Visible = True"
End With

File (ID=30002)
CommandBars<"Worksheet Menu Bar").FindControl(ID:=30002).Copy _
Bar:=CommandBars(" ")

'


Set NewMenu = NewMenuBar.Controls.Add _
(Type:=msoControlPopup)
NewKenu.Caption = "^"

'


Set Newltem = NewMenu.Controls.Add(Type:=msoControlButton)
With Newltem
.Caption = "& "
.OnAc t ion = "Delet eMenuBar"
End With


Set Newltem = NewMenu.Controls.Add(Type:=msoControlButton)
With Newltem
.Caption = "^"
.OnAction = "ShowHelp"
End With
End Sub
. 23.8 .
, , . S e t NewMenuBar = C o m m a n d B a r s . A d d ( M e n u B a r : = T r u e )
. V i s i b l e

VI.

611

T r u e , . 6
.
{ 22}
, nisoBarTypeWormai. , , , msoBarTypeMenuBar.

. ( D e l e t e M e n u B a r )
.
Sub DeleteMenuBar()
On Error Resume Next
CommandBars(" "}.Delete
On Error GoTo 0
End Sub
F i n d C o n t r o l . .
CommandBars("Worksheet Menu Bar").FindControl(ID:=30002).Copy
, { )
. ,
. , C a p t i o n MyMenuBar ( New (}
New Workbook ( )).
.
.

. 23.8.


,
Excel. Excel

. VBA.
Excel 2002 59 ( , Excel 2000).
,
msoBa r Type Popup. ,

612

23.

I n d e x Name.
.
. I n d e x , Name , .
Sub LisCShortCutMenus()
Dim Row As Long, Col As Integer
Dim cbar As CommandBar
Cells.Clear
Application.ScreenUpdating = False
Row = 1
For Each cbar In ConmandBars
If cbar.Type = rnsoBarTypePopup Then
Cells(Row, 1) = cbar.Index
Cells(Row, 2) = cbar.Name
For Col s i To cbar.Controls.Count
Cells(Row, Col + 2) =
cbar.Controls{Col).Caption
Next Col
Row = ROW + 1
End If
Next cbar
Cells.EntireColumn.AutoFit
End Sub
. 23.9 . . , ,
, , Plv?

^wc. 2.?.9. , ,

VI.

613

index,
.
index Excel.
Name.


. Cell (),
, , . , .
Sub AddltemToShortcutO
Set Newltem = CommandBars("Cell").Controls.Add
With Newltem
.Caption = " "
.OnAction = "ToggleWordWrap"
.BeginGroup = True
End With
End Sub
ToggleWordWrap. . 23.10
.

. 23.10.

O n A c t i o n
.
OnAction. .
,
.
Sub A d d l t e m T o S h o r t c u t O
Set Newltem = CommandBara("Document").Controls.Add(ID:=865}
Newltem.Caption = " "
End Sub
=>.
ID, 865,
:
MsgBcx CommandBars("Worksheet Menu Bar") _
.Controls{"").Controls("").ID

614

23.


D e l e t e , .
Sub RemovelcemFromShortcut{}
On Error Resume Next
CommandBars("Cell").Controls(" ").Delete
End Sub
On E r r o r Resume Next ,
, .
:
,
.
Sub RemoveHideMenuItems
CommandBars("Column").Controls{"").Delete
CommandBars{"Row"}.Controls("").Delete
End Sub



, .
, - , .
Row () Column ().
Sub DisableHideMenuItems()
CommandBars("Column").Controls{"").Enabled = False
CommandBars("Row").Controls("").Enabled = False
End Sub


. ,
,
. D i s a b l e C e l l
Cell ().
.
Sub D i s a b l e C e l l U
CommandBars("Cell").Enabled = F a l s e
End Sub
, .
Sub DisableAllShortcutMenus()
Dim cb As CommandBar
For Each cb In CommandBars
If cb.Type = msoBarTypePopup Then _
cb.Enabled = False
Next cb
End Sub

VI.

B1S

. ,
Excel. , Enabled True.


R e s e t (
). ,
. ' , .
Cell () .
Sub ResetCellMenuO
CommandBars("Cell">.Reset
End Sub


. 23.7 , M y S h o r t c u t . OnAction ,
,
. , ShowNumber F o r m a t .

23.7.
Sub CreateShortcut()
Dim myBar As CommandBar
Dim myltem As CommandBarControl
DeletShortcut
Set myBar = CommandBars.Add _
(Name:="MyShortcut", Position:=msoBarPopup, Temporary:=True}
Set myltem = myBar.Controls.Add(Type:=msoControlButton)
With myltem
.Caption = "^ ..,"
. OnAc t ion = " ShowForma tNurrber"
.Paceld = 1554
End With
Set myltem = myBar.Controls.AddfType:=msoControlButton)
With myltem
.Caption = "&..."
.OnAction = "ShowFormatAlignment"
.Facelct = 217
End With
Set myltem = myBar .Controls .Acld(Type: =msoControlButton)
With myltem
-Caption = "..."
.OnAction = "ShowFormatFont"
.Paceld = 291
End With

616

23.

Set myltem = myBar.Controls.Add(Type:=msoControlButton)


With myltem
-Caption = "&..."
.OnAction = "ShowFormatBorder"
.Faceld = 149
.BeginGroup = True
End With
Set myltem = myBar.Controls.Add(Type:=msoControlButton)
With myltem
.Caption = "&..."
.OnAction = "ShowFormatPatterns"
.Faceld - 1550
End With
Set myltem = myBar.Controls.Add(Type:=msoControlButton)
With myltem
.Caption = "&3..."
.OnAction = "ShowFormatProtection"
.Faceld = 2654
End With
End Sub
. 23.11 .

. 23.]I. VBA
, ShowPopup.
, .
, .
Private Sub Worksheet_BeforeRightClick(ByVal Target _
As Excel.Range, Cancel As Boolean)
If Union(Target.Range{"Al"}, Range("data"))-Address = _
Range("data").Address Then
CommandBars{"MyShortcut").ShowPopup
Cancel = True
End If
End Sub
, , , M y S h o r t c u t . C a n c e l T r u e , .

VI.

617

Web-
Cell ().

, ,
.
, .

618

23.

, - . ""
.
.

" "
( )!
, Excel.
.

Excel
Excel ,

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

.
.

, .
,
,
. , , .
,
(2, ), (),
, (4) , (5).
, .
,
4 5.

. , ,
.
< , .
< , .
- , .
& PrintMod VBA, ,
-- HelpMod VBA, ,
. .
userForm , ,
UserFonn.

620

24.

. Excel (, U s e r F o r m ) .

. HLP, Windows Help


System, ,
HTML Help System.

,
,
. , .
Web- .

,
Excel
,
, Excel.
WinHelp HTML Help.
.
,
Excel .

. !

Text Box ( ). ,
, .

. , . .
, .

. U s e r F o r m .




. , .
, ,

VI.

621

. .
. VBA :
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
Excel ^,
.
, . 250 .



T e x t Box . T e x t Box . ,
. . 24.1
T e x t Box, .

, 24.1. Text

Box -

, ActiveX
,
. , .

622

24.

, T e x t Box .
,
V i s i b l e T e x t Box T r u e .
. T e x t Box H e l p T e x t .
Sub ToggleHelpO
ActiveSheet.TextBoxesC'HelpText") .Visible =
Hot ActiveSheet-TextBoxesI"HelpText").Visible
End Sub



, , . ,
, .
. 24.2 , . , ( , ).

. 24.2.

,
S c r o l l A r e a . ,
, . , , . , ,
, , .
.
, , ,

VI

623



Excel
U s e r F o r m , . , .

Label

. 24.3 UserForm, L a b e l : ,
. S p i n B u t t o n . . , .

. 24.3.
SpinButton

SpinButton
. Caption
Label , .
P r i v a t e Sub SpinButtonl_Change()
HelpTopic = SpinButtonl.Value
L a b e l T o p i c C a p t i o n = Sheets{""). _
Cells(HelpTopic, 1)
LabelText.Caption = Sheets{"").Cells(HelpTopic, 2)
Me.Caption = APPNAME & ": Topic " & HelpTopic & " / " _
& SpinButtonl.Max
End Sub
APPNAME ,
.

"" Label


L a b e l . , L a b e l
,
F r a m e , . . 24.4
U s e r F o r m ,
.
F r a m e .
, L a b e l ,
. UserForm.

624

24.

24.1 U s e r F o r m _ I n i t i a l i z e . , S c r o l l H e i g h t Frame ' L a b e l . APPNAME , .

. 24.4.
Frame
Label

, 24.1. Label ,
,
Private Sub UserForm_lnitialize()
Me.Caption = APPNAME & ". "
LastRow = Sheets("") .Range("A6553 6").End(xlUp).Row
txt = " "
For r - 1 To LastRow
txt = txt & Sheets{"").Cells(r, I).Text & vbCrLf
Next r
With Label1
.Top = 0
.Caption = txt
.Width = 160
.AutoSize - True
End With
Framel.ScrollHeight = Labell.Height
Framel.ScrollTop = 0
End Sub
, L a b e l
, .



, , .
. 24.5 U s e r F o r m , L a b e l . ^ , ComboBox, a
.
, , . Label-B-Frame ( ), .

VI.

625

. 24.5. ,
Label,

, .
; . , . ComboBox U E e r F o r m _ I n i t i a l i z e . C u r r e n t T o p i c
,
.
P r i v a t e Suto U p d a t e F o r m O
ComboBoxTopics.Listlndex = CurrentTopic - 1
Me.Caption = HeZpFormCaption &. _
" (" & CurrentTopic & " " & TopicCount & " ) "
With LabelText
.Caption = HelpSheet.Cells(CurrentTopic, 2)
.AutoSize = False
.Width = 212
.AutoSize = True
End With
With Frame1
.ScrollHeight = LabelText Height + 5
.ScrollTop = 1
End With
If CurrentTopic = 1 Then
NextButton.SetFocus
Elself CurrentTopic = TopicCount Then
PreviousButton.SetFocus
End If
PreviousButton.Enabled = CurrentTopic <> 1
NextButton.Enabled = CurrentTopic <> TopicCount
End Sub

Office

, Office , .
. , Microsoft
, Office XP .
Office , ( )
. . 24.6 Office,
.

626

24.

. 24.6. Office
Office , 24.2. . .
, .

24.2. Office
;
Public Const APPNAME As String = ""
Dim Topic
Dim HelpSheet
Sub ShowHelpI)
Set HelpSheet = ThisWorkbook.Worksheets("")
Application.Assistant.On = True
Topic = 1
With Assistant.NewBalloon
.Heading = " " & Topic & " : " & vbCrLf & _
HelpSheet.Cells(Topic, 1)
.Text = HelpSheet.Cells(Topic, 2)
.Button = msoButtonSetNextClose
.BalloonType = msoBalloonTypeButtons
.Mode = msoModeModeless
.Callback = "ProcessRequest"
.Show
End With
End Sub
no Office.
B a l l o o n ( Office ) H e a d i n g T e x t .
B a l l o o n ,
, .

VI.

627

Mode msoModeModeless,
. Callback
, . ,
Show.
ProcessRequest, 24.3, ,
.

24.3.
no Office
Sub ProcessRequest(bin As Balloon, lbtn As Long, lPriv As Long)
Dim NumTopics As Integer
NumTopics = Application,WorksheetFunction.CountA(HelpSheet. _
Range("A:A"))
Assistant.Animation = msoAnimationCharacterSuccessMajor
Select Case lbtn
Case msoBalloonButtonBack
If Topic <> 1 Then Topic = Topic - 1
Case msoBalloonButtonNext
If Topic <> NumTopics Then Topic = Topic + 1
Case msoBalloonButtonClose
bin.Close
Exit Sub
End Select
With bin
.Close
Select Case Topic
Case 1: .Button - nsoButtonSetNextClose
Case NumTopics: .Button = msoButtonSetBackClose
Case Else: .Button = msoButtonSetBackNextClose
End Select
.Heading = " " & Topic & ": " & vbCrLf & HelpSheet .Cells(Topic, 1)
.Text = KelpSheet.Cells(Topic, 2)
.Show
End With
End Sub
ProcessRequest , S e l e c t Case , , . , , l b t n . , , ,
.

Office, : .
Wtsb- .

628

24.

WinHelp System
HTML Help System
Windows Windows Help System (WinHelp).
HLP , , . Microsoft WinHelp HTML Help.
, Microsoft,
HTML Help.
.

.
, -~
.
, .
,
,
.
, . , -, . ,
RoboHELP
eHelp Corporation, RoboHELP (WinHeip HTML Help).
Web- h t t p : //www. ehelp. com.

WinHelp
. 24.7 ,
WinHelp. , (jump words), . WinHelp
, ( ). , ,
WinHelp .

. 24.7.
WinHelp

VI.

629

WinHelp , HLP
.
(comp . os .ms-windows.programmer . winhelp), . , HLP WinHelp .
HLP, , RTF. . Microsoft Word. ,
Microsoft Help Workshop, Help. Help Workshop Microsoft :
ftp://ftp.microsoft.com/softlito/mslfiles/hcwsetup.exe
Ha Web-
HLP, RTF HPJ ( ), HLP.

HTML Help
, Microsoft HTML Help , . HTML . HTML Help
, , , /
ActiveX, , DHTML (Dynamic HTML),
HTML Help HTML. Help Viewer,
Internet Explorer. , , . ,
,
Internet. ,
HTML Help , Web-.
,
.
WinHelp, ,
, HTML Help. HTML Help Workshop,
Web- Microsoft
: h t t p : / 7 m s d n . m i c r a s Q f t . c o m . / l i b r a r y / t o o l s / h t m l h e l p / c h m / H H I S t a r t . h t m .
HTML Excel.
^.
, .
Web- HTML
Help, , .

630

24.



"" (.. WinHelp HTML Help), : Project Properties
( ) VBA.
VBE Tools^xxx Properties (^ ) ( ). Project Properties ( ) General () ,
( , HLP).
,
. Myf u n c s . h i p . , :
ThisWorkbook.VBProject.HelpFile ~ _
ThisWorkbook.Path & "\Myfuncs.hip"
.

<F1>
.

<F1> Us e r F o r m . , , <F1>.

VBA
VBA , :!
. , <F1> .
, .
1. .
2.

, (
).

3.

VBE <F2> Object Browser.

. Project/Library.
5.

C l a s s e s , .

6- Member (.) .
7. Properties . , Member Options
{ ).
8.

,
. .

VI.

631

Member Options
. ,
.
, VBA,
. M a c r o O p t i o n s . M a c r o O p t i o n s ,
(AddTwo Squared):
Sub S e t O p t i o n s U
1
AddTwo
Application.MacroOptions Macro:="AddTwo", _
D e s c r i p t i o n ; - " ; ", _
HelpFile:=ThisWorkbook.Pazh & "\Myfuncs.hip", _
HelpContextID:=100Q
Squared
Application.MacroOptions Macro:~"Squared", _
Description:=" ", _
HelpFile :=ThisWorkbook. Pa"h & " \Myfuncs . h i p " , __
HelpContextID:=2000
End Sub
'

Web-
.



UserForm,
UserForm . , , .
UserForm.
W h a t s T h i s B u t t o n . True,
UserForm .
,
UserForm .
WhatsThisHelp. True,
-
, .
W h a t s T h i s H e l p True,
W h a t s T h i s B u t t o n True. ,
K h a t s T h i s B u t t o n WhatsThisHelp. . 24.1
-

632

24.

24.1. WhatsThisHetp WhatsThisButton


whatsThisHelp

WhatsThisButton

True

True

;
<F1>

False

He ;
<F1>

False

False

He ;
<F1>


WinHelp HTML Help
VBA - .

Help
Help Application !
. WinHelp HLP, HTML Help CHM.
, .
Help :
Application.Help(helpFile, helpContextID)
.
, Excel. , , .
Myapp.hlp, .
, ,
. : .
Sub ShowHelpContents()
Application.Help ThisWorkbook.Path & "\Myapp,hip"
End Sub
1002.
HTML Help, . chm:
Application.Help ThisWorkbook.Path & "\Myapp.chm", 1002



VBA MsgBox
Help (), vbMsgBoxHelpButton

VI.

633

MsgBox. , . ( )
. , . 24.8:
Sub MsgBoxHelpO
Msg = " ?"
Buttons = vbQuestion + vbYesNo + vbMsgBoxHelpButton
HelpFile = Thisworkbook.Path & "\AppHelp.hlp"
ContextXD = 1002
Ans = MsgBox(Msg, Buttons, , HelpFile, ContextlD)
If Ans = vbYes Then Call CloseDown
End Sub



VBA i n p u t B o x Help (), .
, . 24.9.
Sub ShowlnputBox{)
Msg = " "
Defaultval = 0
HFile = ThisWorkbook.Path &
ContextID = 1002
x = inputBox( _
Prompt:=Msg, _
Default:=DefaultVal, _
HelpFile:=HFile, __
Context:=ContextID)
End Sub

Put: 24.8.

11

\AppHelp.hlp"

. 24.9. Help ()

. (WinHelp HTML Help),


"", Excel .
VI. , .

634

24.

,
. , , . ,
, .


, ,
Excel, .
, Excel.
, , , , ,
,
Excel. , .


,
. .
, ,

, , . , .
(*,XLT) . , .
. 25.1 ,
.
Web- . Excel.



, . , .
.
U s e r F o r m
M u l t i P a g e ,
(. 25.2-25.6).

. 25. /. , 30

S36

25.

. 25.2.

. 25.3.

. 25.4.

. 25.5.

. 25.6,


iwacTepa .

FormMain UserForrn, .

F o r m H e l p U s e r F o r m ,
.

H e l p S h e e t , , .

VI.

637

ModMain VBA, ,
UserForm.
ThisWorkbook Open Bef oreClose Workbook, .



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


Excel,
. , ^.


, Workbook_Open
.
Start/jrtortizationWizard, FormMain.
23 .

FormMain
U s e r F o r m . _ I n i t i a l i z e FormMain .
Value MultiPage 0.
MultiPage,
, .
ComboBox,
.
G e t D e f a u l t s , ( '' " ).

638

25.

. , O p t i o n B u t t o n ,
.

,
. . O p t i o n B u t t o n ,
.

UserForm
F o r m M a i n
C l i c k C h a n g e ,
UserForm.
, MultiPage . MultiPage_change
UserForm
, , . 15 .


.
, U s e r F o r m .
, . , .
, , M u l t i P a g e .

UserForm 24.


. C l i c k .

D a t a l s V a l i d ,
. , T r u e . , D a t a l s V a l i d , , (. 25.7).

. 25.7.
, ,

VI.

639

, , .

( , , , ) .
if,
, .

.
, ,
. .
, . ,
.

, R1C1 { ,
).

, ,
S u b t o t a l .
Excel, .

, , ,
.

Excel , .
, C l e a r O u t l i n e
.

, : , , .

, ,
, .
, .

, S a v e D e f a u l t s
U s e r F o r m . , , ( " " ).


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

640

25.

UserForm. , , , UserForm.
, GetDefaults,
UserPorm.
TextBox, ComboBox, OptionButton. CheckBox SpinButton, VBA G e t S e t t i n g . , G e t S e t t i n g , , . , .
APPNAME , .
Sub GetDefaultsU
Dim c t l As Control
Dim CtrlType As String
For Each c t l In Me.Controls
CtrlType = TypeName(ctl)
If CtrlType = "TextBox" Or _
CtrlType = "ComboBox" Or _
CtrlType = "OptionButton" Or
CtrlType = "CheckBox" Or _
CtrlType = "SpinButton" Then
c t l . V a l u e = VBA.GetSetting _
(APPNAME, "Defaults", ctl.Name, ctl.Value)
End If
Next c t l
End Sub
. 25.8 , .

/W. 25.3.

VI.

641

S a v e D e f a u l t s . VBA S a v e S e t t i n g .
Sub

SaveDefaults
Dim c t l As Control
Dim CtrlType As S t r i n g

For Each c t l In Me.Controls


CtrlType = TypeName(ctl)
If CtrlType = "TextBox" Or _
CtrlType "ComboBox" Or _
CtrlType = "OptionButton" Or _
CtrlType = "CheckBox" Or _
CtrlType = "SpinButton" Then
SaveSetting , "Defaults",
End If
Next c t l
End Sub

ctl.Name,

ctl.Value

G e t S e t t i n g , S a v e S e t t i n g
:
HKEY_CURRENT_USER\Software\VB and VBA Program S e t t i n g s \


, . , , .

, .

, , .
(, ).


, - . ,
. ,
, .

Excel.

, .

U s e r F o r m , . Excel.

642

25.

E n a b l e d .
T e x t B o x S p i n B u t t o n .
.
VBA.
VBA.
.

Excel, ,
. , ( ) . , " ", ,
.
,
, , . .
?
, .
?
, ,
?
, , .
, ?
,
, , , .
? Excel
, .
? , ,
? ,
1?
? .
? .
.
.

VI.

643


, , ,
, .
VI. , . ,
VBA, VBA,
.

644

25.

,
. 26 ,
. 27 VBA .
28 Visual
Basic, , U s e r F o r m . 29
. ,
Excel.


Excel 2002
Excel, .

Excel, Excel Macintosh,
Excel, .
.



.
. ,
. , , 32- ,
Windows XP, 16-
Windows (3.x). , 32 Windows 3.x. ,
, ,
Windows, , MacOS Linux.

,
Excel 2002 Excel Windows Excel
Macintosh. , Excel
, , . , Excel 97. Excel 2000 Excel 2002, Excel 98 Macintosh
, . ,
Excel
, ,
VBA, .

, . Excel. , Excel 2000


: ,
SR-1 , SR-la.

, Excel. ,
, , , Excel 2000 SR-1.
. Excel , ,
. , . , .


. .

.
Excel. , Excel.

. , , Excel, .

32- 16- . Windovs


API 16- Excel (Excel 5), .

Windows Macintosh. ,
.

. ExceJ,
.

, ?!
:
, Excel.
, Excel.
,
Excel, . , .

Microsoft, h t t p : / / s e a r c h , support .microsoft.com, , Excel.

VIL

647

Excel
Excel ,
. , " " ( ).
.
Excel, . Excel 2002 .
Microsoft Excel (*.xls). Excel 2002. Excel 97, Excel 2000 Excel 2002.
Microsoft Excel 5,0/95. , Excel 5
.
Microsoft Excel 97-2000 & 5.0/95. ,
Excel 5 .

Microsoft Excel 4.0(*.x!s). Excel 4 .


.

Microsoft Excel 3.0 (*.xls). Excel 3 .


.
Microsoft Excel 2.1 (*.x!s). Excel 2.1 . .
Microsoft Excel 4 (*.xlw). Excel 4 .
, Excel 5.
VBA F i l e F o r m a t
Workbook, . , ,
:
MsgBox ActivetVorkbook. F i l e F o r m a t
F i l e F o r m a t . , , , True, Excel 5:
MsgBox ActiveWorkbook.FileFormat = xlExcel5
. 26.1 , VBA Excel.

26.1. Excel
Excel

Excel 2.1

xlExcel2

16

Excel 3

xlExcel3

29

Excel 4

xlExcel4Workbook

35

Excel 5

xlExcel5

39

Excel 95/97

xlExcel9795

43

648

26.

. 26.1
Excel

Excel HTML

xlHtml

Excel

xlAddln

44
18

Excel 97/2000/2002

xlWorkbookNormal

-4143


Excel (Excel 2002 ), ,
Excel, .
. ,
, Excel , .
VBA, ,
, .
" ".
Excel 2000, Excel 97 Excel 95
Excel 95, .
Excel 95, U s e r F o r m
. Excel 97.
.
Excel
v e r s i o n A p p l i c a t i o n Excel.
I , .
VBA v a l . True, Excel 2002
{Excel 2002 10).
Function XLlOOrLater{)
XLlOOrLater = Val(Application.Version) >= 10
End Function

,
Windows API
Excel 95 32- . Excel 5 16- . ,
Excel. 16- Excel 16- Windows 3.x, 32-
Windows. 32- Excel 32 Windows.
, VBA Windows API,
32- Windows API , 16-.

649

Excel 5, 16- API,


Excel 95 . Excel 95 ,
32- Windows API, Excel 5.
Excel 5
16-, 32- API. Excel, , .
, API. Excel 5, . D e c l a r e
:
32- API
Declare Function GetSystemMetrics32 Lib "user32" _
Alias "GetSystemMetrics" (ByVal nlndex As Long) As Long
1- API
Declare Function GetSystemMeCricsl6 Lib "user" _
Alias "GetSystemMetrics" {ByVal nlndex As Integer) _
As Integer
32- 16- API. A l i a s . ,
Windows API ( ).
, .
D i s p l a y V i d e o I n f o , , .
Sub DisplayVideoInf{)
Const SM_CXSCREEN =
Const SM_CYSCREEN = 1
'

'

If V a l ( A p p l i c a t i o n . V e r s i o n ) > 5 Then
32- Excel
vidWidth = GetSystemMetrics32iSM_CXSCREEN)
vidHeight = GetSystemMetrics32(SM_CYSCREEN)
Else
16- Excel
vidWidth = GetSystemMetric:sl6(SM_CXSCREEN)
vidHeight = GetSystemMetricsl6(SM^CYSCREEN)
End If

Msg = "The c u r r e n t video mode i s : "


Msg = Msg & vidWidth & " X " i< vidHeight
MsgBox Msg
End Sub
Excel
Val ( A p p l i c a t i o n . Versioi}) > 5.

Mac
Macintosh. ,
Excel Macintosh Excel, . Microsoft Excel

650

26.

. , VBA .
VBA, ,
. OperatingSystem
A p p l i c a t i o n True, Windows
(.. "Win").
Function WindowsOS() As Boolean
u
If Application.OperatingSystem like "*win* Then
WindowsOS = True
Else
WindowsOS = False
End If
End Function
Windows- Mac- Excel .
"" (, ). . , Excel
Macintosh ActiveX. . Excel Macintosh "1904", , ,
4 . Excel Windows
"1900". , Macintosh 1 1 1904 . Windows I 1900 .
Windows API. Excel Macintosh
. ,
, .
, ,
( Macintosh Windows). VBA.
P a t h S e p .
PathSep = A p p l i c a t i o n . P a t h S e p a r a t o r
P a t h S e p
.
, , ,

. ( Excel Windows), . ,
.
Excel
Macintosh: .
,
.


. Excel . Excel:
MsgBox

Application.International(xlCountryCode)

United States/English 1. . 26.2.

VII.

6S1

: 26.2. Excel

30

31

33

34

36

39

42

45

46

47

48
49
55
68

81

82

()

84

86
90

91

351

358

92

866

966

972

982

, , ,
,
. ,
, . .
.
. ,
/ / .

652

26.

, , . , ,
. .


,
, , ,
. , (, , ).
Web- , : , .
, 14.
OptionButton, .
.
. 26.1-26.3 UserForm,
.

Puc. 26. i.

. 26.2.

. 26.3. .

VII.

653

VBA
, VBA . Excel
: Excel Object library VBA Object library. Excel . Excel).

""
, , , ,
, . , :
MsgBox S e l e c t i o n . A d d r e s s

A d d r e s s L o c a l A d d r e s s :
MsgBox S e l e c t i o n . A d d r e s s L o c a l
, (1 R1C1). .
MsgBox Selection.AddressLocal _
(ReferenceStyle:=Application.ReferenceStyle)
"" .
. 26.3 ( , ).
26.3. ""

""

Address
Category
Formula
Name
NumberFormat
Refer

AddressLocal
CategoryLocal
FormulaLocal
NameLocal
NumberFormatLocal
RefersToLocal


, , , .
.
, .
, .
, .

654

26.

I n t e r n a t i o n a l A p p l i c a t i o n . ,
, :
MsgBox Application,International(xlDecimalSeparatorl
45 , I n t e r n a t i o n a l . . 26-4.

24.6. International

xlCountryCode

Microsoft Excel

xlCountrySetting

xlDecimalSeparator

xlThousandsSeparator

xlListSeparator

xlUpperCaseRowLetter

( R I C I J

xlUpperCaseColumnLetter

xlLowerCaseRowLetter

xlLowerCaseColvamLetter

xlLeftBracket

,
([) R l c i

xlRightBracKet

,
{}) R1C1

xl LeftBrace

,
({)

xlRightBrace

,
{})

xlColumnSeparator

xlRowSeparator

xlAlternaceArraySeparator , ,
xlDateSeparator

{/]

xl imeSepar ator

(:)

xlYearcode

()

xlMonthCode

( )

xlDayCode

(d)

xlHourCode

()

xlMinuteCode

( )

xlSecondCode

(s)

xlCurrencyCode

W/.

651

. 24.6

xl General Forma tName

xlCurrencyDigits

xlCurrencyNegative

xlNoncurrencyDigits

xlMonthNameChars

. Windows

xlWeekdayNameChar s

. Windows

xlDateOrder

xl24HourClock

T r u e , 24- . F a l s e ,
12-

xlNonEnglishFunctions

T r u e ,

xlMetric

T r u e , . F a l s e ,

xlCurrencySpaceBefore

T r u e ,

xlCurrencyEefore

T r u e , . F a l s e -

xlCurrencyMinusSign

T r u e ,
. F a l s e ,

xlCurrencyTrailingZeros

T r u e , , s

xlCurrencyLeadingZeros

T r u e , ,

xlMonthLeadingZero

T r u e ,

xlDayLeadingZero

T r u e ,

xl4DigitYears

T r u e , ,
F a l s e ( )

xlMDY

T r u e , -, r a i s e , --

xlTimeLeadingZero

T r u e ,


,
, , . VBA D a t e S e r i a l ,

556

26.

Excel (
).
D a t e S e r i a l S t a r t D a t e . 1 .

Sub WriteDateO
Dim StartDate As Date
StartDate = DateSerial(2001, 2, 15)
Range("Al") = StartDate
End Sub
, ,
. ! , . .

, ,
Excel, Excel Macintosh,
Excel.
, VBA.

W/.

657


VBA

Excel
. , , , .. , Excel , ,
Excel . , 256
. , , . VBA, ,
.


Excel
.
"" VBA. Excel.
F i l e S e a r c h ,
,
. Excel 97 .

F i l e S y s t e m O b j e c t ,
Microsoft. Excel 2000
.

VBA
VBA, , . 27.1.

| 27.1. VBA,

ChDir

ChDrive

Dir

FileCopy

FileDateTime

FileLen

( )

GetAttr

Kill

MkDlr

Name

RmDir

SetAttr

, .


True, . , False.
Dir , . F a l s e .
Function FileExists{fname) As Boolean
If Dir(fname) <> "" Then _
FileExists = True _
Else FileExists False
End Function

F i l e E x i s t s .
, VBA.


True, . F a l s e .
Function PathExists() As Boolean
1
TRUE,
On Error Resume Next
PathExists = GetAttr(pname) And vbDirectory = vbDirectory
End Function

VII.

659


( ) ,
. , ,
.
Sub ListFilesO
Directory = "': Windows\desktop\ "
= 1
1


Cells(r, 1) = "FileName"
Cells(r, 2) = "Size"
Cells(, 3) = "Date/Time"
Range("A1:C1").Font-Bold =

True


f = Dir(Directory, 7)
Do While f <> ""
r=r+ 1
Cells(r, 1) =
Cells(r, 2) = FileLerMDireczory & 1)
Cells(r, 3) = FileDateTime(Directory & f)
'

f = Dir
Loop
End Sub
Ha pwc. 27,1 .

. 27. l.
ListFiJ.es
, D i r .
, ,
, D i r .
Web- .
.

660

27. VBA

D i r . , , Excel, :
f = D i r ( D i r e c t o r y & " * . x l ? " , 7)
. * , x l ? . D i r .
, 7, , (,
), . , .
, , . GetOpenFileName, 12.

FileSearch
F i l e S e a r c h Microsoft Office.
VBA . ,
,
(, * . x l s ) , ,
. Excel 97 .
. 27.2 , F i l e S e a r c h . .

27.2. FileSearch

FileName

, {

FoundFiies

Lookin

S e a r c h S u b F o l d e r s T r u e ,
Execute

NewSearch

F i l e S e a r c h


FileSearch.


( ) ,
. ,
.
Sub

ListFiles2()
Directory =

'


= 1
Cells.ClearContents

":Windows\desktop\"

661

C e l l s ( r , 1) = " "
C e l l s ( , 2) = ""
C e l l s ( , 3) = "/"
Range("Al:C1").Font.Bold = True
= + 1
On Error Resume Next
With A p p l i c a t i o n . F i l e S e a r c h
.Newsearch
.Lookln Directory
.Filename = " * . * "
.SearchSubFolders = False
.Execute
For i = 1 To .FoundFiles.Count
C e l l s ( r , 1) = .FoundFiles(i)
C e l l s { r , 2) = F i l e L e n ( . F o u n d F i l e s ( i ) )
C e l l s ( r , 3) = FileDateTime(.FoundFiles(i))
r = r + 1
Next i
End With
End Sub
FileSearch Windows ( *. LNK}.

Web-
( ).


( )
T r u e , . E x e c u t e
o u n t FoundF i I e s 1, .
Function FileExists2(path, fname) As Boolean
With Application.FileSearch
.NewSearch
.Filename = fname
.Lookln = path
.Execute
If .FoundFiles.Count = 1 Then
F i1eExi s ts 2 = True
Else
FileExists2 = False
End If
End With
End Function
F i l e S e a r c h .

27. VBA

,
* . x l s My Documents , budget.
Li s tBox UserForm.
Sub F i n d F i l e s O
With Application.FileSearch
.NewSearch
.Lookln = "C:\My Documents"
.SearchSubFolders = True
.TextOrProperty = "budget"
.MatchTextExactly = False
.Filename = "*.xls"
.Execute
For i = 1 To .FoundFiles.Count
UserForml.ListBoxl.Addltem .FoundFiles{i)
Next i
End With
UserForml.Show
End Sub

FileSystemObject
FileSystemObject Windows Scripting Host ic . Web- (, VBScript JavaScript)
Excel 2000 .
Windows Scripting Host . , Windows Scripting Host . , , .
FileSystemObject h t t p : //itisdn.
microsoft.com/scripting.


, , ( ),
True, .
Function FileExists3{fname) As Boolean
Set FileSys = CreateObject("Scripting.FileSystemObject")
F i l e E x i s t s 3 = FileSys.FileExists(fname)
End Function
FileSystemObject, FileSys,
F i l e E x i s t s .
Web- ,
{ VBA, FileSearch FileSystemObject) .

VII.

663


, , () True, .
Function PatriExistsB(path) As Boolean
Set FileSys = C r e a t e O b j e c t ( " S c r i p t i n g . F i l e S y s t e m O b j e c t " )
On Error Resume Next
Set FolderObj = F i l e S y s . g e t f o l d e r ( p a t h )
If Err = 0 Then
PathExists3 = True
Else
PathExists3 = False
End If
End Function
F o l d e r ,
F o l d e r O b j . , . , .


F i l e S y s t e m O b j e c t . D r i v e s . . 27.2
, , -.
(), "" , , , ,
.
Windows T o t a l S i z e AvailableSpace
,
. Windows NT, Windows 2000
,
Web- .

. 27.2. ShowDrivelnfo

664

27. VBA

Sub ShowDrivelnfoW
Dim FileSys, Drv
Dim Row As Integer
Set FileSys = CreateObject("Scripting.FileSystemObject")
Cells.Clear
Row = 0
On Error Resume Next
For Each Drv In FileSys.Drives
Row a Row + 1
Cells(Row, 1) - Drv.DriveLetter
Cells(Row, 2) = Drv.IsReady
Select Case Drv.DriveType
Case 0: Cells(Row, 3) = ""
Case 1: Cells(Row, 3) = ""
Case 2: Cells(Row, 3) = ""
Case 3: Cells(Row, 3) = ""
Case 4: Cells(Row, 3) = "CD-ROM"
Case 5: Cells(Row, 3) = "RAM"
End Select
Cells(Row, 4) = Drv.VolumeName
Cells(Row, 5) = Drv.TotalSize
Cells(Row, 6) = Drv.AvailableSpace
Next Drv
End Sub


VBA , .
/ ,
Excel.
.

. . .

. , ( VBA, ).

. .
. VBA .
, VBA
,
. . , . .
" ". "" .
FileSystemobject Textstream, . FileSystemobject Windows Scripting Host. ,
- .

VII.

685


VBA Open ( Open A p p l i c a t i o n )
. , ,
.
Open , .
Open For [Access ] [] _
As [ #]_ [=_]
Open . , ( ).
, ( ).
:
Append , ;
I n p u t , ,
;
O u t p u t , . (
);
B i n a r y ,
;
R a n d o m ,
, _ Open.

( ). Read, W r i t e Read
Write.

( ). : S h a r e d , Lock Read, Lock W r i t e , Lock Head W r i t e .

, , 1 511 ( ). F r e e F i l e ,

_ ( )
( ) .


VBA
.
1. Open.
2. Seek ( ).
3. I n p u t , I n p u t # L i n e I n p u t #.
4. C l o s e .

666

27. VBA



.
1. Open.
2. Seek ( ).
3. W r i t e # P r i n t #.
4. C l o s e .


, VBA, Open. :
Open " m y f i l e . t x t " For I n p u t As #1
#1.
, #2.
Open "another.txt" For Input As #2
Excel
Excel .
- CSV (Comma Separated Values) ( ).
, . Excel
.
- PRN. , ^
.
- TXT (Tab-delimited) ( ).
,
.
^
, .
, Excel .
,
1* , , , .
VBA F r e e F i l e , . . :


. ,
Seek.

VII.

667


VBA .
.

I n p u t .

I n p u t # ; .

L i n e I n p u t # (
/ ).

W r i t e # ,
. ,
/
. , W r i t e #,
I n p u t #.

P r i n t # ,
. , / . , P r i n t #, L i n e I n p u t # I n p u t .


, .


( ).
Sub ImportData(}
Set ImpRng = ActiveCell
Open "c:\windows\desktop\textfile.txt" For Input As #1
r = 0
Do Until EOF(l)
Line Input #1, data
ActiveCell.Offset(r, 0) * data
r = r + 1
Loop
Close #1
End Sub
, . ^
.

27. VBA


,
CSV.
, Write #.
, / . Write #
, .
Data .
, , . , Value 0.
, ( IsEmpty) 0 .
Sub ExportRangeO
Dim Filename As String
Dim NumRows As Long, NumCols As I n t e g e r
Dim r As Long, As Integer
Dim Data
Dim ExpRng As Range
Set ExpRng = Selection
NumCols = ExpRng.Columns.Count
HumRows = ExpRng.Rows.Count
Filename * " c : \ w i n d o w s \ t e x t f i l e . t x t "
Open Filename For Output As #1
For r = 1 To NumRows
For = 1 To HumCols
Data = ExpRng.Cells(r, c)-Value
If IsNumeric(Data) Then Data = Val(Data)
If IsEmpty(ExpRng.Cells(r, c) > Then Data = ""
If <> NumCols Then
Write #1, Data;
Else
Write #1, Data
End If
Next
Next
Close #1
End Sub

Web- .
. 27.3 .

. 27.3. ,
VBA

VII.

66:)


,
. ,
. .
, .
Sub ImportRange()
Dim ImpRng As Range
Dim Filename As String
Dim r As Long, As Integer
Dim txt As String, Char As String * 1
Dim Data
Dim i As integer
Set ImpRng = ActiveCell
On Error Resume Next
Filename = "c:Windows\textfile.txt"
Open Filename For1 Input As #1
If Err <> 0 Then
MsgBox " : " & Filename, vbCritical, ""
Exit Sub
End If
r = 0
=
txt = ""
Application.ScreenUpdating - False
Do Until EOF|1J
Line Input #1, Data
For i = 1 To Len(Data)
Char = Mid(Data, i, 1)
If Char = "," Then '
ActiveCell.Offset(r, c) = txt
= + 1
txt = ""
Elself i - Len(Data) Then '
If Char <> Chr<34) Then txt = txt & Char
ActiveCell.Offset(r, c> ~ txt
txt = ""
Elself Char <> Chr{34) Then
txt = txt & Char
End If
Next i
= 0
r = r + 1
t
Loop
Close #1
Application.ScreenUpdating = True
End Sub
, , : , . , , : #2001-05-12*.
Web- .

670

27.

Excel
, ,
Excel. ,
, Excel.
.
, , .
Private Sub Workbook_Open()
Open Application.Path & "\excelusage.txt" _
For Append As #1
Print #1, "Started " & Now
Close #1
End Sub
, e x c e l u s a g e . t x t . , :
Started 03/09/00 9:27:43
. ,
"Stopped", .
P r i v a t e Sub Workbook_BeforeClose(Cancel As Boolean)
Open A p p l i c a t i o n . P a t h & " \ e x c e l u s a g e . t x t " _
For Append As #1
P r i n t # 1 , "Stopped " & Now
Close #1

End Sub


. F i l t e r F i l e , , ( i n f i l e . t x t )
, , ( o u t p u t . t x t ) .
Sub FilterFile(}
Open "infile.txt" For Input As #1
Open "output.txt" For Output As #2
TextToFind = "January"
Do Until EOF(l)
Line Input #1, data
If InStr(1, data, TextToFind) Then
Print #2, data
End If
Loop
Close
End Sub
Web- .

VII.

671

256
, Excel 256 .
^ , Excel ,
256- ( )!
I m p o r t R a n g e , .
. 256- ,
.
Sub ImportLongLines()
1
: 256
Dim
Dim
Dim
Dim
Dim
Dim
1

ImpRange As Range
r As Long, As Integer
CurrLine As Long
Data As String, Char As String, Txt As String
i As Integer
CurrSheet As Worksheet


Workbooks.Add xlWorksheet
Open ThisWorkbook.Path & "\longfile.txt" For Input As #1
r = 0
=
Set ImpRange * ActiveWorkbook.Sheets(1).Range("Al")
Application.ScreenUpda ting = False

'

672

, { )
CurrLine = CurrLine + 1
Line Input #1, Data
For i = 1 To Len(Data)
Char - Mid(Data, i, 1)
?
If <> 0 And Mod 2 5 6 - 0 Then
Set CurrSheet = ActiveWorkbook.Sheets.Add(after:= _
ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))
Set ImpRange = CurrSheet,Range{"Al")
=
End If
?
If Char - "," Then
ImpRange.Offset(r, c) = Txt
= + 1
Txt = ""
Else

If Char <> Chr(34) Then _
Txt = Txt SL Mid (Data, i, 1)
?
If i = Len(Data) Then
ImpRange.Offset(r, c) = Txt
= + 1

27. VBA

t = "
End If
End If
Next i

=
CurrLine - 1
Set ImpRange = ActiveWorkbook.Sheets(1).Range("Al")
r = r + 1

'

Do Until EOF(l)
Set ImpRange = ActiveWorkbook.Sheets(1).Range("Al")
CurrLine = CurrLine + 1
Line Input #1, Data
Application.StatusEar = " " & CurrLine
For i = 1 To Len(Data)
Char = Mid(Data, i, 1)

?
If 0 And Mod 256 = 0 Then
=
Set ImpRange = ImpRange.Parent.Next.Range("Al")
End If

If Char = ", " Then
ImpRange.Offset(r, c) = Txt
= + 1
Txt = ""
Else

If Char <> Chr(34) Then
Txt = Txt & Mid(Data, i, 1)
?
If i = Len(Data) Then
ImpRange.Offset(r, c) = Txt
= + 1
Txt = "
End If
End If
Next i
=
Set ImpRange = ActiveWorkbook.Sheets(1),Range("Al"}
r = r + 1
Loop

Close #1
Application.ScreenUpdating = True
A p p l i c a t i o n . S t a t u s B a r = False
End Sub
. , .
. , , , .

VII.

673

Web- . , , 100 600


.

HTML

HTML. HTML, , ,
,
.
ExportToHTML.
Sub ExportToHTML()
Dim ws As Worksheet
Dim Filename As Variant
Dim TDOpenTag As String, TDCloseTag As S t r i n g
Dim CellContents As S t r i n g
Dim Rng As Range
Dim r As Long, As Integer
1


Set Rng = Application.Intersect{ActiveSheet.UsedRange,

'


Filename = Application.GetSaveAsFilename( _
InitialFileName:="myrange.htm", _
fileFilter:="HTML F i l e s ( * . h t m ) , *.htm")
If Filename = False Then Exit Sub

'


Open Filename For Output As #1

<TABLE>
P r i n t #1, "<TABLE BORDER=1 CELLPADDING=3>"

'


For = 1 To Rng.Rows.Count
P r i n t #1, "<TR>"
For - 1 To Rng.Columns.Count
TDOpenTag = "<TD ALIGN=RIGHT>"
TDC1os eTag = "</TD>"
If Rng.Cells(r, c).Font.Bold Then
TDOpenTag = TDOpenTag & "<B>"
TDCloseTag = "</B>" & TDCloseTag
End If
If Rng.Cellsfr, c ) . F o n t . I t a l i c Then
TDOpenTag = TDOpenTag & "<l>"
TDCloseTag = "</I>" & TDCloseTag
End If
CellContents - Rng.Cells(r, c).Text
P r i n t #1, TDOpenTag & CellContents & TDCloseTag
Next
P r i n t #1, "</TR>"
Next r

674

Selection}

27. VBA


P r i n t #1, "</TABLE>"


Close #1


MsgBox Rng.Count & " " & Filename
End Sub
Web- .

.
. ,
. , . F o r - N e x t .
HTML ( ) . , ,
.
. 27.4 , . 27.5 , HTML.

Excel ' Web ? , , : "" HTML. , ExportToHTML 70 . 2,7 .

VII.

675

Excel Web ,
15,8 ( ). , ExportToHTML . . , ExportToHTML
: .
ExportToHTML
.

, : VBA, F i l e S e a r c h
F i l e S y s t e m O b j e c t . VBA
.
VBA
VBA.

676

27. VBA


Visual
Basic

,
,
VBA .
VBA
,
VBA, . VBA, , VBA U s e r F o r m .

IDE
IDE OLE Automation Visual
Basic Editor. Visual
Basic Extensibility Library ( ToolsOReferences (^)) , VBE,
IDE.
References
Microsoft Visual Basic for Application Extensibility. VBIDE.
VBIDE ,
VBIDE, , IDE. ,
,
,
, IDE.
OLE 20.
,
.

VBA.
VBA.
.
U s e r F o r m .
Excel 2002
Excel 2002, ,
Excel 2002 . , Microsoft VBA
VBA. , ,
, .

,
Excel ( ^=*). Visual Basic Project
. ,
, ,
VBA. , .
.
VBProject, , .
On E r r o r Resume Next
Set x = ActiveWorkbook.VBProject
If Err <> 0 Then
MsgBox " ."
Exit Sub

End If

He , ,
. . Visual Basic Project.

IDE
. VBE (Visual Basic Environment
Visual Basic). Excel, VBE . IDE .
VBE

VBProject
VBComponent

678

28. Visual Basic

CodeModule
Designer
Property
Reference
Window
CommandBar
windows commandBars, Extensibility Library, Excel. VBProject,
( Excel 2002 " Excel 2002").

VBProjects
V B P r o j e c t .
V B P r o j e c t , , V B P r o j e c t Workbook.
, V B P r o j e c t :
Dim VBP As VBProject
Set VBP = ActiveWorkbook.VBProject
Dim , Microsoft Visual Basic for Application Extensibility.
VBPro j e c t VBA,
( UserForm, , , ).
VBComponents. , V B P r o j e c t
R e f e r e n c e s , , .
VBProjects.
Excel.
VBProjects. VBProject.
.

VBComponents
VBComponents, VBComponents . VBA:
Set VBC = ThisWorkbook.VBProject.VBComponents(1)
Set VBC = ThisWorkbook. VBProject. VBCojnponents ("Modulel )

References
VBA Excel .
1
1
, Tools ^ References ( ^
) (. 28.1). (,
VBA, Excel, OLE Automation, Office Object Library),
.
VBA. R e f e r e n c e s
R e f e r e n c e ,
. ,

VII.

679

Name, D e s c r i p t i o n F u l l P a t h Reference
.
Sub ListReferences()
Dim Ref As Reference
Msg = ""
For Each Ref In ActiveWorkbook.VBProject.References
Msg = Msg & Ref.Name & vbCrLf
Msg = Msg & R e f . D e s c r i p t i o n & vbCrLf
Msg = Msg & R e f . F u l l P a t h & vbCrLf & vbCrLf
Next Ref
MsgBox Msg
End Sub
. 28.2 . .

. 28.1. References () ,

. 28-2.

, Reference, L i s t References VBA Extensibility Library. Ref object, VBA Extensibility


Library .
.
R e f e r e n c e . A d d F r o m F i l e ,
, . AddFromGuid , GUID.
.


showCoinponents, 28.1, VBA :
;
;

680

' .

28. Visual Basic

28.1. VBA
Sub ShowComponents()
Dim VBP As VBProject
Set VBP = ActiveWorkbook.VBProject
NumComponents = VBP.VBComponents.Count
Cells.ClearContents
For i = 1 To NumComponents
1

Cells(i, 1) = VBP.VBComponents(i).Name
1

Select Case VBP.VBComponents(i).Type


Case 1
Cells(i, 2) = ""
Case 2
Cells(i, 2) = " "
Case 3
Cells(i, 2) = " "
Case 100
Cells(i, 2) = " "
End Select
'

Cells(i, 3} = _
VBP.VBComponents(i).CodeModule.CountOfLines
Next i
End Sub
, 28.3 ShowComponents.
VBA , .

. 28.3.
ShowComponents
Web- .
, VBA Extensibility Library.


, , VBA VBA. VBComponent ( E x p o r t , Remove
i m p o r t ) , . , ,
, , . , . , , VBA , .

VII.

681

.
UserBook.xls {Modulel), .
updateUserBook.xls VBA, Modulel UserBook.xls .
BeginUpdate, 28,2, UpdateUserBook. UserBook.xls.
, UserBook.xls . (. 28.4).

- 28.4.

28.2.
Sub UpdateUserBookf)
Filename = "UserBook.xls"
1


On Error Resume Next
Workbooks(Filename).Activate
If Err <> 0 Then
MsgBox Filename " '", vbCxitical
Exit Sub
End If

Msg = " Mcdulel UserBook.XLS


Msg = Msg & " ." & vbCrLf & vbCrLf
Msg = Msg & " ."
If MsgBox(Msg, vblnformation + vbOKCancel) = vbOK Then
Call ReplaceModule
Else
MsgBox " !", v b C r i t i c a l
End If
End Sub
, ReplaceModule.
, 28.3, Modulel, UserBook. x l s ,
UpdateUserBook,

28.3.
Sub ReplaceModule()
'
Modulel
Filename = ThisWorkbook.Path & "Ntempmodxxx.bas"
ThisWorkbook.VBProject.VBComponents("Modulel")
.Export Filename
1

682

Modulel User3oo4.xls

28. Visual Basic

Set VBF = ActiveWorkbook.VBProject


On Error GoTo ErrHandle
With VBP.VBComponents
.Remove VBP.VBComponents f"Modulel")
.Import Filename
End With
1


Kill Filename
MsgBox " .", vblnformation
Exit Sub

ErrHandle:
1
?
MsgBox ".
vbCritical
End Sub

.", _

.
1. M o d u l e l { ). , ,
2. U s e r F o r m . x l s M o d u l e l ( ).
Remove VBComponents.
3. ( )
U s e r B o o k . x l s .
4. .
5. , .
.
( ) Web- .
, ,
.

VBA VBA
, , VBA VBA. A d d S h e e t A n d B u t t o n .
1. .
2.

ConroandBu t t o n .

3. , CommandButton.
4. CommandB u t t o n . C o m m a n d B u t t o n l _ C l i c k .
1 .
28.4 A d d S h e e t A n d B u t t o n .

VII.

683

28.4. ,
;
Sub AddSheetAndButtonO
Dim NewSheet As Worksheet
Dim NewButton As OLEObject
1


Set NewSheet = Sheets.Add


Set NewButton = NewSheet.OLEObjects.Add _
("Forms.CommandButton.1")
With NewButton
.Left = 4
.Top = 4
.Width = 1 0 0
.Height = 24
.Object.Caption = " 1"
End With


Code = "Sub CommandButtonl_Click()" & vbCrLf
Code = Code "
On Error Resume Next" & vbCrLf
Code = Code & "
Sheets(""1"").Activate" & vbCrLf
Code = Code & "
If Err <> 0 Then" & vbCrLf
Code = Code & "
MsgBox "" 1
& vbCrLf
Code = Code & "
End If" & vbCrLf
Code = Code & "End Sub"

With ActiveWorkbook.VBProject. _
VBComponents{NewSheet.Name).CodeModule
NextLine = .CountOfLines + 1
.insertLines NextLine, Code
End With
End Sub
. 28.5 , A d d S h e e t A n d B u t t o n .
VBA . , Code.
. I n s e r t L i n e s
.
N e x t L i n e
. . , ,
O p t i o n E x p l i c i t ,
.
. 28.6 ,
A d d S h e e t A n d B u 1 1 on.

684

28. Visual Basic

. 28.5. ,
CommandButton, VBA

.28.6.
VBA


UserForm
, ( U s e r F o r m , ,
,
,
. VBE, .
UserForm, . 2S.7, 100 CommandButton. See
. , CommandButton .
, .

VBA .

. 28.7.
CommandButton UserForm
VBA

VH.

685

UserForm

U s e r F o r m . U s e r F o r m , . , , . ,
C a p t i o n , . VBE, , U s e r F o r m . IV , U s e r F o r m .
, . VBE. , VBA
U s e r F o r m . , D e s i g n e r U s e r F o r m .
, , CommandButton U s e r F o r m .
, .
R u n T i m e B u t t o n , , . CommandButton
.
U s e r F o r m . CommandButton .
VBE, , .
Sub RunTimeButton{)
1

Dim B u t n As CommandButton
Set Butn = UserForml.Controls.Add("Forms.CommandButton.1")
With B u t n
. C a p t i o n = " "
.Width = 100
.Top * 10
End With
UserForml.Show
End Sub
D e s i g n T i m & B u t t o n . ,
D e s i g n e r , VBComponent. ,
; Add CommandButton.
D e s i g n e r , C o m m a n d B u t t o n U s e r F o r m ,
.
Sub DesignTimeButton()
'

Dim Butn As CommandButton

686

28. Visual Basic

Set Butn = ThisWorkbook.VBProject. _


VBComponerits("UserForml") _
.Designer.Controls.Add Forms.CommandButton.1")
With Butn
Caption = " "
.Width = 120
.Top = 40
End with
End Sub

100
CommandButton
D e s i g n e r
UserForm. 100 CommandButton ( ), C a p t i o n CammandButto:]
100 ( CommandButton).
28.5 A d d l O O E u t t o n s ,

28.5. UserForm, 100


Sub AddlOOButtons()
Dim UFvbc As Object 'VBComponent
Dim CMod As Object '
Dim ctl As Control
Dim cb As CommandButton
Dim n As Integer, As Integer, r As Integer
Dim code As String
Set UFvbc = ThisWorkbook.VBProject.VBComponents("UserForml")
1


For Each ctl In UFvbc.Designer.Controls
UFvbc.Designer.Controls.Remove ctl.Name
Next ctl

VBA
UFvbc.CodeModule.DeleteLines 1, UFvbc.CodeModule.CountOfLines

' 100 CommandButton


n = 1
For r = 1 To 10
For = 1 To 10
Set cb = UFvbc.Designer.Controls.Add("Forms.CommandButton.1")
With cb
.Width = 22
.Height = 2 2
.Left = {c * 26) - 16
.Top = {r * 26) - 16
.Caption =
End With
1


With UFvbc.CodeModule

VII.

687

code = ""
code = code & " P r i v a t e Sub CommandButton" & n & "_Click" & vbCr
code = code & "Msgbox " " T h i s is CommandButton" & n & " " " " & vbcr
code = code & "End Sub"
. I n s e r t L i n e s .CountOfLines + 1, code
End With
n = n + 1
Next
Next r
1
VBA.UserForms.Add("UserFonnl").Show
End Sub
A d d l O O B u t t o n s ,
U s e r F o n n l . . . D e l e t e L i n . e s CodeModule. CoinmandButton F o r - N e x t . .
( CommandButtonl).
P r i v a t e Sub CommandButtonl_Click()
MsgBox " CommandButtonl"
End Sub
,
,
. , .
.

, End Sub:
VBA. UserForms.Add("UserForml") - Show
, , UserForm. VBA UserForm, 100 , ,
. U s e r Forml UserForms, Add.
(, )
. Add Show.
, U s e r F o r m s .


UserForm
VBA
UserForm . ; , .

688

28. Visual Basic


, , , ,
. .
MakeForm .
1. U s e r F o r m .
Add VBComponents.
2. CommandButton U s e r F o r m .
D e s i g n e r .
3.

U s e r F o r m .
( C o m m a n d B u t t o n l _ C l i c k )
.

4.

U s e r F o r m .

5.

U s e r F o r m .


U s e r F o r m , . ,
. , .
UserForm , . .
MakeForm.

28.6. UserForm
3ub MakeForm{)
Dim TempForm As Object 'VBComponent
Dim NewButton As Msforms.CommandButton
Dim Line As Integer
Dim TheForm
Application.VBE.MainWindow.Visible = False
1

UserForm
Set TempForm = ThisWorkbook.VBProject. _
VBComponents.Add(3) 'vbext_ct_MSForm
With TempForm
.Properties("Caption") = " "
.Properties("Width") =2 00
.Properties("Height") = 100
End With

CommandButton
Set NewButton = TempForm.Designer.Controls _
.Add("forms.CommandButton.1")
With NewButton
.Caption = "!"
.Left = 60
.Top = 4 0
End With

VII.

CommandButton
With TempForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub CoiranandButtonl_Click()"
.InsertLines Line + 2, "MsgBox ""!"""
.InsertLines Line + 3, "Unload Me"
.InsertLines Line + 4, "End Sub"
End With
1

UserForm
VBA.UserForms.Add(TempForm.Name).Show

UserForm
ThisWorkbook.VBProject.VBComponents.Remove
End Sub

TempForm

, . 28.8.
. 28.8.
UserForm,
( ,
VBA
, MakeForm,
VBA Extensibility Library, TempForm
o b j e c t ( VBComponent).
.
, VBE,
V i s i b l e F a l s e . ;
.


, .
G e t O p t i o n , U s e r F o r m .
O p t i o n B u t t o n , . ,
O p t i o n B u t t o n , .
28.7.

28.7.

Function GetOption(OpArray, Default, Title)
Dim TempForm As Object 'VBComponent
Dim NewOptionButton As Msforms.OptionButton
Dim NewCommandButtonl As Msforms.CommandButton
Dim NewCommandButton2 As Msforms.CommandButton
Dim i As Integer, TopPos As Integer
Dim MaxWidth As Long
Dim Code As String
'

VBE

28. Visual Basic

Application.VBE.MainWindow.Visible = False
'

UserForm
Set TempForm = _
ThisWorkbook.VBProject.VBComponents.Add{3)
TempForm.Properties{"Width"} = 300

OptionButton
TopPos = 4
MaxWidth = 0
For i = LBound(OpArray) To UBound(OpArray)
Set NewOptionButton = TempForm.Designer.Controls. _
Add("forms.OptionButton.1")
With NewOptionButton
.Width = 300
.Caption = OpArray(i)
.Height = 15
-Left = 8
.Top = TopPos
.Tag = i
.AutoSize = True
If Default =.i Then .Value = True
If .Width > MaxWidth Then MaxWidth = .Width
End With
TopPos = TopPos + 15
Next i

'


Set NewCommandButton1 = TempForm.Designer.Controls. _
Add("forms.CommandButton.1")
With NewComnandButtonl
.Caption = ""
.Height 18
.Width = 44
.Left = MaxWidth + 12
.Top = 6
End With

'

OK
Set NewComraandButton2 = TempForm.Designer.Controls. _
Add("forms.CommandButton.1")
With NewCommandButton2
.Caption - "OK"
.Height = 18
.Width = 4 4
.Left = MaxWidth + 12
.Top = 28
End With

CommandButton
Code
Code
Code
Code
Code
Code
Code
Code

=
=
=
=
m
=
=

""
Code
Code
Code
Code
Code
Code
Code

&
&
&
&
&
&
&

"Sub CommandButtonl_Click{)" & vbCrLf


" GETOPTION_RET_VAL=False" & vbCrLf
" Unload Me" & vbCrLf
"End Sub" & vbCrLf
"Sub CommandButton2_Click()" & vbCrLf
" Dim ctl" & vbCrLf
GETOPTION_RET_VAL = False" & vbCrLf

Vll.

691

Code
Code
Code
Code
Code
Code
Code

=
=
=
=
=
=
=

Code
Code
Code
Code
Code
Code
Code

&
&
&
&
&
&
&

" For Each ctl In Me.Controls" & vbCrLf


"
If TypeNaitie(ctl) = ""OptionButton"" Then" & vbCrLf
"
If ctl Then GETOPTION_RET_VAL = ctl.Tag" & vbCrLf
"
End If" & vbCrLf
" Next ctl" & vbCrLf
" Unload Me" = vbCrLf
"End Sub"

With TempForm.CodeModule
.InsertLines .CountOfLines + 1, Code
End With
'


With TempForm
.Properties{"Caption") = T i t l e
.Properties ("Width") = NewConunandButtonl.Left + _
NewCommandButtonl.Width + 10
If .Properties{"Width") < 160 Then
.Properties("Width") = 160
NewCommandButtonl.Left = 106
NewCommandButton2.Left = 106
End If
.Properties("Height") = TopPos + 2 4
End With

'


VBA.UserForms.Add(TempForm.Name).Show

'


ThisWorlcbook.VBProject .VBComponents .Remove VBComponent: =TempForrn
'

GetOption GETOPTION_RET_VAL
End Function
GetOption , ,
. . UserForm .

GetOption
GetOption .
OpArray , , OptionButton.
Default , OptionButton, UserForm. 0, OptionButton.
Title ,
UserForm.

GetOption
GetOption .
1. VBE,
UserForm .

692

28. Visual Basic

2.

U s e r F o r m TempForm.

3. O p t i o n B u t t o n . , .
Tag .
Tag .
4. CommandButton:
.
5. CommandButton .
6. UserForm:
CommandButton UserForm.
7. U s e r F o r m .
, C o m m a n d B u t t o n l _ C l i c k . , O p t i o n B u t t o n , GETOPTIQN_RET_VAL ( P u b l i c ) .
8. U s e r F o r m .
9.

GETOPTI0N_RET_VAL .
UserForm
VBA Extensibility Library. , { modOptionsForm).
Getoption.

G e t o p t i o n . U s e r F o r m ( : Ops).
Sub TestGetOption()
Dim Ops{1 To 5)
Dim UserOption
On Error Resume Next
Dim x
Set x = ActiveWorkbook.VBProject
If Err <> 0 Then
MsgBox " _
.", vbCritical
On Error GoTo 0
Exit Sub
End If
Ops(1) = ""
Ops(2) = ""
Ops(3) = ""
Ops(4) = ""
Ops(5) - " "
UserOption = GetOption(Ops, 5, " ")
MsgBox Ops(UserOption)
End Sub

VII.

693

UserOption .
, UserOption False.
. 28.9 UserForm, .

28.9. UserFonr.
GetOption
UserForm , , . , Useroption . ,
UserForm .

, GetOption
CommandButton. GetOption
UserForm.
Sub CommandButtonl_Click()
GETOPTION_RET_VAL = False
Unload Me
End Sub
Sub CommandButton2_ClickU
Dim ctl
GETOPTION_RET_VAL = False
For Each ctl In Me.Controls
If TypeName{ctl} = "Op t i onBu11 on" Then
If ctl Then GETOPTION_RET._VAL = ctl. Tag
End If
Next ctl
Unload Me
End Sub
, UserForm , VBE.
UserForm , , ('):
ThisWorkhook.VBProject.VBComponenfcs.Remove
VBComponent j=TempForm


VBA. VBA , VBA, U s e r F o r m .
.

28. Visual Basic

VBA .
Excel 97
. , , .


(class module) 1 VBA, . ,

>. , Excel .
oiacca , , .
(.
15, 18, 19 23).
. "
?". : .
, .
,
.
.
.

, . (
18).

.
. ( 19 23)

Windows API .
, , <NumLock> <CapsLock>. , .

UserForm.
. J5 , CommandButton C l i c k .

, ; . . ,
.

: NumLock
, , . NumLock,
: V a l u e . <NumLock>
Windows API. <NumLock>. API, ( VBA). ? ,
.
VBA. <NumLock>
, V a l u e :
MsgBox NumLock.Value
<NumLock>. ,
<NumLock>:
NumLock.Value = T r u e
, , ,
. VBA , . , . .


VBE I n s e r t s Class Module ( ).
, C l a s s l .
Properties , <F4>. Properties NumLock
(. 29.J).

. 29. /. , NumLock

696

29.

VBA
, Value.
<NumLock>,
Windows API, <NumLock>. .
P r i v a t e Type KeyboardBytes
kbBytefO To 255) As Byte
End Type
Dim kbArray As KeyboardBytes
Private Declare Function GetKeyState _
Lib "user32" (ByVal nVirtKey As Long) As Long
Private Declare Function GetKeyboardState _
Lib "user32" (kbArray As KeyboardBytes) Long
Private Declare Function SetKeyboardState _
Lib "" (kbArray As KeyboardBytes) As Long
Const VK_NUMLOCK = &H90
,
<NumLock>. V a l u e . , V a l u e . , P r o p e r t y Get:
P r o p e r t y Get Value{) As Boolean
Value = GetKeyState(VK_NUMLOCK) And 1 = 1
Snd P r o p e r t y
Property .
"
".
, Windows API G e t K e y S t a t e ,
<NuinLock>. , VBA Value. , VBA, ,
P r o p e r t y Get:
MsgBox NumLock.Value
,
<NumLock>. : .
P r o p e r t y L e t :
Property Let Value(boolVal As Boolean)
GetKeyboardState kbArray
kbArray.kbByte(VK_NUMLQCK) = Abs(boolVal}
SetKeyboardState kbArray
End Property
P r o p e r t y L e t ,
True, F a l s e . VBA, , Value
NumLock True- P r o p e r t y Let:
NumLock.Value = True

VII.

697

NumLock
, N u m L o c k C l a s s ,
. , VBA ( ),
:
Dim NumLock As New NumLockClass
, N u m L o c k C l a s s (..
). , NumLock .
V a l u e NumLock T r u e . <NumLock>.
Sub NumLockOn{)
Dim NumLock As New NumLockClass
NumLock.Value = T r u e
End Sub
, <NumLock> (True , F a l s e ).
Sub GetNumLockState{)
Dim NumLock As New NumLockClass
MsgBox NumLock.Value
End Sub
, <NumLock>.
Sub ToggleNumLock{)
Dim NumLock As New NumLockClass
NumLock.Value = Not NumLock.Value
End Sub
Web-
,.
<CapsLock>.


, V a l u e . . , .


, : , . , C l a s s l , C l a s s 2
.. .


.
. ,
-:

698

29.

.
VBE Auto List Members , . .
, , , , . ,
, P r o p e r t y Get.
P r o p e r t y Get.
Property Get FileNameOnly () As String
FileNameOnly = ""
For i = Len(FullName) To 1 Step -1
Char = Mid(FullName, i, 1)
If Char = "\" Then
Exit Function
Else
FileNameOnly = Char & FileNameOnly
End If
Next i
End Property
, P r o p e r t y Get . , . FileNameOnly. , FullName.
, FullName : \windows\myfile.txt,
my f i l e . t x t . FileNameOnly ,
VBA .
, , :
Property Get ( ) P r o p e r t y Let (
; ). , ,
) P r o p e r t y Get.
.
Property Get SaveAsExcelFile() As Boolean
SaveAsExcelFile = XLFile
End Property
Property Let SaveAsExcelFile(boolVal As Boolean)
XLFile = boolVal
End Property
/
, Property Let
/ Property Set.
, Public,
.
P r o p e r t y Get P r o p e r t y Let,
:
Public SaveAsExcelFile As Boolean
, , P r o p e r t y Let P r o p e r t y Get.
XLFile,
. Property Get

699

. FileSys, SaveAsExcelFile:
MsgBox FileSys.SaveAsExcelFile
, P r o p e r t y Let
. , ( ), SaveAsExcelFile True:
FileSys.SaveAsExcelFile = True
True P r o p e r t y Let, .
XLFile.
.
, .
. , VBA , , .


. Sub Function, . , , .
:
.
VBA,
, :
. XLFile.
, .
Sub SavePileO
If XLFile Then
Ac t i veWorkbook.SaveAs F i1eName:= FName, _
FileFormat:=xlWorkbookNormal
Else
ActiveWorkbook.SaveAs FileName:=FName, _
FileFormat:=xlCSV
End If
End Sub
, ,
, .


: I n i t i a l i z e Terminate.
I n i t i a l i z e , Terminate
. I n i t i a l i z e , .
:

700

29.

Private Sub Class_InitializeO


'

End Sub
Private Sub Class_Terminate()
1

End Sub
( ),
, , .
, N o t h i n g . ,
M y O b j e c t :
S e t MyObject = N o t h i n g

: CSVFile
, , , C S V F i l e C l a s s . .

E x p o r t R a n g e . , CSV.

I m p o r t R a n g e . , ,
CSV.

I m p o r t CSV,
CSVFileName. , I m p o r t R a n g e .

E x p o r t ,
E x p o r t R a n g e , CSV, CSVFileName.


,
. C S V F i l e C l a s s , . :
Private RangeToExport As Range
Private ImportToCell As Range
R a n g e T o E x p o r t Range, .
I m p o r t T o C e l l Range, , CSV. P r o p e r t y Get P r o p e r t y L e t (. ).


C S V F i l e C l a s s 29.1.
P r o p e r t y G e t , P r o p e r t y
L e t .

VII.

701

29.1. CSVFileClass
Property Get ExportRange () As Rar.ge
Set ExportRange = RangeToExport
End Property
Property Let ExportRange(rng As Range)
Set Range To Expo r-t = rng
End Property
Property Get ImportRange() As Range
Set ImportRange = ItnportToCell
End Property
Property Let ImportRange(rng As Range)
Set ImportToCell = rng
End Property


C S V F i l e C l a s s ,
. .

Export
E x p o r t , 29.2,
E x p o r t . : , .
. , E x p o r t R a n g e .
R a n g e T o E x p o r t .
.

! 29.2.

Sub Export(CSVFileName)
1
CSV
Dim ExpBook As Workbook
If RangeToExport Is Nothing Then
MsgE-ox " "
Exit Sub
End If
On Error GoTo ErrHandle
Application.ScreenUpdating = False
Set ExpBook = Workbooks.Add(xlWorksheet)
RangeToExport.Copy
Application.DisplayAlerts - False
With ExpBook
.Sheets(1).Paste
.SaveAs FileName:=CSVFileName, FileFormat:=xlCSV
.Close SaveChanges:=False
End With

702

29.

Appli ca t i on.Cu tCopyMode = False


Application.ScreenUpdating = True
Application.DisplayAlerts = True
Exit Sub
ErrHandle:
ExpBook.Close SaveChanges:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox " " & Err & vbCrLf & vbCrLf & Error(Err), _
vbCritical, " "
End Sub
E x p o r t R a n g e T o E x p o r t .
CSV, . , . (,
), E r r H a n d l e ,
.

import
I m p o r t , 29.3, CSV, CSVFileName. , i m p o r t T o C e l l . I m p o r t R a n g e .
. E x p o r t , I m p o r t .

29.3.

Import{CSVFileName)
'
CSV
Dim CSVFile As Workbook
If ImportToCell Is Nothing Then
MsgBox " "
Exit Sub
End If
If CSVFileName = "" Then
MsgBox "He "
Exit Sub
End If
On Error GoTo ErrHandle
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open CSVFileName
Set CSVFile = ActiveWorkbook
ActiveSheet.UsedRange.Copy Destination:=ImportToCell
CSVFile.Close SaveChanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True

Vil.

703

Exit Sub
ErrHandle:
CSVFile.Close SaveChanges:=Palse
Application.ScreenUpdating * True
Application.DisplayAlerts = True
MsgBox " " & Err & vbCrLf & vbCrLf & Error(ErrJ, _
vbCritical, " "
End Sub

CSVFileClass
C S V F i l e C l a s s VBA, , C S V F i l e C l a s s .
.
Dim CSVFile As New C S V F i l e C l a s s
, , .
Dim S e t .
Dim CSVFile As CSVFileClass
'

Set CSVFile = New CSVFile

-<

Dim S e t , , S e t .
: , .
, ,
. , S e t
.
,
, .
VBE Auto List Members .
, .

CSV, t e m p . c s v . , .
Sub ExportARange()
Dim CSVFile As New CSVFileClass
With CSVFile
.ExportRange = ActiveWindow.RangeSelection
.Export CSVFileName:=ThisWorkbook.Path & "\temp.csv"
End With
End Sub
w i t h - E n d W i t h . ,
:
Sub ExportARange()
Dim CSVFile As New CSVFileClass
CSVFile.ExportRange = ActiveWindow.RangeSelection
CSVFile.Export CSVFileName:=ThisWorkbook.Path & "\temp.csv"
End Sub

704

29.

CSV.
, .
Sub ImportAFileO
Dim CSVFile As New CSVFileClass
With CSVFile
On Error Resume Next
.ImporcRange = ActiveCell
.Import CSVFileKame:=ThisWorkbook.Path & "\temp.csv"
End With
If Err <> 0 Then _
MsgBox " " & ThisWorkbook.Path & "\temp.csv"
End Sub
VBA . CSVFileClass:
Sub Export3Files()
Dim CSVFile(1 To 3) As New CSVFileClass
CSVFile(l)-ExportRange = Range{"Al:A20")
CSVFile(2).EKportRange = Range("Bl:B20")
CSVFile(3).ExportRange = Range{"Cl:C20")
For i = 1 To 3
CSVFile(i).Export CSVFileName:="File" & i & ".csv"
Next i<
End Sub

,
, . .
.

VII.

70S


Excel

, "" Internet,
., FAQ (). (frequently asked questions} (
), . , , , .
, .
,
VBA, , : Excel 97 .
,
. ( ) .

m i c r o s o f t . p u b l i c . e x c e l . *.

comp. a p p s . s p r e a d s h e e t .

Microsoft.

, .
.

. ,
.
, , , .

.
.

Excel.

Visual Basic.

, , .

VBA.

CommandBar,

, . , .
, , ,
.

Excel
Excel ?
Excel , XLM. VBA
Excel 5, XLM.
XLM , VBA.


Excel 4. XLM?
. Excel 97, VBA.
, Excel, ,
.

Excel 97 ,
XLM?
.

, Excel 4
VBA. ?
. , , . . Excel XLM, , . , Excel.

VII.

707

VBA XLM Excel 4?


. XLM RUN. ,
T e s t , M o d u l e l B o o k l . x l s :
=RUN(Bookl.xls ! M o d u l e l . T e s t )

Lotus
1-2-3 Quattro Pro VBA?
. Excel .

VBA?
Internet VBA.
h t t p : / /www. j - w a l k , c o m / s s /

Excel
-?
, .

,
?
. ,
. , , 1 ^ .
.
, . ,
. , , . .

,
?
. (, L i s t E n t r i e s ) .
=ListEntries . ,
, .

Application.Calculation
. ,
.
C a l c u l a t i o n A p p l i c a t i o n . ,
.
. ] 2000 W o r k s h e e t ,
E n a b l e C a l c u l a t i o n . F a l s e ,
, .
T r u e .

?
.
, Microsoft
.

708

30. Excel

?
, .

?
Excel 2002, . Excel
.

?
, Excel. Windows
. :
. .
. .

,
?
. Windows .
: . ToolTip
( ). . .

Excel 2002?
Microsoft . { ) . , , Excel 2002,
.

Excel?
. WAV MIDI, Windows API ( 11).
Excel 2002, to Speech.
(, ):
A p p l i c a t i o n . S p e e c h . S p e a k ( " H e l l o " & Application.UserName)

Excel
. , .
?
, . .
, . ,
. , .
Excel 5/95,
, .
, .

VII.

709

^ ^ .
. , , , #!. , "" .


?
Excel 2002,
. ,
; . , , . :
, (
).
Excel VBA
WorkbookBef o r e P r i n t . ,
,
:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
For Each sht In ThisWorkbook.Sheets
sht.PageSetup.LeftHeader = ThisWorkbook.FullName
Next sht
End Sub

, "" ,
.
Excel?
" " (Easter Eggs). Excel Easter Egg.
Excel 2000, .
1. Excel.
2. <F5>.
3. X2000:L2000 <Enter>.
4. 2000.
5. <>, .
6. <Ctrl> <Shift> . , .
Excel 2000.

Excel 2002?
Excel 2002 ( ). Easter Egg Excel h t t p : //www. j - w a l k . c o m / s s / e x c e l / e a s t e r e g g . h t m .
Microsoft , Easter Egg, ,
.

710

30. Excel

Visual Basic
Excel 95 VBA ,
Excel 97, .
, , , VBE. <Alt+Fll> Excel VBE.

VBA
?
. . ,
, , . , .

Excel 95 "" ,
,
.
?
. , Excel 97. , , .

,
. ?
. () , Excel .
. P e r s o n a l . x l s \ X L S t a r t .

. ?
P e r s o n a l . x l s ,
.

, Excel 97 ,
Project VBE
Sheeti 1111111111(Sheet1 (9)). ?
" " W o r k s h e e t . , ,
"". , 32 Excel .
Properties VBE. Excel 2000.

VII.

711

VBA .
?
, .
Web. Excel password. , Excel , .

?
. VBA .
,
.

Option
Explicit. ?
O p t i o n E x p l i c i t , , ,
( ). , , VBE Tools^Options (^).
Editor () Require Variable Declaration ( ).
VBA.

VBA ?
?
VBA : ,
, , .. , , , .. ToolS^Options ( Editor Format ( ))
VBE.

VBA VBA.
?
. M o d u l e l .
With ActiveWorkbook.VBProject
.VBComponents.Remove .VBComponents("Modulel")
End With
Excel 2002. .

Excel 2000, VBA .


Excel 2002, .
?
Excel 2002 : Visual Basic Project. . , ^ 1 ^

.

772

30. Excel

?
"
", .
VBA
. .


. !
?
, . , VBA. .

, UserlnterfaceOnly
.
.
ActiveSheet.Protect UserlnterfaceOnly:=True
,
. , .
, ,
U s e r l n t e r f a c e O n l y .

, ?
VBE , . VBA, .
.
.

(&) BVBA.
,
.
, VBA , . , .

VBA
().
: .

Excel 95 VBA "


",
. Excel 97,
VBE. ?
, , . Excel 91
x l V e r y H i d d e n .

VII.

713

VBA
, XLS .
?
Excel .
, .
,
.

XLS
.
. ?
, Break on All Errors ( ). Options ( General ()) VBE. , VBA.
, XLA.

VBA ?
. (macro) "" . .

?
VBA, . (, ) , , F u n c t i o n . Sub.

?
, , .
VBA . , , . . Dim, P u b l i c P r i v a t e .
.


?
: ( ). ,
.
Dim X As Variant
X = Array(30, 40, 50)

714

30. Excel

, ( ). , 3- :

Dim X (0 2) As Variant
, , , ,
.

?
VBA ,
. , MyVar, i n t e g e r .
%:
Dim MyVar%
, VBA.

Integer
Long

%
&

Single
Double

!
#

Currency
String

@
$

VBA,
. ,
. ?
VBA, , . , "" , ,
. ,
.
.
VBA MsgBox. . .

,
, . ,
, 0, .
?
, . Excel
. . ^ .

,
.
C h a n g e . - , C h a n g e .
S h e e t () W o r k s h e e t _ C h a n g e , C h a n g e .

VII.

775

?
!
.

(Sub Workbook.Open),
. ?
, , .
ThisWorkbook ().

S h e e t (), VBE Project.


.
?
, .
, 19.

Excel. VBA
?
. , VBA , . .

Eqv

T r u e , T r u e F a l s e

imp

T-s

Like

T r u e , T r u e

, ?
Run A p p l i c a t i o n . Macrol,
Personal.xls:
Run "Personal.xls!Macrol"

VBA
,
.
?
. ,
Excel. , .

716

30. Excel

, , , .
, Tools^References VBE.

, ,
Excel. , Excel
.
?
? ,
\XLStart. ,
Workbook_Open .

,
Workbook_Open.
?
. <Shift> ^. Workbook_Bef oreClose, <Shift>
. <Shift>
.

VBA
?
VBA ,
XLM. , XLM VBA. 1 S h e e t l m y f i l e . x l s , -. \f i l e s .
MsgBox ExecuteExcel4Macro {" ' : \ f i l e s \ [myf i l e . x l s ] S h e e t l ' .' RlCl")
, R1C1.


VBA?
:
ActiveWorkbook.Close

SaveChanges:=False

Saved Workbook True.



ActiveWorkbook.Saved = True
,
.
Excel :
Application.DisplayAlerts = False

,
?
OnTime A p p l i c a t i o n . , .

VII.

717

, OnTime,
.

, ?
P r i v a t e :
P r i v a t e Sub MyMacro()
:
Sub MyMacro

(Optional FakeArg as I n t e g e r )

GIF?
.
1 GIF M y c h a r t . g i f .
Set CurrentChart = Sheets (">11" 1 .ChartObjects (1) .Chart
Fname = ThisWorkbook.Path & "\Mycnart.gif"
CurrentChart.Export Filename:-Fname, FilterName:="GIF"

VBA
VBA? ,
?
. : , ().
. , . , . , .
. P u b l i c .

.


.
?
, , . ^
. , .
. .


?
, .

718

30. Excel


.
?
VBA.
MyFunc 1 ( ):
Application.MacroOptions Macro:="MyFunc", Category:=1
.

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

( )

( )
( )
( )
DDE/External ( )
( )
( ,
)

?
XLM. , .

,
. ,
(#)?
MyFunction,
, :
MyFunction = CVErr(xlErrValue)
xlErrValue . .

VII,

719

,
?
<Ctrl+Alt+F9>.

Excel
VBA?
. Excel
W o r k s h e e t F u n c t i o n A p p l i c a t i o n . , POWER ()
, :
Ans = A p p l i c a t i o n . W o r k s h e e t F u n c t i o n . P o w e r ( 5 , 3)
5 , (125).
VBA , . , VBA (Sgr),
SQRT VEiA .

Excel 95 WorksheetFunction. ,
Excel 2002 Excel 95?
. W o r k s h e e t F u n c t i o n . :
Ans = Application.WorksheetFunction.Power(5, 3)
Ans = Application.Power(5, 3)

VBA ?
, . Excel ^ Analisys Toolpak VBA.
VBA Tools^References (^). a t p v b a e n . x l s , .
. , CONVERT , 5000 :
MsgBox

CONVERT(5000,

"m",

" m i " )

?
.
, , . v b C r ,
.
MsgBox " H e l l o " & vbCr & A p p l i c a t i o n . U s e r N a m e

, ,
.
Excel, ?
.
.

720

30. Excel

.
,
?
.
Object Browser, VBE. <F2>
Object Browser Excel Libraries;/
Workbooks ( )- Classes () () Excel. Members
().
, .
, :
Range("A1").

Range. , Tools ^ O p t i o n s , Editor Auto List Members.
VBA . ,
. Immediate VBE <F1>. , .

?
? ?
, . . ,
W o r k s h e e t s , W o r k s h e e t . : W o r k s h e e t s ( I ) Works h e e t Workbooks. , , , W o r k s h e e t s (" 1 " ) .
. F o r E a c h - N e x t .

VBA ,
"subscript out of range". .
?
. , ,
, M y S h e e t :
Set X = ActiveWorkbook.Worksheets("MySheet")

?
,
VBA . 1 ,
2 : D50:
W o r k s h e e t s ( " 1 " ) . S c r o l l A r e a = "B2:D50"

VII.

721

,
Worksheets (-"1") . S c r o l l A r e a = ""
, S c r o l l A r e a .
,
. Workbook_Open.

Select
Application.Goto?
S e l e c t Range .
A p p l i c a t i o n . G o t o
. A p p l i c a t i o n . G o t o , . , Goto ,
.

?
A c t i v a t e S e l e c t .
. ,
1: . . , , .. .
Range("").Activate
, 1 : . , :
Range("").Select


?
.
, ( ):
On Error Resume Next
Cells.SpecialCells(xlCellTypeConstants, 23).ClearContents
On E r r o r Resume Next
, , .

, VBA ,
. , ,
?
c e l l s .
{.. L5):
Cells{5, 12)-Select

VBA Excel?
<=> , Excel ,
!
Excel
Application.Quit

722

30. Excel

?
,
:
Application.ScreenUpdating = False

VBA
?
, :
n
Range<"D14:G20 ).Select
ActiveWorkbook.Names.Add Name:="inputArea", _
RefersToRlCl: ="=1!R14C4:R2 0C7"
:
Sheets("1").Range("D14:G20").Name = "inputArea"

, ?
Name Name, Range.
, . ,
F a l s e .
Funct ion RangeName(rng) As Variant
On Error Resume Next
RangeName = rng.Name.Name
If Err <> 0 Then RangeName = False
End Function

,
?
. :
ActiveSheet.PrintPreview EnableChanges:=False
EnableChanges P r i n t P r e v i e w
, Object Browser.


?

S t a t u s B a r A p p l i c a t i o n .
:
Application.StatusBar = " " & FileNum
:
Application-StatusBar = False

VII.

723

VBA,
. Select.
?
. ,
.
VBA, .
S e l e c t . :
Range("1").Select
Selection.Copy
Range{"Bl").Select
ActiveSheet.Paste
, .
Range{"Al")-Copy

Range("Bl")

, 1 S e l e c t .

VBA. ,

Range.Sort?
VBA . , . ,
VBA.
VBA .

,
, .
, ?
VBA TypeName,
S e l e c t i o n . .
If TypeNarae(Selection) <> "Range" Then
MsgBox " !"
Exit Sub
End If
R a n g e S e l e c t i o n , R a n g e , .
,
. Window, Workbook. :
MsgBox A c t i v e W i n d o w . R a n g e S e l e c t i o n . A d d r e s s

VBA ,
. Selection.Rows.Count ,
. ?
. C o u n t (, ).

724

30. Excel

, VBA .
S e l e c t i o n . A r e a . C o u n t , .
, NumRows.
NumRows = 0
For Each areaCounter In Selection.Areas
NumRows = NumRows + areaCounter.Rows.Count
Next areaCounter
, .

Excel .
?
. .
Counter = GetSetting("XYZ Corp", "InvoiceNum", "Count",
Counter = Counter + 1
SaveSetting "XYZ Corp", "InvoiceNum", "Count", Counter

0)

, C o u n t e r . . C o u n t e r
.

Excel,

?
, .

Excel,
? ,
,
.
Excel:
Application.DisplayAlerts = False

VBA
? ,
<1+8+ > <Ctrl+Shift+CTpenKa
> ,
?
<1+5+ > VBA
Selection.End(xlDown).Select
: xlToLeft, xlToRight
xlup.

W/.

725

?
.
MsgBox

ActiveSheet.Range("A65536").End(xlUp).Address

, 65536 .
MsgBox ActiveSheet.Cells(Rows-Count,

11.End(xlUp).Address

, 65536
!
,
.
With ActiveSheet.Cells{Rows.Count, 1)
If IsEmptyt.Value) Then
MsgBox .End(xlUp).Address
Else
MsgBox .Address
End If
End With

VBA ,
.
?
. S e t .
.
Dim MyRange as Range
Set MyRange = ThisWorkbook.Worksheets{"1"),Range("Al")
S e t R a n g e MyRange.
, :
MyRange.Value = 10
, .

, ,
?
. Dim. . ,
, ReDim, . ReDim P r e s e r v e .

,
?
F a l s e S e l e c t . , .
Sub S e l e c t C h a r t S h e e t s O
Dim cht As Chart

726

$0. Excel

If Charts.Count = 0 Then Exit Sub


Charts(1).Select
For Each cht In Charts
cht.Select False
Next cht
End Sub


?
, . ,
VBA , . .
^ ,
OnUndo .
, , , =>. :
A p p l i c a t i o n . O n U n d o " " , "MyUndoMacro"

Lotus 1-2-3,
.
. VBA?
Excel ,
Excel i n p u t B o x ,
. , , .
, 1.
UserVal = Application.InputBox(prompt:=" ", :=1)
If UserVal <> False Then Range("Al") = UserVal

VBA InputBox,
Application. ?
. Excel I n p u t B o x , .
I n p u t B o x ( ). , .

RGB
. ?
, . Excel 56 (
56 ). RGB . Excel
.

VBA, .
, (") .
?
, VBA
1.
=(1="";;)

VII.

727

:
R a n g e ( " B l " ) . F o r m u l a = "=IF(Al="YeE",TRUE,FALSE)"
. :
R a n g e ( " B 1 " ) . F o r m u l a = " = I F ( A l = " " Y e s " ' \ TRUE,FALSE)"
VBA C h r 34.
. .
Range("Bl").Formula =
"=IF(A1=" & Chr(34J & "Yes" & Chr(34J & " ,TRUE,FALSE)"

, .
?
, VBA 0 . , 1, VBA:
Option Base I
, . :
Dim Months(I To 12) As S t r i n g

VBA .
?
, ,
. o p t i o n E x p l i c i t , . Excel
, . With-End
With , . , A p p l i c a t i o n . S c r e e n t l p d a t i n g =
F a l s e . , , , .

Windows API.
Windows Excel?
. Excel 2002. ,
Hwnd A p p l i c a t i o n .


,
UserForm .
?
. VBA MsgBox I n p u t B o x . , Excel I n p u t B o x .

728

30. Excel

UserForm 12
CommandButton. ,
?
, CommandButton C l i c k . C o m m a n d B u t t o n _ c l i c k .
(. 15).

UserForm?
.
, GIF
GIF I m a g e .

X UserForm?


U s e r F o r m Windows API.
. U s e r F o r r i _
Q u e r y C l o s e , U s e r F o r m .
>
.
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As integer)
If CloseMode = vbFormControlMenu Then
MsgBox " ."
Cancel = True
End If
End Sub

UserForm,
ControlSource.
?
, ,
.
, ^ ,
.


UserForm? Visual Basic 6.0,
, VBA.
, C o n t r o l .
, CommandButton.

P r i v a t e Sub UserForm_Initialize()
Dim Buttons () As CoiwnandButton
Cnt = 0
For Each Ctl In UserForml.Controls

VII.

729

If TypeName(Ctl) = "CommandButton" Then


Cnt = Cnt + 1
ReDim Preserve Buttons(1 To Cnt)
Set Buttons (Cnt) = Ct:l
End If
Next Ctl
End Sub

UserForm
?
. H i d e U s e r F o r m , .
U n l o a d , "" ( T e r m i n a t e U s e r F o r m ) U s e r F o r m .

UserForm
?
U s e r F o r m .
, . Excel 2000, U s e r F o r m .
Show v b M o d e l e s s . :
UserFormi.Show

vbModeless

Excel ,
UserFormi .Show vbModeless.
Excel 2002, Excel 97?
, Excel ,
. .
Sub ShowUserForm()
If Val {Application .Version)
ShowModelessForm
Else
UserFonnl. Show
End If
End Sub

>== 9 Then

Sub ShowModelessForm ()
Dim frm As Object
Set frm = UserFormi
frm.Show 0
' vbModeless
End Sub
ShowModelessForm Excel 97,
.

,
. ?
U s e r F o r m . 15
, , .

730

30. Excel

Excel,
UserForm?
, .
. ^. U s e r F o r m I m a g e . <F4> Properties. P i c t u r e
<Ctrl+V>, I m a g e .
, A u t o S i z e T r u e .

UserForm,
?
U s e r F o r m .
VBA G e t O p e n F i l e n a m e . ,
, .

Lotus 1-2-3 Windows Quattro Pro


Windows, .

Excel?
, .


ListBox.
.
?
, Courier
New. L i s t B o x
( 14).


Excel VBA?
( ) Excel
A p p l i c a t i o n . D i a l o g s . , , :
Application.Dialogs(xlDialogFormatNumber).Show
, , Object Browser. VBE <F2>,
Excel x l D i a l o g . , .

, ,
. ?
D i a l o g s , . , ( x l D i a l o g C h a r t T y p e ) ,
| , .

VII,

731

UserForm
.
?
. UserForm, ,
. File^Export File (=*
) . 1
, File^lmport File ( * ).

UserForm
?
. , , , C a p t i o n .

VBA,
.
.
Excel 2000. Excel 2000
P r T o F i l e N a m e P r i n t o u t .
:
ActiveSheet.Printout PrintToFile:=True, _
PrToFileName:="test.prn"

UserForm,
. ?
, UserForm,
, .
UserForm, .

,
, ?
, , ? , Zoom U s e r F o r m . , 640x480.

UserForm,

?
. Ref E d i t . 14.


UserForm?
. L e f t
UserForm. , S t a r t U p P o s i t i o n
0.

732

30. Excel

Excel 5/95?
. .
Excel 5.O. ,
Excel 5/95.

Excel?
Excel .
Excel ,
.

Microsoft Office Update.

,
.


Web-.

?
^
^. ^ , , ^, VBA.


.
?
^ ,
. .
, .
. .

, ,
,
. ?
,
Excel.
, .
, .
Excel .

733

?
=> . Microsoft Excel (*.xla).

,

.
, .

?
! , ,
. ,
. .

:
XLS XLA?
Excel 97 . Excel 97,
. .

?
VBE ( <Alt+Fll>) I s A d d l n
T h i s W o r k b o o k F a l s e . ,
I s A d d l n T r u e .

XLS XLA,
XLS? XLA? ,
?
, . VBA "" .
XLS, XLA. XLA VBA, .

?
VBE Tools^xxxx Properties (^ )
( ). Protection () Lock project from viewing ( ), .

? ,
XLA, ,
?
.
. Excel , . , .

734

30. Excel

CommandBar
.
, , ,
<Shift>?
. Windows API. 11,

Excel 95 , Excel 97
. ?
Excel 97, Excel .
CommandBar. Excei, ( 1 *).

,
Excel 95?
, Excel 95.


.
?
VBA, , VB , .

, Faceld
.
Faceld ?
Microsoft , , F a c e l d . 22 .

,
Excel . Excel
?
Excel , , .
VBA. " "
. , VBA,
.

VII.

735

Excel.
?
. :
Sub ResetAllToolbarsO
For Each tb In CoimandBars
If tb.Type = msoBarTypeWcrmal Then
If t b . B u i l t l n Then tb.Reset
End If
Next tb
End Sub
, , , .


.
WorkbookActivate WorkbookDeactivate.
, ,
, , , , .


?
BeginGroup , , True.

?
, , S t a t e .
My Item.
CommandBars(l).Commands("MyMenu"). _
Commands("My I t e m " ) . S t a t e = msoButtonDown
,
S t a t e msoButtonUp.


. Excel
^
. .

?
.
Sub DisableAllShortcutMenusO
Dim cb As CommandBar
For Each cb In CommandBars
If cb.Type = msoBarTypePopup Then _
cb.Enabled = False
Next cb
End Sub

73S

30. Excel

,
?
. :
CommandBars("Toolbar L i s t " ) . E n a b l e d = False

CommandBarsC'Toolbar List").Enabled = False,


!
Excel 97. Excel 97
SR-1.

VII.

737


,
Excel

, ,
, . ,
, . ,
, . , ,
Excel.
.

Microsoft.

Web-.


Microsoft
" " .
. , Microsoft. Microsoft .


, ^ . .
,
, Microsoft.
.

,
. ,
( ). ,
, .
URL,
, Internet , . Wsb ( ,
m i c r o s o f t . c o m ) . , URL, ,
. URL
, , , ,
.
, , ,
. .

Microsoft Knowledge Base


Microsoft Knowledge Base
( Microsoft). Microsoft
, , , , ..
Internet . h t t p : / / s u p p o r t . m i c r o s o f t .com/,

Microsoft Excel
Microsoft Excel h t t p : //www.microsoft.coxa/
office/excel/.

Microsoft Office
,
Office ( Excel),
: h t t p : //of i c e u p d a t e . m i c r o s o f t . com/.


Usernet Internet,
, . , , ,
, ( , , ). ,
, 24 (, ,
).

739

Internet, , .
Microsoft Outlook Express . - Office 2002.

( Internet ), microsoft . p u b l i c . * : http://communities.microsoft.com/newsgroups/.

,

, ,
. a p p s . s p r e a d s h e e t . , 90% Excel. ? "" Microsoft.

Microsoft
Microsoft , Excel
. ISP Microsoft, , Microsoft. msnews . m i c r o s o f t .com.
. A.I , .
. 1. Microsoft, Excel

microsoft . p u b l i c , e x c e l , programming Excel XLM


microsoft . p u b l i c . e x c e l . 123guattro Lotus 1 -2-3 Quattro Pro
Excel

microsoft . p u b l i c . excel .worksheet.


functions
microsoft.public.excel .charting
microsoft, p u b l i c , e x c e l , p r i n t i n g
microsoft. p u b l i c . e x c e l . queryOAo


Excel
Excel
Microsoft Query Data Access Objects
(DAO) Excel

microsoft, p u b l i c . excel .datamap


Excel Data Map
microsoft.public.excel.crashesGPFs General Protection
Fault

microsoft . p u b l i c , excel, misc


microsoft. p u b l i c . e x c e l . l i n k s
microsoft . p u b l i c . excel .macintosh
microsoft. p u b l i c . e x c e l . i n t e r o p o l e

740

,
Excel
, Excel Macintosh
OLE, DDE

. , Excel

. A.I

microsoft-public.excel.setup

Excel

microsoft. public. excel. templates

Spreadsheet Solutions XLT

microsoft .public .excel. sdk

, Excel Software Development Kit


, . , .
:
http://groups.google.com/
Deja.com.
, Google.


1. , . FAQ ( ) Google ( " " ).
2. . "!" " Excel"
, " VBA Excel 2002".
3. ,
.
Excel.
4. , .
5. , , .
,
.
6. , ,
7. , , .
8. . .
9. .
10. HTML.
11. , "", . , ?
? , ListBox, UserForm.

741

: Excel, ListBox UserForm.


Google ,
. ,
, .

Web-
Internet , Excel.
, .

Spreadsheet Page
Web- . ,
, . , , Easter Eggs a Excel,
, . ,
. h t t p : / / j - w a l k . c o m / s s /
, , ,
. (, !)

Web- (Chip Pearson),


Excel
Pearson Software Consulting, VBA, .
h t t p : //www. cpearson.com.

Web- (Stephen Bullen),


Excel
Excel .
Business Modelling Solutions, Ltd.
VBA Excel, ", '1. h t t p : //www.bmsltd.co.uk/excel/.

,

. , . .apps. spreadsheet h t t p : //www. faqs.
o r g / f a g s / s p r e a d s h e e t s/faq.

742

. , Excel



VBA

VBA .
Excel.
Excel 2002 VBA .

AppActivate

call

chDir

chDrive

close

const

Date

Declare

DLL

DefBool

Boolean ,
Byte ,

Def B y t e
Def D a t e

Date ,

DefDec

Decimal ,

DeDouble

Double ,

Def mt

Integer ,

DefLng

Long ,

Def o b j

Object ,

Def sng

Single ,

Def str

String ,

Def Var

Variant ,

DeleteSetting

Dim
Do-Loop

2nd

, ; , End ,
I f , Sub, F u n c t i o n , P r o p e r t y , Type S e l e c t

Enum*

Erase

Error

744

. VBA

. . 1

Event *

E x i t Do

Do-Loop

E x i t For

Do-For

E x i t Function
Exit

Property

Exit

sub

Function

Filecopy

For Each-Next

For-Next

Function

F u n c t i o n

Get

GoSub... Return

GOTO

If-Then-Else

implements*

input #

Kill

bet

Line Input #

Load

Lock.. .unlock

Lset

Mid

MkDir

Name
on Error
o n . . . GoSub

o n . . . GOTO

open

o p t i o n Base

o p t i o n compare ,
option Explicit
Option P r i v a t e

, p r i v a t e

print #

Private

P r o p e r t y Get

P r o p e r t y Get

745

. . 1

P r o p e r t y Let

p r o p e r t y Let

Property set

p r o p e r t y Set

Public

Put

RaiseEvent

Randomize

ReDim

Rem

( , ['])

Reset

Resume

RmDir

Rset

Savesetting

Seek

S e l e c t Case

sendKeys

set

SetAttr

static

stop

sub

s u b

Time

unload

W h i l e . . .Wend

width #

with
write #

Excel VBA
VBA , Excel, Excel
VBA.
WorksheetFunction. , VBA
. Excel ,
VBA:
Deg = Application . WorksheetFunct ioji.Degrees (3 .14)

746

. VBA

WorksheetFunction Excel 97.


Excel WorksheetFunction
:
Deg = Application.Degrees(3.14}
Excel 2002 VBA .

.2. VBA

Abs

Array

ASC

ASCII

Atn

cbool


Boolean

cbyte

Byte

Ccur
cdate

Currency
Date

CDbl

Double

cdec

Decimal

choose

chr
cine


Integer

CLng

Long

Cos
Createobj ect


OLE Automation

csng

Single

cstr

String

CurDir

Cvar

Variant

CVDate

Date ( ;
)
,

cvErr
Date

DateAaa

DateDif f

DatePart

Dateseriai

747

. .2

DateVAlue

Day

DDB

Dir

DoEvents

Environ

EOF

True,

Err

Error

(),

FileAttr

FileDateTime

FileLen

Filter

Fix

Format

F o r m a t c u r r e n c y * ( )
FormatDateTime

FormatNumber

Formatpercent
FreeFile
FV

GetAiisettings
GetAttr
Getobj ect

,
OLE Automation

Getsetting

Hour
IIF
input
inputBox

instr

instrRev*

mt

ipmt

IRR

isArray

T r u e ,

748

. VBA

isDate

T r u e ,

isEmpty

True,

isError

T r u e ,

isMissing

T r u e ,

isNull

T r u e , Nu 11

isNumeric

T r u e ,

isobject
Join

T r u e , OLE Automation
,

LBound

Lease

Left

Len

LOF

Log

LTrira

Mid

Minute
MIRR

Month

MonthName

MsgBox

Now

NPer

NPV

oct

Partition

, ,

Pmt

Ppmt

PV

QBcolor

RGB

Rate

Replace*

RGB
Right

, RGB
,

749

. .2

Rnd

"- 0 1

Round

RTrim

Second

seek

sgn

shell

sin

SLN

space

spc

split

sqr
str

strcomp

strConv

string

strReverse*

switch

, ^
, T r u e

SYD

Tab

Time

Timer

, ,

TimeSeriai

Timevalue

Trim

TypeName

UBound

ucase

val

varType

Weekday

WeekdayName

Year

* Excel 97 .

750

VBA

,
. .
Excel.

Return GoSub

(, Integer)

. , . ,
, Excel Windows (, , )

. , . ,
s h e e t s (" S h e e t 2 " ) , s h e e t 2

10

11

13

14

16

17

18

. , ,

20

. ,

28

E x i t s u b

35

47

DLL

48

DLL

49

DLL

51

52

53

54

55

57

58

59

61

62

63

67

68

70

752


71

74

75

76

91

wi th . , S e t . , , (, A c t i v e c e l i ) ,

92

F o r

93

94

Nu 11

96

97

98

321

322

325

380

381

382

S e t

383

S e t ( }

385

387

s e t

393

G e t

394

G e t ( )

422

423

424

. ,

429

ActiveX ( , )

430

432

438

440

442

753


443

Automation ,

445

446

447

448

449

450

451

P r o p e r t y L e t P r o p e r t y G e t

452

453

DLL

454

455

457

458

Automation, Visual Basic

459

460

461

462

463

481

482

735

T E M P

744

746

1004

, . . , VBA. , Excel ( ) VBA. ( R a i s e E r r ) , VBA

754

. VBA


ANSI

ANSI, (
), , , ( ),
.
.

ANSI

"

&01

00000001

&02

00000010

403

00000011

&04

00000100

405

00000101

4N06

00000110

&07

00000111

<Backspace>

08

00001000

<Backspace>

<>

&09

00001001

<>

10

<Line Feed>

40

00001010

11
12

&

00001011

&H0C

00001100

13

<Enter>

00001101

<Enter>

14

SHOE

00001110

15
IK

4H0F

00001111

&10

00010000

17

00010001

II

&11

412

00010010

19

413

00010011

20

&14

00010100

21

&15

00010101

22

416

00010110

23

&17

00010111

24

&H1S

00011000

25

&19

00011001

26

00011010

27

&1

00011011

28

&1

00011100

29

&H1D

00011101

30

&1

00011110

31

&H1F

00011111

32

<6>

&20

00100000

<6>

33

&21

00100001

<!>

34

&22

00100010

<">

35

&23

00100011

36

&24

00100100

<#>
<$>

37

&25

00100101

<%>

756

. ANSI

ANSI

38

&

&26

00100110

<&>

&27

00100111

<(>

39

41)

SH28

00101000

41

&29

00101001

42

&2

00101010

43

SH2B

00101011

<*>
<+>

82

00101100

<>

&H2D

00101101

<->

&2

00101110

44
45

47

&H2F

00101111

</>

46
48

&30

00110000

<0>

49

&31

00110001

<1>

&32

00110010

<2>

51

&

00110011

<3>

52

&34

00110100

<4>

53

&5

00110101

54

&6

00110110

<5>
<6>
<7>

55

&37

00110111

56

&38

00111000

<8>

57

SH39

00111001

<9>

58

&

00111010

<:>

59

&

00111011

00111100

<;>
>

60

<

&H3D

00111101

<=>

62

>

&

00111110

<

63

&H3F

00111111

<?>

64

01000000

>:

SH41

01000001

<>

66

&42

01000010

67

43

01000011

<>
<>

68

<D>

44
SH45

01000100

69

01000101

<>

70

&46

01000110

<F>

71

&47

01000111

<G>

72

&48

01001000

73

&49

01001001

<>
<|>

74

&4

01001010

<J>

61

757

ANSI

75

&H4B

01001011

&H4C

01001100

77

L
M

4H4D

01001101

78

4H4E

01001110

79

0
p

4H4F

01001111

80

4H50

01010000

81

&H51

01010001

82

&H52

01010010

83

&HS3

01010011

84

SH54

01010100

85

4H55

01010101

86

4H56

01010110

87

&H57

01010111

88

&H58

01011000

89

4H59

01011001

90

4H5A

01011010

91

[
\

4H5B

01011011

&H5C

01011100

]
-

4H5D

01011101

76

92
93
94
95
96

4H5E

01010

&H5F

01011111

&H60

01100000

97

4H61

01100001

98

4H62

01100010

99

&H63

01100011

100

&H64

01100100

i 01

e
f

4H65

01100101

&H66

01100110

4H67

01100111

h
i

4H68

01101O00

&H69

01101001

4H6A

01101010

1H6B

01101011

4H6C

01101100

4H6D

01101101

DO

1
m
n

4H6E

01101110

111

4H6F

01101111

102
103
104
105
106
107
108

109

758

'

. ANSI

ANSI

'

<>
<q>

112

&70

01110000

113

71

01110001

114

&72

01110010

<|>

115

&73

01110011

<s>

116

01110100

117

&75

01110101

<>

118

&76

01110110

<v>

119

SH77

01110111

<w>

120

&78

01111000

<>

121

79

01111001

122

01111010

<>
<z>
<(>

123

&7

01111011

&7

01111100

&H7D

01111101

126

SH7E

01111110

<l>
<)>
<">

127

&H7F

01111111

<Del>

SH80

10000000

<Alt+0128>

81

10000001

<Alt+O129>

&82

10000010

Alt+0130

SH8S

10000011

<Alt+0131>

&84

10000100

<Alt+0132>

&85

10000101

<Alt+0133>

124
125

128

129
130

I
)

131

132

133

134

86

10000110

<Alt+0134>

135

&87

10000111

<Alt+0135>

136

88

10001000

<Alt+0136>

137

/<

&89

10001001

<Alt+0137>

138

Jb

&8

10001010

<Alt+0138>

48

10001011

<Alt+0139>

134
140

48

10001100

Alt+0140

141

&H8D

10001101

< Alt+ 0141 >

SH8E

10001110

<Alt+0142>

&HBF

10001111

<Alt+0143>

142
143

144

90

10010000

<Alt+0144>

14S

&91

10010001

<A1t+0145>

146
147
148

92

10010010

<Alt+0146>

93

10010011

<Alt+0147>

94

10010100

<Alt+0148>

>

759

ANSI

149

495

10010101

<Alt+0149>

150

SH96

10010110

Alt+0150

151

497

10010111

<Alt+0151>

152

&98

10011000

<Alt+0152>

153

TU

&9

10011001

<Alt+0153>

154

&9

10011010

<Alt+0154>

155

&9

10011011

<Alt+0155>

156

tfa

49

10011100

<Alt+0156>

157

SH9O

10011101

<Alt+0157>

158

fl

SH9E

10011110

<Alt+0158>

159

&H9F

10011111

<Alt+0159>

SHA0

10100000

Alt+0160
<Alt+0161>

160
161

&1

10100001

162

SHA2

10100010

<Alt+0162>

163

10100011

<Alt+0163>

164

44

10100100

<Alt+0164>

&5

10100101

<Alt+0165>

SHA6

10100110

<Alt+0166>

&7

10100111

<Alt+0167>

88

10101000

<Alt+0168>

&9

10101001

<Alt+0169>

&

10101010

Alt+0170

&

10101011

<Alt+0171>

10101100

<Alt+0172>

.HAD

10101101

<Alt+0173>

&

10101110

<Alt+0174>

SHAF

10101111

<Alt+0175>

165
166
167
168
169
170
171
172

173
174

175
176

SHB0

10110000

<Alt+0176>

&1

10110001

<Alt+0177>

178

&2

10110010

<Alt+0178>

179

&

10110011

<Alt+0179>

180

SHB4

10110100

Alt+0180

181

&HBS

10110101

<Alt+0181>

182

46

10110110

<Alt+0182>

47

10110111

<Alt+0183>

177

183
184

&8

10111000

<Alt+Q184>

185

SHB9

10111001

<Alt+0185>

7B0

. ANSI

ANSI

"

186
187

&

10111010

<Alt+0186>

&

10111011

<Alt+0187>

188

10111100

<Alt+0188>

139

j
S

4HBD

10111101

<Alt+0189>

190

&

10111110

Alt+0190

191

&HBF

10111111

<AltfO191>

192

&0

11000000

<Alt+0192>

193
194

&1

11000001

<Alt+01S3>

&2

11000010

<Alt+0194>

195

&

11000011

<Alt+0195>

196
197

&4

11000100

<Alt+0196>

&5

11000101

<Alt+0197>

198

SHC6

11000110

<Alt+0t98>

47

11000111

<Alt+0199>

11001000

Ait+0200

&9

11001001

<Alt+0201>

11001010

<Alt+0202>

&

11001011

<Alt+0203>
<Alt+0204>

199
200

201
202
203

204

11001100

&HCD

11001101

<Alt+0205>

206

SHCE

11001110

<Alt+0206>

207
20S

&HCF

11001111

<Alt+0207>

4HD0

11010000

<Alt+0208>

20!J

SHD1

11010001

<Alt+0209>

&HD2

11010010

Alt+0210

&HD3

11010011

<Alt+0211>

&HD4

11010100

<Alt+0212>

&HD5

11010101

<Alt+0213>

4HD6

11010110

<Alt+0214>

&7

11010111

<Alt+0215>

4HD8

<Alt+0216>

&HD9

11011001

<Alt+0217>

&HDA

11011010

<Alt+0218>

SHDB

11011011

<Alt+0219>

205

210
211
212
213
214
215
210
217
218
219
220
221
222
223

SHDC

11011100

Alt+0220

4H0D

11011101

<Alt+0221>

4HDE

11011110

<Alt+0222>

8HDF

11011111

<Alt+0223>

ANSI

224

&HE0

11100000

<Alt+0224>

225

&1

11100001

<Alt+0226>

226
227
228

&2

11100010

<Alt+0226>

&

11100011

<Alt+0227>

44

11100100

<Alt+0228>

&5

11100101

<Alt+0229>

&6

11100110

Alt+0230

&7

11100111

<Alt+0231 >

232

&8

11101000

<Alt+0232>

233

&9

11101001

<Alt+0233>

234

11101010

<Alt+0234>

235

&

11101011

<Alt+0235>

236
237

11101100

<Alt+0236>

&HED

11101101

<Alt+0237>

233
239
240
241

&

11101110

<Alt+0238>

&HEF

11101111

<Alt+0239>

&HF0

11110000

Alt+0240

&HF1

11110001

<Alt+0241>

&HF2

11110010

<Alt+0242>

229
230
231

242

243
244

&HF3

11110011

<Alt+0243>

&HF4

11110100

<Alt+0244>

246

&HF5

11110101

<A!t+0245>

246

&HF6

11110110

<Alt+0246>

SHF7

11110111

<Alt+0247>

&HF8

11111000

<Alt+0248>

4HF9

11111001

<Alt+0249>

&HFA

11111010

Alt+0250

&HFB

11111011

<Alt+0251>

&HFC

11111100

<Alt+0252>

&HFO

11111101

<Alt+0253>

8HFE

11111110

<Alt+0254>

&HFF

11111111

<Alt+0255>

247
248
249
250
251
252
253
254
255

, <Ah >,
<NumLock>

762

. ANSI


Web-

, Web- .

.
. Excel,
.
Power Utility Pak. Excel.
Sound-Proof 2000.
.
().


, , Web- . , 3 c h a p t e r s \ c h a p 0 3 \ .

. .

3
a r r a y examples. x l s .
m e g a f o r m u l a - l . x l s
( ).
megaformula-2 . x l s ().
megaformula-3 . x l s
VBA.

c o n t r o l s on s h e e t . x l s ActiveX
.

comment o b j e c t s . x l s VBA,
Comment.

s h e e t s o r t e r . x l s .
s h e e t s o r t e r - 2 . x l s , , .

10
c o m m i s s i o n . x l s Commission, .
d r a w . x l s Draw,
.
key p r e s s . x l s API <Shift>, <Ctrl> <AIt>.
month names . x l s MonthNames, .
mysum. x l s MySum, Excel .
r e v e r s e . x l s Reverse,
, .
u p p e r c a s e . x l s UpCase, Excel
.
w i n a p i . x l s Windows .
windows d i r e c t o r y . x l s ShowWindowsDir,
API Windows.

11
a b o u t s e l e c t i o n . x l s , .
b a t c h p r o c e s s i n g . x l s .
: t e x t 0 1 . t x t , t e x t 02 . t x t t e x t 03 . t x t .
d a t a t y p e . x l s C e l l T y p e ,
.
d a t e and t y p e . x l s , .

764

. Web-

d e l e t e empty rows. x l s .
e x t r a c t element. x l s ExtractElement,
f i l e a s s o c i a t i o n . x l s API,
, .
in r a n g e . x l s , True, .
i n p u t . x l s VBA InputBox .
l a s t saved and p r i n t e d . x l s LastSaved LastPrinted,
,
l i s t f o n t s . x l s .
max a l l s h e e t s . x l s MaxAllSheets,
.
next empty row. x l s
.
page count. xls , , .
p l a y sound. x l s Alarm,
sound.wav .
p r i n t e r info.xls API, .
random functions .xls Randomlntegers (
) RangeRandomize ( ).
range s e l e c t i o n s . x l s
.
Selection Demo.
r e g i s t r y . x l s APJ, .
s e l e c t max.xls ,
.
s e l e c t i v e c o l o r . x l s .
s h e e t o f f s e t . x l s SheetOf f s e t .
s o r t i n g d e m o . x l s VBA, .
sound, x l s API, .
s t a t f u n c t i o n s . x l s S t a t F u n c t i o n .
t o g g l e s . x l s .
u t i l i t y f u n c t i o n s . x l s : F i l e E x i s t s , F i l e NameOnly, P a t h E x i s t s , RangeNameExists, S h e e t E x i s t s WorkBooklsOpen.

765

v a r i a n t t r a n s f e r . x l s , .
video mode . x l s API,
.
worksheet functions. x l s : SheetName, WorkbookName, AppName, CountBetween, LastlnColumn, LastlnRow
IsLike.
w r i t e and read r a n g e . x l s
.

12
get f i l e n a m e . x l s GetOpenFilename.
get d i r e c t o r y . x l s API , .

13
c o n t r o l s on sheet . x l s (
) .
c o n t r o l s on s h e e t 2 . x l s ,
.
get name and sex. x l s , 13.
newcontrols . , Toolbox ( ) VBE.
Toolbox Import Page ( ).
s p i n b u t t o n events . x l s , SpinButton.
s p i n b u t t o n t e x t b o x . x l s SpinButton TextBox.
userform events . x l s , UserForm.

14
a c t i v a t e s h e e t . x l s
L i s t Box.
change s i z e , x l s UserForm .
f i l l l i s t b o x . x l s ListBox.
item t r a n s f e r . x l s
ListBox.
move i t e m s . x l s
ListBox.

766

. Web-

multi-column l i s t b o x . x l s ListBox, , .
multicolumn l i s t b o x . x l s ListBox, , .
m u l t i p l e l i s t s . x l s
ListBox.
queryclose . x l s , UserForm .
ref e d i t . x l s Re f Edit,
s e l e c t r o w s . x l s ListBox,
.
s e l e c t e d i t e m s . x l s
ListBox.
s p l a s h . x l s - .
unique. x l s ListBox .
u s e r form menus . x l s CommandButton ListBox.
and s c r o l l s h e e t s . x l s
.

15
c o l o r p i c k e r . x l s , .
d a t a f o r t n . x l a , Excel
. .
modeless u s e r f o r m . x l s
UserForm, .
modeless userf 2 . x l s
UserForm, .
m u l t i p l e b u t t o n s . x l s .
mymsgbox.xls MyMsgBox, VBA
MsgBox.
owe c h a r t - simple. x l s UserForm.
Office Web Components.
owe c h a r t 2000 . x l s UserForm Office Web Components. Excel 2000.
owe c h a r t 2 0 0 2 . x l s UserForm Office Web Components. Excel 2002.
owe s p r e a d s h e e t . x l s Spreadsheet, Office Web Components.

767

p r o g r e s s - l . x l a . ( userForm.)
p r o g r e s s - 2 . x l s . ( &
UserForm.)
wizard. x l s "", .

16
t e x t t o o l s . x l s , 15
( ).
u n d o . x l s , VBA.

17
b u d g e t . x l s
( VBA).
e x t e r n a l db.xls ( VB). budget. mdb.
modify p i v o t . x l s VBA,
.
simple d b . x l s ,
. , "" ,
survey d a t a . x l s
.

18
animated c h a r t . x l s .
autof i l t e r c h a r t . x l s
.
c h a r t a c t i v e c e l l . x l s .
c h a r t image map. xl s , -.
c h a r t in u s e r f o r m . x l s " " .
c h a r t s e r i e s c l a s s . x l s C h a r t S e r i e s Class.
clock C h a r t . x l s , () .

768

. Web-yam

combobox c h a r t . x l s
.
c r e a t e c h a r t . x l s , . , ""
.
d a t a l a b e l s . x l s
.
events - chart s h e e t . x l s .
events - embedded.xls
.
get c h a r t r a n g e . x l s ,
Range, .
h y p o c y c l o i d . x l s ,
.
l i n k e d p i c t u r e s . x l s
.
m u l t i p e c h a r t s . x l s ,
s k e t c h . x l s , .

19
a p p l i c a t i o n events 2k.xls Application.
UserForm,
Excel 2000 .
a p p l i c a t i o n e v e n t s 97 . x l s Application. Excel 97 .
log workbook o p e n . x l s
.
onkey demo . x l s .
s e l e c t i o n c h a n g e . x l s SelectionChage Worksheet .
t r a c k changes in cominents.xls ,
, .
v a l i d a t e e n t r y l .xls . EnableEvents.
v a l i d a t e entry2 . x l s . EnableEvents .

20
automate Excel. doc Microsoft Word, Excel. p r o j e c t s i o n . x l s .

769

make memos. x l s Microsoft


Word, , .
simple ado example . x l s Microsoft Access ( budget .mdb).
s t a r t c a l c u l a t o r . x l s ( ) Microsoft
Calculator ().
s t a r t charmap.xls Microsoft
Character Map ( ).
system dialogs .xls 50 .

21
t e x t t o o l s . x l a , 16.

22
a u t o s e n s e . x l s "" , , .
commandbarbuttons.xls CommandBar But ton.
dynamic c a p t i o n . x l s , .
f a c e i d s . x l a Faceld
. \ f a c e i d s .
h i d e and r e s t o r e . x l s
.
l i s t a l l c o n t r o l s . x l s , Caption .
l i s t cominandbars.xls , ,
CommandBar.
month l i s t . x l s
CommandBar.
t o g g l e t o o l b a r s . x l s V i s i b l e
CommandBar.

23
add newmenu.xls .
add to Tools menu.xls
,
hide m e n u . x l s .

770

. Web-

L i s t menu i n o . x l s , (, ) .
l i s t s h o r t c u t menus.xls , .
menu m a k e r . x l s , .
new menubar. x l s Excel.
new s h o r t c u t m e n u . x l s .
t o g g l e g r i d l i n e s . x l s "" .

24
\ a s s i s t a n t \ f o r m l e t t e r . x l s Office.
\comments\formletter.xls
.
f unction\myf unc. x l s .
h t m l h e l p \ f o r m l e t t e r . x l s
HTML Help ( ).
o t h e r \ m y a p p . x l s : Help, .
textbox\f o r m l e t t e r . x l s TextBox, .
u s e r f o r m l \ f o r m l e t t e r . x l s
Label, UserForm.
userform2\f o r m l e t t e r . x l s
"" Label UserForm.
u s e r f o r m 3 \ f o r m l e t t e r . x l s
ComboBox Label, UserForm.
w i n h e l p \ f o n n l e t t e r . x l s
WinHelp ( ).
w o r k s h e e t \ f o r m l e t t e r s . x l s
.
24 . . .

25
l o a n a m o r t i s a t i o n "wizard. x l a -, . . .

771

26
m u l t i l i n g u a l w i z a r d . x l s , .

27
does f i l e e x i s t . x l s . .
export import . x l s CSV,
CSV, .
export to HTML. x l s HTML.
f i l t e r t e x t f i l e , x l s (inf i l e .
t x t ) , o u t p u t . t x t , .
import more than 256 . x l s sheet 1.
256 ,
. longf i l e . t x t , 600 .
l i s t f i l e s l . x l s , ,
. , .
l i s t f i l e s 2 . x l s , ,
, . Pi leSearch.
show d r i v e inf . x l s ! , FileSystemOb j e c t .

28
add 100 b u t t o n s . x l s , 100 CommanButton UserForm .
add b u t t o n and code. x l s
CommandButton VBA.
add u s e r f o r m . x l s UserForm
" ".
o p t i o n s f o r m . x l s UserForm
" " ( OptionButton),
, , .
r e p l a c e module.xls .
UserBook.xls.
show components . x l s , VBA, .

772

. Web-

29
c s v c l a s s . x l s ,
CSV.
k e y b o a r d . x l s , NumLock
CapsLock.

Power Utility Pak


Power Utility Excel.
Web- , 30 .
Power Utility Pak
Excel. , Excel : PUP 2000.
Power Utility Pak P U P 2000,
, Create a PUP Toolbar.
Power Utility Pak . PUP 2000 1
.

Power Utility Pak


Power Utility Pak , Excel.
1.

Excel ^.

2.

Power Utility Pak 2000.

3.

, .

Power Utility Pak re


, Power Utility Pak.

Sound-Proof 2000
Sound-Proof 2000 Excel, .
Microsoft Agent .
, (. .).
Excel 2002 . Sound-Proof 2000
.
() . ,
154,78 " " ( .
). (, " "),
(, " '').
Web- Sound-Proof 2000. 12 . $24,95.

773

Sound-Proof 2000 Excel.


^
Sound-Proof 2000.

Sound-Proof 2000
Sound-Proof 2000 .
1.

Excel ^ .

2-

Sound-Proof 2000.

3.

Sound-Proof 2000,
.
Sound-Proof 2000 , , .

774

. Web-

ActiveX, 116; 361; 530


ADO, 44; 537
Analysis ToolPack, 60
API, 263
Application, 166
ASCII-, 84

HLP. 621
HTML, 90
HTML Help System, 621

I
IDE. 677
InputBox, 272; 319; 320

BASIC. 130
Boeing Calc, 34

CheckBox, 337
COM. 544
ComboBox, 337
CommandButton, 364
Comments, 162
CSV,667
CubeCalc, 34

L
Label. 338
ListBox. 338; 365; 372
Lotus 1-2-3, 33; 85; 132
LotusScript. 35; 132

M
Microsoft Query. 44
MsgBox, 193; 321:398
MultiPage. 338; 385
MultiPlan, 38

It
DAO. 44
DDE. 525
DDL, 310
DLL, 84

Object Browser. 173


Office Web Components, 411
OLE, 530
OptionButton, 339

E
Excel
Excel
Excel
Excel

2.1. 38
4, 40
95, 40
3, 39
I'

FileDialog, 330
FileExists, 294
FileNameOnly, 294
Frame. 338
Q
GetOpenFilename, 325
GetSaveAsFilename. 328

I>
PathExists, 295
PRN, 667
Project Explorer, 137
Public, 211
Q
QuattroPro, 36

R
R1C1.66
Range. 167
RangeNameExists, 295
RefEdit, 366; 422
Rem, 176

s
ScrollBar. 339
SheetExists, 295
SpinButton, 339
SuperCalc, 33

T
TextBox, 339
Toolbox, 359
Twin, 34
TXT, 667
U
UI Builder. 37

VBA. 43; 113; 131


VBE, 46; 135
VGA, 128
VisiCalc, 32

w
Windows API, 263; 310; 649
Windows Help System. 621
WinHelp. 629
WordPerfect. 36
WorkbookIsOpen, 296
Workbooks. 195
X
XLA, 550
XLB, 566
XLM, 39; 48; 131
XLS, 550

. 65
. 65
, 65
. 44; 530

. 147

775

, 479
, 577
, 146
, 148

, 604
, 59

, 458
, 528
, 593
, 46

, 75
,
242
, 472
.
492
, 159; 221; 502
, 247
, 101

, 696
,
260
, 336
, 55
, 569
, 468

, 331
, 179
, 192

, 466
, 366
, 333
, 482
, 406

, 430
, 460
, 270
, 53
, 382

,
284
. 58

, 114; 545

Office, 627
, 542
, 245
-, 122
Windows API, 310

, 84
, 211
, 84
, 245
, 173; 516; 531
, 156
, 189
,

64

. 141; 177
, 52
. 273
Excel, 113; 127:561
, 525
, 127; 312
. 518
59; 447
, 530
, 100
, 49S
, 67

, 397

776

, 406
, 544
, 48
, 268
, 278
, 188

, 190
, 525

, 586
,
689

, 394
, 138
, 596
, 261
,
547
, 373; 599
, 351
, 430
, 594
, 337;
582; 685

, 58
,
551
3

, 527
, 228

, 687
, 346
, 534
, 286
, 611

, 681

, 466
, 667
. 77; 187; 656
, 315
, 665
, 143; 229; 455
. 460
, 281
,

146
, 577
, 51; 57; 453

, 478
Excel, 82
, 494
VBE, 135
, 50; 318

Windows, 526
, 179
, 367
, 149

, 123
, 35
,73;271

, 74
, 529
, 485
, 260
ChartSeries, 473
,
101

, 109
,34
, 654

, 45; 157
, 423

, 423
, 584
, 423
, 549
, 423
, 594
, 652
, 144

, 344
, 300
, 463
, 157; 550
,
Addlns, 553
369
CommandBars, 572; 594
,
Comments, 162
463
Dialogs, 331
, 427
References, 679
. 451
VBComponents, 679
,
VBProjects, 679
698
Workbooks, 195
, 72
, 359

, 143;
, 668
150
, 703
, 564; 592
, 55; 68
, 51; 120;
, 154
154;213; 345; 593

387
, 360

, 160; 176
, 638
, 621
, 60
, 499
, 33
, 99
, 92
, 185; 648
, 130
, 255

, 664
For Each-Next, 195
, 343
If-Then, 198
, 84
Select Case, 201

With-End With, 195


, 68
, 158
, 464
, 564; 593;
, 343
612
, 228
, 89

, 642
, 268
, 269
, 145

, 608
, 551
. 48
, 48
, 155
, 183
, 654

, 57
, 34
, 283
, 73; 190
, 393
, 635
, 55; 260
, 370
, 79
, 544; 548
, 49; 117; 592
, 487
Excel
GetSaveAsFilename, 328
GetOpenFilename, 325
InpuiBox, 320
SendKeys. 540
. 291
, 184

Comment, 162
FileSearch, 661
, 159
, 288; 700
, 190
,
127;653
, 695

, 34; 61; 89; 262; 542


, 210

, 345

777

.
240

, 117
. 119
, 546
, 340
VBE. 146
,
401
, 252
, 103
, 377

, 647
, 647
,
572

, 70; 183
, 211
, 245
, 51

, 682
, 126

. 113; 224
,
293
, 220; 347; 351 .
, 343
, 45; 157
Addln, 555
Application, 166
Comment, 160
FileDialog, 330
FileSearch, 661
FileSystemObject, 663
Range, 167
Series, 469
UserForm, 520
, 45; 131
IDE, 678
, 454
, 140
, 423
, 185
, 190
, 484
, 181

778

, 2 10
, 244

. 146
, 106
, 136
, 550
Immediate. 137; 220
Properties, 342
, 319
, 137; 139
, 63
GoTo, 197
, 69
, 188
, 668

. 155
, 261
, 547

. 159
Excel, 649
, 376
, 468
. 289
, 275
, 181
, 279
, 275
, 558
, 592
,
51
V , 132
, 122

. 368

, 602
, 568
, 476
, 336; 345
. 660
,
632; 639
. 396

,510
, 519
, 238; 438

, 442
, 457

, 188
Excel, 121
. 72

, 60
, 50; 89;
119; 136; 564
, 340
, 340
, 529
Excel, 95
, 154
, 213

, 311

, 221
. 378

, 568

Excel, 83
, 615
, 339
. 605
, 178
, 499
, 191

, 518
, 185
. 666
, 185
, 259

,
, 269
434
, 379
, 65

, 281
. 289
, 2S2
, 407
, 69

, 248
, 51; 148
, 224
,
419
,
487

, 650
, 42
, 484
, 127
, 593
, 75
, 532

,
552
, 663
, 339

,
296
. 298
, 667
,
477
, 441

, 116; 335
, 192
, 43; 241

, 489
, 48; 115

, 621
Office, 626
,
499
, 665
, 112
, 186
, 186
, 97
, 98
-, 107

, 347
, 441
, 24-1
, 225
, 247
, 165

, 439
, 111
, 274
, 55; 68
, 70
, 70
, 71


, 102
, 543
, 647

, 352; 512
, 425
. 272
, 513
. 296
, 350

, 394
, 175
, 698
, 500
, 136
,
113
, 313
, 114
, 127
, 665
, 370

, 60
. 80
, 276
, 440
,
67 L
, 140; 209
, 389; 484
, 243

, 702
, 701
, 32
, 232; 29!

, 46; 87
, 421
, 47
, 148
, 667
, 592
, 94

, 46
, 559
, 149

, 97; 563
, 419
, 127; 312
, 531
, 453

. 549
, 570
, 125
, 84

, 95

Visual Basic, 135


, 594
, 92

, 616
, 569
. 595

, 463
, 61; 440

, 575
Addln, 555
Application, 166
Comment. 161
FileSearch, 661
, 158

779

, 342;
583

Boolean. 288
Cells. 168
Comment, 163
Offset. 170
Range, 167
Style, 584
Tag, 358
Visible, 585
, 631
, 530

, 184

, 608
, 287
. 49
,
529
, 93; 314; 641
. 558
, 579
, 69
, 307
, 347; 497; 604
Activate. 504
BeforeClose, 507
BeforePrint, 506
BeforeRightClicM15
BeforeSave, 505
Change, 509
Deactivate, 506
NewSheet. 505
On Key, 523
OnTime, 522
Open, 504
SelectionChange, 514
Sheet Activate, 505
, 481
, 355
. 515
, 700
, 355
Addln, 557
Application, 516
Chart, 515
SpinBution, 355
UserForm, 353; 520

780

Workbook, 503
Worksheet, 509
, 42; 62; 646

, 356
, 596

, 418
, 410
, 318; 388
, 3(>7
. 388
. 696
. 574
. 120
, 616
, 393
, 79
, 364
, 484
, 545
, 564
, 536
, 443
, 240;471
,
362
, 225
. 232
, 291

, 140
, 547
, 286
, 154
, 100
, 338
, 354
, 290
, 183
, 161; 437;
619

V B A H X L M . 131

. 223
, 117

, 65
, 65

. 561
, 574
,
358;580
, 63
, 67
R1C1,66
, 70
, 158
, 136
, 54
, 42

, 448
, 136; 564. 591; 592
, 448
, 388
. 187
, 187
, 60
., 113
, 128
, 637
. 204
, 75
, 60

, 526

623
, 339
, 420
, 86; 665
,

, 345
, 121
, 234
, 546
, 182
. 179
, 279

, 467
, 275

, 572

, 498
, 84
, 105
, 227

, 461
, 575
, 598
, 138
, 568
, 433
, 278
, 431
, 583
, 593

, 458
, 312
, 553
, 565
, 488
, 285
, 658
, 114

, 416; 548
, 606
, 418

, 82
, 84;
88
, 84: 88
, 43; 113

Lotus 1-2-3, 85
Quattro Pro, 86
, 56

, 433
, 671
, 337

, 415

RIC1.66
, 648
. 586
, 53; 298
, 461

, 86
, 85
, 53; 63; 113
, 73
, 465
, 55; 140; 240
CreateObject. 532
FileExists, 294
FileNameOnly, 294
GetOption, 692
Ilf, 201
lnputBox, 272; 319
MsgBox, 193; 321
PathExisls, 295
RangeNameExists, 295
SheelExists, 295
Shell, 526
Windows API, 263; 310
WorkbooklsOpen. 296
, 247
, 251
, 251
, 252
, 256
, 249

, 419

. 490
, 565

, 149

, 227; 420
, 203
Do Until, 207
Do While. 206
For-Nexl, 204
, 462

, 53

, 315
, 666

, 362
, 149

, 702
, 138
, 41

CheckBox, 337
ComboBox. 337
CommandButlon, 364
Frame, 338
Image, 338
Label, 338
ListBox, 338; 365; 372
MultiPage. 338; 385
OptionBinton, 339
RefMit. 366; 422
ScrollBar, 339
SpinButton, 339
TexlBox, 339
VBA, 175

Excel, 361
MsgBox, 398

, 647

781

VBA
Excel 2002

. .
..
. .
3. . , . . ,
. . , . .

"".
101509, , . , . 43, . 1.
. . 090230 23.06.99
.
20.10.2003. 70X100/16.
Times. .
. . . 49.0. .-. . 44,82.
3000 . 975.
" "
,
.
197110, -, ., 15.

Excel

" - "

ISBN 5-8459-0474-9

ISBN 5-8459-0314-9

ISBN 5-8459-0400-5

...it
"Excel"
Ciic^rmtti

www.dialektika.com

w w w . wiltiamspubli shing.com

www.clscopress.ru