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

- -

..
..


Microsoft Word
Visual Basic for Applications

-
2010

- -

..
..

Microsoft Word
Visual Basic For Applications

-
2010

- -

32.977
286
681.3 (075)
.., ..
Microsoft Word Visual Basic For Applications:
/ . ., 2010. 83 .: . 29.

Microsoft Word c
Visual Basic For Applications.

.

150405 (170400)
240801 (170500)
,
, II (IV ; 32 ) III (V
; 51 ).
:
,
.. ( );

. . ( ).

-
.

.. , ..

-
, 2010

- -

.............................................................................................. 5
1. VBA ........................................................................ 8
1.1. Visual Basic
VBA...
1.2. .............................. 11
1.3. VBA .................... 17
1.4. VBA ....................................... 24
1.5. .
............................................................................ 31
1 ........................................... 37
2. VBA................................. 38
2.1.
2.2. ............................................. 46
2 ........................................... 51
3. VBA ................................................ 52
3.1. .
...
3.2. .
....................................... 60
3 ........................................... 66
4. VBA.................................................... 67
4.1. ...................................
4.2. . 73
4 ........................................... 79
................................................................................... 80
............................................................. 84

- -


VBA Microsoft Word.
: ,
,
- ,
,
Delphi, Visual C++, Visual Basic .

, Microsoft Office ( 99%
),
,
VBA. VBA
: Visual Basic For Applications, .. Visual Basic
. Microsoft
Office Microsoft
Office .

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


.
,
Visual Basic,

. ,
.
VBA
.
, QBasic QuickBasic,
-
. VBA

QBasic QuickBasic.
. .1 .2.
,
5

- -

ax2+bx+c=0. . .1
QuickBasic,
. .2.
Visual Basic For Applications.

. .1. QuickBasic.

. .2. Visual Basic For


Applications.

- -

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

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

.

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

Microsoft Word.
,
VBA Word.

VBA ,
,

.
-


.
7

- -

1. VBA
1.1. Visual Basic
VBA
VBA (Visual Basic For Applications) ,
BASIC.
, ,
,
.
BASIC
60-x . XX .

.
: Beginners Allpurpose Symbolic Instruction Code. :
.
,
,
( ,

),
.
80- . XX .
()
BASIC .

BASIC.
() MS
DOS.

QBASIC QuickBasic, Microsoft
OC MS DOS.
90- . XX . ,
OC Windows,
Windows (
Windows).
Windows-
, Microsoft
Basic, OC
Windows .
Visual Basic. ,
Visual Basic
,
8

- -

Basic .

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

( , , ,
, )
, ,
-
, . Visual Basic
.
,
,
( Visual Basic
). .
, ,
..
.

,
.

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

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

- -

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

Windows-.

( ) Visual Basic
Microsoft Office.

Microsoft Office, .

Microsoft Office.
:

. ,
Microsoft Office,
: ,
,
VBA.
VBA ,
Microsoft Office,
Visual Basic
. VBA
,
( , , , ),
, Microsoft Office,
.
, VBA
Microsoft Word, Word, ,
, , , , ,
. VBA
Microsoft Office
.
,
VBA ,

.

10

- -

1.2.
,
Microsoft Office
.
,
, ,
. Microsoft Office
, ,
.
.

Microsoft Office: Microsoft Word, Microsoft Excel,
Microsoft Access, Microsoft PowerPoint.
Word (
Microsoft Office
,
Microsoft Office ).
, OC Windows
Word.
, ,
(. 1.1).

. 1.1. Microsoft Word


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


11

- -

,
. . 1.2.

1, 2 .. ,
, ,
, ,
. ,
, , ,
,
_. ,
. ,
( ),
.

. 1.2.
( )
(..
)
, .
12

- -

.

, .1.3.

. 1.3.


. ,
.
13

- -

, Ctrl
Alt, - .
,
.
Ctrl Alt ,
, -
. ,
.

, .

. , . 1.4,
Alt + .
,
, .
Microsoft Word ,
, . -,
, ,
( ).
-, .
.
,
.
: ,
, . ,
, ,
, , .
,
Word.
, (
) :
1. F8 .

, Word.
F8 . Esc
.
2. Ctrl+ (
) Ctrl+ ( )
.
3. Ctrl+I (
) Ctrl+ ( )
.

14

- -

. 1.4.

, ,
,
. :
, -
, .
.
15

- -

, , ,
, .
.

. .
, ,
.
,
. ,
,
, ..
,
.
Ctrl+
.
,
.
,
. ,
.
,
,
.
,

. ,
( )
.

.

.
.
Visual Basic.

Visual Basic, -
.
,
.
.

16

- -

1.3. VBA
VBA
.
,
Microsoft Office - ,
,
,
( )
. ,
.

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

. ,
VBA, ,
,
.
VBA, Microsoft Word

Visual Basic (. 1.5).

. 1.5. VBA Microsoft


Word
17

- -


VBA.

,
VBA, . 1.6.

. 1.6. VBA
,
,
Microsoft Visual Basic.
( Microsoft Word Microsoft Excel),
VBA.
,
Windows : ,
() . ,

Microsoft Word Microsoft Excel,
VBA .
,
. ,
.
18

- -

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

, (
), .
(
VBA Toolbar).
, ,
.
, .
( )
- .
,
. ,
( ) .
,
Save () File ().
Cut () Edit
(). Run
( ) Run .
VBA
(UserForm).
,
(, , , ,
). ,
,
.
( ),
, ,
(.. , ).
(
) .

, .
, .
, ,
,
. ,
.
19

- -

:
VBA
. ,
Insert ().
, .
UserForm ( )
.

( Toolbox).
,
. ( )
,
.
, A
(Label).
ab ,
(Textbox).
(CommandButton).
, ,
(OptionButton). ,
,
(Checkbox).
,
,
.
, ,
.

(Properties). ,
,
. -
, .
,
.
,
.
, .
(eight) (Width) ,
(Left)
(Top), (Backolor) ,
(ForeColor).
,
, .
20

- -

,
. ,
,
, .
.
,
50 , ,
, :
, Width ( )
50 100.
( 100 ),
(. 1.7).

. 1.7. .

. 1.8. .

,
.
, (Visible)
21

- -

: True () False ().


,
(

).
,
, .
.
( BackColor ForeColor)
,
(. 1.8).

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

. 1.9. VBA

22

- -


(Project),
, .
,
,
.
,
.1.9. VBA,
.
.
: -
,
,
.
.

:
View
() Code.
, .

. ,
,
.
View Object.
,
,
.

,
VBA. :

, .
File
Close and return to Microsoft Word.

VBA
, . 1.4.

23

- -

1.4. VBA
, VBA,
:
VBA. ,
.

.

. ,
.
,
Microsoft Word, : .doc.

VBA
Visual Basic ( . 1.3).

VBA.
VBA
Insert UserForm.
, ,
. ,
Properties.
,
, .
UserForm1.
,
, Properties.
, Properties
.
Caption,
. ,
.
, ,
.
UserForm1 :
VBA.
.

.
-.
Properties Backolor,
. ,
24

- -

,
. ,
: System () Palette (
). System,
.
Palette, .
- ,
.
Label
(),
A.
Label1.
. Properties Label1
Caption
Label1 : VBA.
.
Properties Font -
. 18
.

. 1.10.
25

- -

.
ForeColor.
Properties. ForeColor
.
, BackColor.
Palette
( ).
.

CommandButton1 CommandButton2.
CommandButton1 Properties Caption
: .
CommandButton2 , :
. Font
, 14 .
, ,
ForeColor .
,
Label2.
.
.
Caption :
( ,
). Font
, 16 , .
Foreolor, .
, . ,
, .
Properties Visible
False.

.
.1.10.

. ,
, .
,
.

- ,
.
, VBA,
.
26

- -

CommandButton1,
,
.
:
Private Sub CommandButton1_Click()
End Sub
.
Sub ( Subroutine,
- ) .
,
, , .

. CommandButton1,
Click (-
). End Sub
. .
,
( Label2) .
Visible False ( )
True ( ), , , Visible
. ( Visual Basic),
, .
.
,
.
. ,

:
Label2.Visible.

, Visual Basic .
, ,
. , ,
, :
Label2.Visible = True
,
. ,
, :
Private Sub CommandButton1_Click()
Label2.Visible = True
End Sub
CommandButton1 .
.

27

- -

CommandButton1 Label2
.
,
.
(. .1.9).
, .
Click ( ).

. ,
, .
DblClick (
), .
:
Private Sub CommandButton1_DblClick (ByVal Cancel
As MSForms.ReturnBoolean)
End Sub
,
, , ,
, ,
. :
Label2.Visible=False
CommandButton1
:
Private Sub CommandButton1_DblClick (ByVal Cancel
As MSForms.ReturnBoolean)
Label2.Visible=False
End Sub
CommandButton1 .
,
View Object.
, ,
CommandButton2.
,
, :
Private Sub CommandButton2_Click()
End Sub
,
.
,

28

- -

CommandButton2.
:
End

:
Private Sub CommandButton2_Click()
End
End Sub
,
. :
Private Sub CommandButton1_Click()
Label2.Visible = True
End Sub
Private Sub CommandButton1_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
Label2.Visible = False
End Sub
Private Sub CommandButton2_Click()
End
End Sub
, ,
. Run Run
.
,
. 1.11.
,
,
.
.
,
.
, . ,
.
, , -
.
.
, : Compile error (
). , ,
. , , ,
, ,
29

- -

.
.

. 1.11.
,
,
. Visual Basic
,

File

),
.doc .

30

- -

1.5. .

, VBA
,
, .
(UserForm) ,
.

8 . , Windows (
)
, ()
, . 8
,
.
: , , , , , ,
, .
,
Microsoft Word (, .doc). Word
Visual Basic
VBA. Insert UserForm
.
.
: ,

, ,
.

.
Properties ()
Caption. ,
, : .
.
, ,
Toolbox Frame (
VBA ).
:
. ,
, ,
Frame1.
Frame1 (
),

31

- -

. ,
8 .
VBA,
Frame1 Caption, ,
. Properties
Caption :
.

OptionButton ()
.
Toolbox :
.
:
OptionButton1,
OptionButton2,
OptionButton3,
OptionButton4,
OptionButton5,
OptionButton6,
OptionButton7, OptionButton8.
Caption. Caption
, .
OptionButton1 Caption
, OptionButton2 ,
OptionButton3 , OptionButton4 ,
OptionButton5 , OptionButton6 ,
OptionButton7

,
OptionButton8

.

CommandButton1 CommandButton2. Caption
, .
,
Font.
.
. 1.12.

. , ,
UserForm1
. , . 1.4,
Backolor ( ),
.
Visual Basic :
.
,
. , ,
vb ( Visual Basic)
. vbWhite,
vbBlack, vbRed, vbYellow,
vbBlue, vbGreen, vbMagenta,
32

- -

vbCyan.
:
UserForm1.Backolor = vbWhite

. , ,
, .
,
OptionButton1,
:
Private Sub OptionButton1_Click()
End Sub

. 1.12.

,
, .
:
Private Sub OptionButton1_Click()
UserForm1.Backolor = vbWhite
End Sub
33

- -

,
OptionButton2,
. :
UserForm1.Backolor = vbBlack
OptionButton2
:
Private Sub OptionButton2_Click()
UserForm1.Backolor = vbBlack
End Sub

.
6 ,
.

CommandButton1 ( ).
, .
,
,
.
,
, Ok,
.
Msgbox.
Msgbox
, .
:
Msgbox( .. )

CommandButton1_Click,
. :
Private Sub CommandButton1_Click()
Msgbox( .. )
End Sub

,
. .1.13.

OK .
34

- -

. 1.13.
CommandButton2 (),
,
, .

, :
Private Sub CommandButton1_Click()
MsgBox (" ..")
End Sub
Private Sub CommandButton2_Click()
End
End Sub
Private Sub OptionButton1_Click()
UserForm1.BackColor = vbWhite
End Sub
Private Sub OptionButton2_Click()
UserForm1.BackColor = vbBlack
End Sub
Private Sub OptionButton3_Click()
UserForm1.BackColor = vbRed
End Sub
Private Sub OptionButton4_Click()
UserForm1.BackColor = vbYellow
End Sub
Private Sub OptionButton5_Click()
35

- -

UserForm1.BackColor = vbBlue
End Sub
Private Sub OptionButton6_Click()
UserForm1.BackColor = vbGreen
End Sub
Private Sub OptionButton7_Click()
UserForm1.BackColor = vbMagenta
End Sub
Private Sub OptionButton8_Click()
UserForm1.BackColor = vbCyan
End Sub

. . 1.14 ,
. ,
.
8 , ,
, ,
.

. 1.14.

36

- -

1
1. ()?
2. Word
?
3. , Word
?
4. ,
?
5.
Word , VBA?
6. ,
VBA ?
7. , VBA
.
8. ?
9. VBA ?
10. ?
11. ,
?
12. ?
13. Visual Basic?
14. VBA ?
15. VBA
?

37

- -

2. VBA
2.1.
,
,
,
.
,

(), . ,
.

().
,
. .

Visual Basic.
.
, ,
. ,
, ,
(+ - * / < >
= <= >= <>).
:
x
Summa
faktorial
y1
z234
Summa_vklada
:
1y ( )
( )
Summa vklada ( )
x/y ( ,
Visual Basic ).

38

- -

,
, .
, , ,
Sum. , , l
( ), ,
, s,
v . .
(,
Turbo Pascal bject Pascal), Visual Basic
,
, . Visual Basic ,
, ,
Variant. ,
.
.
Visual Basic
, .. .

:
Dim Name
Name .
,
( Visual Basic
). :
Dim Name as Type
Type .
Visual Basic
:
Byte
0 255;
Boolean , .. ,
: True () False
();
Integer 32 768
32 767;
Long
2 147 483 648 2 147 483 647;
Single .
3. 402 823 * 1038 1. 401 298 * 1045
1. 401 298 * 1045 3. 402 823 * 1038 (
,
) ;
39

- -

Double
.
-1. 797 693 134 862 32 * 10308 -4. 940 656 458 412 47 * 10-324
4. 940 656 458 412 47 * 10-324 1. 797 693 134
862 32 * 10308 ;
Currency .
,
, 4 .
-922337203685477.5808 922337203685477.5807.
, , ,
;
String .
VB ,
, , .
, ,
.
:
Name = Value
Name ,
; Value ; =
.
( ).
, .
:
x = 10
s =
x ,
10, s
.

, ,
.
,
, .
, :
y = 5*8+30/6
,
( 45) y.
,
, ( ).
40

- -


Visual Basic . Visual Basic
, .
VBA,
. ,

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

. Val.
,
, .

, ,
.
. ,

.
Str, ,
.

VBA .
,
.
.doc,
VBA .
:
1. .
,
.
2. .
.
3. .
.

.

. ,
, .
41

- -


, .
.
:
.
. ,
,
.
ab .
, Textbox1.
Font , ,
, .
.
Textbox2 .
Textbox3
.
Label1, Label2 Label3.
Label1 : , Label2:
. Label3 :
.
: CommandButton1 c

,
CommandButton2

,
CommandButton3 CommandButton4 .

, .
. 2.1.

.

a,b s (a
, b , s )
:
Dim a,b,s As Integer
a b.
.

Textbox1.
Text Textbox1.
Val
a.
:
a = Val(Textbox1.Text)
42

- -


Textbox2 b, :
b = Val(Textbox2.Text)

, ,
s:
s = a*b

Str
:
TextBox3.Text = Str(s)
CommandButton1
:
Private Sub CommandButton1_Click()
Dim a, b, s As Integer
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
s = a * b
TextBox3.Text = Str(s)
End Sub
CommandButton2 ( ),

. Textbox1
Text ,
, :
Textbox1.Text = " "
:
TextBox2.Text = " "
TextBox3.Text = " "
:
Private Sub CommandButton2_Click()
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub

43

- -

. 2.1.


,
, .
:
Private Sub CommandButton1_Click()
Dim a, b, s As Integer
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
s = a * b
TextBox3.Text = Str(s)
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
End Sub
Private Sub CommandButton3_Click()
44

- -

MsgBox (" .. ")


End Sub
Private Sub CommandButton4_Click()
End
End Sub

.
. 2.2.


,
. ,
,
Ok .

. 2.2.

45

- -

2.2.

,
Visual Basic. , Visual Basic
,
( ).

\ (

). :
,
,
.
.
,
17 \ 5 3 ( 3,4,
0,4 ), 42 \ 8
5 ( 5,25,
).

. Visual Basic
Mod, ,
. 17 Mod 5
2.
.
, .
.
,
100
. , , 365
365 \ 100 3. ,
3.
100.
( ).
365 Mod 100
65.

10.
65 \ 10 6,
.
,
10, .
65 Mod 10 5, ..
46

- -

. , .
3, 6 5 14.

.
.doc,
VBA
.

. :
TextBox1
Label1, CommandButton1 ( ),
TextBox2 Label2,
CommandButton2 ( ),
, CommandButton3 ( ),
.

,
.
.
. 2.3.
.

. : x
; r1 ; r2
; r3 ; v
100 ( ); Sum
.
:
Dim x,r1,r2,r3,v,Sum As Integer
,
.
Val
x.
:
x = Val(TextBox1.Text)

r1,
100:
r1 = x \ 100

47

- -

. 2.3.


100 Mod
v:
v = x Mod 100
r2
v 10 r3
v 10:
r2 = v \ 10
r3 = v Mod 10
, ,
Sum:
Sum = r1 + r2 + r3

48

- -

, ,

:
TextBox2.Text = Str(Sum)
,
CommandButton1 ( ) :
Private Sub CommandButton1_Click()
Dim x, r1, r2, r3, v, Sum As Integer
x = Val(TextBox1.Text)
r1 = x \ 100
v = x Mod 100
r2 = v \ 10
r3 = v Mod 10
Sum = r1 + r2 + r3
TextBox2.Text = Str(Sum)
End Sub
CommanButton2 ( )
, ,
.. Textbox1 Textbox2
.
CommandButton3 ( ).
:
Private Sub CommandButton1_Click()
Dim x, r1, r2, r3, v, Sum As Integer
x = Val(TextBox1.Text)
r1 = x \ 100
v = x Mod 100
r2 = v \ 10
r3 = v Mod 10
Sum = r1 + r2 + r3
TextBox2.Text = Str(Sum)
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub CommandButton3_Click()
End
End Sub
49

- -


. ,
, . 2.4.

. 2.4.

,

.
.

50

- -

2
1. Visual
Basic?
2. Visual Basic
?
3.
Visual Basic?
4. , Visual Basic.
5. Visual Basic ?
6.
?
7.
?
8. VBA
?
9. Visual Basic
?
10. Visual Basic
?

51

- -

3. VBA
3.1. .



. , ,
.
VBA.
, .

. VBA
:
If Then 1 Else 2
:
, If.
- .
,
: True () False ().
, 1,
Then. ,
2, Else.

VBA.

If ... Then ... Else ,


.
. -
( )
, ,
(
) .

, .
:
If Then
1
Else
2
End If
52

- -


End If, ,
, .

,
.
, ,
( ,
).
.
( ) :
If Then

. , If,
, ,
Then (
). ,
If, ,
.

:
If Then

End If

.
:
If 1 Then
1
ElseIf 2 Then
2
Else
3
End If

. 1,
If. (
True), 1,
Then ElseIf.

53

- -

1 ( False),
. 2.
True, 2,
Then Else.
2 , 3,
Else End If.

, . 3.2.
.
,
, . ,
10000 .,
5 % .
,

.
10000 .,
,
.
:
1. . Textbox1
. Textbox2 .
,
Textbox2
. ,
10000 .
Textbox2 ,
Visible
False.
,
,
.
Textbox3
,
. ,
, Textbox3,
, Textbox1.
2. Label1, Label2 Label3,
.
Label1 ( ) ,
( )
. Label2 ( )
54

- -

Textbox2, ,
, 10000 .
Label3 .
3. CommandButton1, CommandButton2,
CommandButton3

CommandButton4.

CommandButton1 ()
. CommandButton2
()
,
. CommandButton3
( )
. CommandButton4
() .
.3.1.
CommandButton1.
CommandButton1_Click,

: Sum
; Skidka , ; Itog
,
. Sum , Skidka
Itog .
Val
, Textbox1,
Sum.

If : Sum>10000.


Sum. 10000,
:
1. , 5 % ,
Skidka.
2. ,
.
Itog.
3. Textbox2
Label2.
4. Label2 : .
5.
Str
Textbox2 Texbox3.


Then.
55

- -

. 3.1.

10000,
:
1. Itog , Summa,
,
Itog
Textbox3.
2. Label2
: , .

Else.
, ,
:
If Sum > 10000 Then
Skidka = Sum * 0.05
TextBox2.Text = Str(Skidka)
TextBox2.Visible = True
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)

56

- -

Label2.Caption = "
"
Label2.Visible = True
Else
Itog = Sum
TextBox3.Text = Str(Itog)
Label2.Visible = True
Label2.Caption
=
",
"
End If

,
CommandButton2. :
1. TextBox1,
TextBox2 Textbox3.
2. Label2 Textbox2
.
Text
.
Visible
False.
CommandButton3 ( ) CommandButton4 ().
,
.


. :
Private Sub CommandButton1_Click()
Dim Sum As Integer
Dim Skidka, Itog As Double
Sum = Val(TextBox1.Text)
If Sum > 10000 Then
Skidka = Sum * 0.05
TextBox2.Text = Str(Skidka)
TextBox2.Visible = True
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
Label2.Caption = "
"
Label2.Visible = True
Else
Itog = Sum
TextBox3.Text = Str(Itog)
Label2.Visible = True
Label2.Caption = ", "
57

- -

End If
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Label2.Visible = False
TextBox2.Visible = False
End Sub
Private Sub CommandButton3_Click()
MsgBox (" .. ")
End Sub
Private Sub CommandButton4_Click()
End
End Sub

,
.

.3.2.

58

- -

3.2 ,
,
10000 .

3.3.

. 3.3 ,
.
,
,
.

59

- -

3.2. .


,

.

If...Then...Else ( ,
). ,
Then Else (
), ,
.
.
,

.


Select Case.
,
- ( to select
). :
Select Case N
Case N1
1
Case N2
2

Case Nk
k
Case Else
k+1
End Select
Select Case ,
; N -;
N1,N2...Nk -.
- ,
Case ,
-.

.
60

- -

1, 2, k.

Visual Basic, . - N
,
k+1, Case Else.

End Select.

.
. ,
Case Else
k+1, .
: - N
,
, ,
.

.
-,
.
,
.
:
Case N1, N2,....,Nt
x
N1, N2,....,Nt -;
x ,
.
-
, :
Case Nn to Nk
y
Nn -;
Nk ; y
, .
Case
. ,
.

.
1000 . .
1000 4999 . ,
61

- -

3 %. 5000 19999 .
, 5 %.
20000 . ,
10 %. ,
4
,
-.

,
, 3.1
. 3.1.
,
.
, : Sum
, Skidka ,
Itog .
,
.

If ... Then ... Else,
.
,
. Sum.
( 1000 ., ,
, ) Sum 1 999 .,
: Case 1 To 999.
:
1. ,
, c
Caption Label2.
2. Textbox2,
, .
3. Textbox3, ,
, Textbox1, .
, ,
:
Case 1 To 999
Label2.Caption = ", "
TextBox2.Visible = False
TextBox3.Text = Sum

62

- -

1000 4999 . (
3%- ), -
Sum : Case 1000 To 4999.
-
:
1. ,
, Caption
Label2.
2. , 3 %,
0.03
Skidka.
3. ( TextBox2),
.
4. Texbox2
(
Str).
5.
Itog.
6. Textbox3,
Str.
, :
Case 1000 To 4999
Label2.Caption = " "
Skidka = Sum * 0.03
TextBox2.Visible = True
TextBox2.Text = Str(Skidka)
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
5000 19999 .,
- Sum : Case
5000
To
19999.
,
0.05 ( 0.03 ,
).
, :
Case 5000 To 19999
Label2.Caption = " "
Skidka = Sum * 0.05
TextBox2.Visible = True
TextBox2.Text = Str(Skidka)
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
63

- -

,
20000 .
-:
Case Is

(, , , , , )
, . 20000 .
:
Case is > 20000
,
, ,
0.1.
:
Case Is > 20000
Label2.Caption = " "
Skidka = Sum * 0.1
TextBox2.Visible = True
TextBox2.Text = Str(Skidka)
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
,
:
Private Sub CommandButton1_Click()
Dim Sum As Integer
Dim Skidka, Itog As Double
Sum = Val(TextBox1.Text)
Select Case Sum
Case 1 To 999
Label2.Caption = ", "
TextBox2.Visible = False
TextBox3.Text = Sum
Case 1000 To 4999
Label2.Caption = " "
Skidka = Sum * 0.03
TextBox2.Visible = True
TextBox2.Text = Str(Skidka)
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
Case 5000 To 19999
64

- -

Label2.Caption = " "


Skidka = Sum * 0.05
TextBox2.Visible = True
TextBox2.Text = Str(Skidka)
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
Case Is > 20000
Label2.Caption = " "
Skidka = Sum * 0.1
TextBox2.Visible = True
TextBox2.Text = Str(Skidka)
Itog = Sum - Skidka
TextBox3.Text = Str(Itog)
End Select
End Sub
, (

)
2, 4 ,

.

65

- -

3
1. Visual Basic.
2.
Visual Basic?
3.
Visual Basic?
4. ,
?
5. ?
6. ?
7.
?
8. ,
?
9. -,
?
10.
?

66

- -

4. VBA
4.1.


. Visual Basic (
) ,
. Visual Basic :

.
,
.
Visual Basic :
For i:=a To b

Next i
,
. i
. .
a b,
a , b.
,
. Next i
, , .
:
a,
. i .
,
. ,

,

i b.
, .
, ,
. ,
b-a+1.
,
. -,
a b
, ,
67

- -

. -,
,
. , ,
,
Step
.
:
For i:=a to b Step k,
k .

,

, .

Step, k,
.
i
k. ,
a ,
b.

:
n, .
.
n 1 n.
: n!.
, 5 5!
:
5! = 1*2*3*4*5 = 120.

n (, n
12). , ,
.

:
1. . Textbox1
n Textbox2
n.
2. Label1 Label2,
, . 1.

68

- -

3. ( CommandButton).
(CommandButton1)
,

. (CommandButton2)
, .
(CommandButton3)
.
(CommandButton4) .

. 4.1.

. 4.1.
.
. n
f.
Long,
2 . n
,
Textbox1.
n.

If.
69

- -

Then ,
n 1 12 (
Or ).
,
.
2 Long,
.
Msgbox ,
: .
1 12.
, ,
f.
f , 1.
.
.

1 n.
i.
Visual Basic
:
f = 1
For i = 1 To n
f = f * i
Next i
, ,

Textbox2.
:
Private Sub CommandButton1_Click()
Dim n, f As Long
n = Val(TextBox1.Text)
If (n > 12) Or (n < 1) Then
MsgBox
("
.

1 12")
Else
f = 1
For i = 1 To n
f = f * i
Next i
TextBox2.Text = Str(f)
End If
End Sub

70

- -

. 4.2.

4.3.

71

- -

,
,

.
:
Private Sub CommandButton1_Click()
Dim n, f As Long
n = Val(TextBox1.Text)
If (n > 12) Or (n < 1) Then
MsgBox
("
.

1 12")
Else
f = 1
For i = 1 To n
f = f * i
Next i
TextBox2.Text = Str(f)
End If
End Sub

Private Sub CommandButton2_Click()


TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub CommandButton3_Click()
MsgBox (" .. ")
End Sub
Private Sub CommandButton4_Click()
End
End Sub
. 4.2 4.3
. , .4.2,
,
.
Ok,
( ).
, . 4.3, ,
.

72

- -

4.2. .

,

.
,
,
.
.

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

,
.

:
1. .
.
Textbox1 , Textbox2 ,
( ),
Textbox3 . Textbox4
.
2. Label1, Label2, Label3 Label4,
.
3. .
(CommandButton1) ,
, ,
.
(CommandButton2) ,

.
.
4. .
Clipart,
Microsoft Office.
Coins.wmf, ,
.

73

- -

, , ,
.doc. VBA,
. ,
, . 1-3,
,
, 4
.

4.4. ,
, Image
( )
,
(Toolbox). :
.
, Image.
Image1
.
.
, - ,
(Properties) Image1
Picture. None ().
Picture, ,
74

- -

- ( ).
-, Load
Picture, (. 4.4).

. ,
, ,
Load Picture.
, ,
( ,
) .
(
Coins.wmf), ,
.
Image1 .
Image1 ,
, .
, ,
Image1. :
, ,
.
,
,
.
Image1 PictureSizeMode (,
).
0,
. 1,

, Image1.

.
, , . 4.5.

CommandButton1 ( ).
: n (
) p ( )
, Summa ( ) Sum (
)
.
Summa Textbox1 (
Val
), n Textbox2, p
Textbox3.
75

- -


.
,
, ,
. 1 n.
:
For i = 1 To n
Summa = Summa * (1 + p / 100)
Next i

. 4.5.
, ,
Textbox4.
:
, ,
, ..
, . Summa
.
Visual Basic
.
FormatNumber, .
:
76

- -

FormatNumber(Expression, NumDigitsAfterDecimal)
Expression ,
.
( ).
NumDigitsAfterDecimal
( ),
.
- .
Sum:
Sum = FormatNumber(Summa, 2)
Sum (
)
Str Textbox4.
CommandButton2 ()
CommandButton3 () ,
.
:
Private Sub CommandButton1_Click()
Dim n, p As Integer
Dim Summa, Sum As Double
Summa = Val(TextBox1.Text)
n = Val(TextBox2.Text)
p = Val(TextBox3.Text)
For i = 1 To n
Summa = Summa * (1 + p / 100)
Next i
Sum = FormatNumber(Summa, 2)
TextBox4.Text = Str(Sum)
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End Sub
Private Sub CommandButton3_Click()
End
End Sub

77

- -

. 4.6 ,
.

. 4.6.

.

78

- -

4
1.
?
2. Visual Basic
?
3. ?
4.
.
5. ?
6. Visual Basic
?
7. ,

?
8.
, VBA?
9.

?
10. VBA
?

79

- -


,
, ,
.
3 4 ,

.
, -
,
,
.
1.
:
a) ;
) ;
) ;
) .
2.
) ;
) ;
) ;
) .
3.

) F6;
) F7;
) F8;
) F9.
4.
,
:
) ;
) ;
) .
5. VBA
,
) Insert File;
) Insert Module;
) Insert Procedure;
) Insert UserForm.
80

- -

6. VBA ,
) BackColor;
) ForeColor;
) TextColor;
) TextBackground.
7. VBA ,

) BackColor;
) ForeColor;
) TextColor;
) TextBackground.
8. VBA ( ,
, ) ,
) Caption;
) Font;
) Text.
9. VBA ,

) ;
) ;
) .
10. (
) VBA
) ;
) ;
) ;
) .
11. VBA Visible,
:
) 2
) 3
) 4
) 5
12. Visual Basic

) ;
) ;
) ;
) .

81

- -

13. VBA , -
Ok, c
) Msgbox;
) Txtbox;
) Toolbox.
14.
Visual Basic
) 1y;
) ;
) Sum2.
15. Visual
Basic
) Dim;
) Var;
) Variable.
16. Visual Basic
) Double;
) Long;
) Single.
17. Visual Basic

) Str;
) StrtoInt;
) InttoStr;
) Val.
18. Visual Basic
:
) Str;
) StrtoInt;
) InttoStr;
) Val.
19. Visual Basic
:
) /
) \
) |
) :

82

- -

20. Visual Basic


:
) End;
) End If;
) Exit;
) Else If.
21.

) If;
) Then;
) Else.
22. Visual Basic

) Case of;
) Select Case;
) Case Else.
23. :
)
) ;
) ;
) .
24. c Visual Basic

) Goto I;
) Next I;
) Step I.
25.
,
) Val;
) FormatNumber;
) Str.

83

- -


. Visual Basic. .: , 1998.
.. VBA Office 2000. .:
, 1999.
.. VBA. .: , 1999.
.. . :
, 1997.
. Microsoft Office 2000. . .: , 2000.
. / .. . .: ,
2001.
.. Excel 5 Excel 7 Visual
Basic. .: , 1996.
.. Visual Basic 6.0, Visual Basic For Applications 6.0.
. .: , 2000.
.. VBA 2002. .: , 2003.
. .. MS Word. .: -, 1999.
.., ..
Borland Delphi: / . ., 2004.
.., .. Delphi . .: , 2005.
. . Visual Basic 5 (VBA) . .:
-, 1997.
.. Excel-VBA. - . .:
, 2008.
.. VBA Microsoft Office 2007.
.: , 2007.
. Microsoft Office 97: . .: , 1998.

84

- -


Microsoft Word
Visual Basic for Applications

..
. ..
.

25.10.2010.

2010 ., . 104
60x84/16.

. 5,5 .-. . ;
. 104.

5,5 . . .

. 1.
100 .

-

198095, ., . , 4.

85