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

, Visual Basic for Applications.

1.
1.1.

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

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

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

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

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

1.2.

1.2.1.


. - .

; ( VBA)
( ) .

1.2.2.

:
<> < > <>,
< > (, ,
VBA). :

( , ). ,
( VBA):
= 4 + 7

= + 2

= 2

= *3 +

: 4+7, 11 .
, 11.
2, 13 (11+2=13).
, 13.
2. ,
: *3+ (
13 2), 2*3 + 13 = 19
(, 19 ).
: =19, =2.
()

. , , ,
5

19
() a, 2 .
.
-
. . ,
= *3+ :

= *3 +

= 4+7

= +2

=2

=*3+

1.2.3. ()


( ) .
:

<> <> <> <1> <> <2>,

, 1, (
), 2. , ,
, if,
then, else. VBA :

If <> Then <1> Else <2> End If

, <> <
2>. , ,
. -
:

+
-- +
--

1 2 1

+ - ( )
( ). , y, :
2

y x x 0, -:

x 1, x 0

+ -
x<0
?

y = x*x y = x+1

VBA :

If x<0 Then

y=x*x

Else

y=x+1

End If

, Then ( +) Else (
-) ,
End If .
:
, ; ,
. ,
7

S . -
:

a<0

a = 1

S=S+a

VBA :

If a<0 Then

a=1

End If

S=S+a


, (
). , :

x=10

If x>5 Then

x=x-5

If x>7 Then

x=x+4

y=x

Else

x=x*x-1

y=x+8

End If

Else

x=x-4

y=1

End If

: x
10. x>5? ,
Then: x 5,
() x>7? ,
, Else: x=x*x-1 y=x+8,
x=24, y=32. If , Else
8

If , : x=24, y=32.
, , x=10 x=2,
,
Else: x=x-4, y=1, :
x = -2, y = 1.
- :

x=10

+ -
x>5 ?

x=x-5 x=x-4

+ - y=1
x>7 ?

x=x+4 x=x*x-1

y=x y=x+8

?
, .
: (>), (<), (>=),
(<=), (<>).
( VBA And), (Or),
(Not), (Imp). , ,
, (x<5) And (x>=2).

1.2.4.

. ,
20- , . :
20 . : ,
, , 0,
, () :
.
, : (),
.
9

: ,
.
,
. ,
, , .
, , ,
. ,
, , 4<7, ,
, . , ,
, , 2<0, ,

.

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

+
-

(, ).
VBA:
While <> < > Wend

Do While <> < > Loop)

, :

a = 7

While a > 0

a = a - 1

Wend

=0, .. a = a1
, a>0. ,
10

a=1, a=11=0. (0>0)


, Wend (
While). , 0.
a=7 a=0, , ..
a>0 .
,
. ,
. , , ,
,
. , , .
.

. ,
.
:

,
, , ..
.
Until-,
( , ).
VBA:

Do < > Loop Until <>.

, Until-:

a = 7

Do

a = a - 1

Loop Until a < 0


: =-1, .. , 0,
( a<0 ).
a=7 a=0,
, .. a=a1,
11

( -1, a<0
).
,
,
, , . ,
, , .

. ,
, , 20, 100, N,
-
. .
( )

, , 0.15. ,
.
For, ..
For. VBA :
For <-> = < > To < >
< >
Next

, , 1:
For <-> = < > To < >
Step <>
< >
Next

- , ;
, , ,
, ,
, , ,
(
,
).
.
,
. 1,
, , .
, ,
.
.
i i0, N,
h. - :
12

i=i0

iN

i=i+h

,
While. -For VBA:
k = 2

m = 4

For i = k + 1 To m * 2 + 1 Step 0.5

k = k + i

Next

, i, 3-,
, 9 (4*2+1=9);
0.5, 7,
13. k = 80. , k
, k+1=3
.
, For: ,
- ,
, , ,
.
.
. ,
,
. VBA
Exit, For Exit For, ,
Do, Exit Do. ,
a = 7

Do

a = a 1

If a = 5 Then

Exit Do

End If

13

Loop Until a < 0

5, . . ,
.
For:
k = 2

m = 4

For i = k + 1 To m * 2 + 1 Step 0.5

k = k + i

If k = 5 Then

Exit For

End If

Next

.
. .
( ) ,

. , ,
:

-
1

+
-
2

( 1)
( 2).
.
. ,
X, Y, i-
i- xi X yj
14

Y, .. , (
2 ).

VBA:

For i = 1 To 7

For j = 1 To 5

If i <= j Then

Cells(i, j) = 1

End If

Next

Next

Cells(i, j) Excel, i
j. Excel:

: ,
,
.

, ,
(
) .

1.2.5. -

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

x, y
x, y




15

VBA x
InputBox, :

x = InputBox( x)


MsgBox, :

MsgBox (x)

x, , 5.

MsgBox x =&x

: x = 5.
, Excel.
Cells(i,j), i , j .
, x 1 :
x = Cells(1,1),

x , , 2 ( )
:

Cells(2,3) = x

1.3.

,
. ,
, . ,

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

,
, ,
.

, . ,
(Integer VBA)
( ).
(String)
. (Boolean)
True () False ()
(Not), (And), (Or),
(Imp).

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

. , .., . ,
2109 3040 ,
. ()
. :
.

: . . = ,
, , : .,
., .=3.
A1:A3 Excel VBA :
Range("A1:A3").Copy. Copy
. Excel (
17

), Excel
, , , , Integer.

:
(, ,
);
(
, , );
(,
);

(, , );
,
( , Excel, ,
..);
, (, ,
, , , ).
,
, . ,
, () (
, ), .
,
. .
, Integer ,
Double ( ) ,
210 = 20 .
,
, ( )
, ( ). ,
a=b b ,
a. ,
, , b ,
a. ,
. ,
. ,
a=b a Integer b Double
b.
, ,
.
, (
). ,
,
( ).
,
18

,
. VBA
Dim :

Dim < > As < >.

, Dim a As Double, b As Boolean, c As Integer.


, .

. , ,
.
, . ,

. , ,
, .
() .
, VBA CStr (
String), CInt ( Integer).
CInt(x) x ( x
) x ( ,
, x=123 123). , ,
x=abcd , CInt(x)
. CStr(x),
x, , x=35.4 35.4,
, ,
(&) : CStr(x)&"abcd" 35.4abcd.

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


, ,
. , b+7*(d-c) b, d, c,
, ,
.
, ,
() .
19

,
.
.
. ,
, , .
() .
. ,
. ,
, .
.
, , ,
, , (,
) . , VBA: (3) .
: 24, 8, 6, 77, 3.
(3) , 6, .. (3) = 6.
:

(1) (2) (3) (4) (5)


24 8 6 77 3

, , (3) = (5),
(3), (5), 3, 6 .
(i) i- .
- ,
, ,
, M(4,3)
M.

, . , ,
(2) = . ,
,
.
, ,
, . , VBA
, :
Dim A(10) As Integer,

:
Dim M(5,7) As String

20

, ,
.
(,
, , ,
, ). , ,
: Dim A(10) As Integer, Dim (10) As
Integer , i = 1, 2, , 10
(i)=(i).
: A = B.
: Dim (10) As Sin
gle, ,
(i)=(i) - .
, , , Dim (20) As In
teger, ,
(i)=(j), (j)=(i) i 10, j 20 , ..
(Integer).
(i) i > 10 , .. .

. ()
.
, .
,
(, ). ,
: ( ), ( ), (
) ( S), :
( ), ( ), (
), ( ) (
D). VBA :

Type S Type D

As String As String

As Integer As String

As Single As Integer

End Type As Single

End Type

, Dim As S, Dim B As D,
.
-
( ), , .. ,
, ,
2 , 300.
:
21


. . .


2 300

, :
. =

. = 3

. = . + 200


. . .


3 500

, S,
, :


. . .


4 600

: . = ., . =
. =.
D :


. . . .


48 150.5

, ,
, , . = .
, :
= .
, : Dim
M(10)As S. i-
22

: M(i)..
, - , .
:
,
.
, , . , ,

.
,
- ,
. .
, ,
: . ( )
,
, .
,
() ().
( ),
(
, ).
-
.
, (,
).
( ),
( ,
).
.
- -
: : put(elem) set(elem)
get(). get . ,
Q , Q.get
, .
S S.get
, . .
,
, , (
) () .
,
. ,
, : , , , ,
, d 20 .. get
- G ,
.
23

, , , ,
,
.
, , , , ,
-
, , on-line .

1.4.

: +

- ()

-

, ,

/
(, ,
)

:
1. :
, .
2. ,
. .
3.

. , . ,
,
.
4. , ,
, .
.
5. ,
, , .
( ), x, y, z
24

, ,
.
6. , ,
, - , ,
- - .

, , .
.
7.
. ,
, , ,
.
8. :
- , , , ,
, (, If
),
.
.
9. :
, . ,
, .
-
, ..

1.4.1.

, .
, ,

.
.

(),
.
.
,
. ,
Y, G
, Z,
R, P(X)Y G(Y)Z R(Z)
, ,
,
, .. . , , , Y P
, , G
25

, G(Y) ,
.
, (, ,
input- ) (, , output- ).

,
.
. , ,
, Integer,
String, Single,
(Integer, String) Single. R
.
, (
) .
, .
- :
, .

, .., , P (,
) = <>.
, .. -
.
, .. (x,y) x y.
a b
, , 0.15 + P(a,b) -2.
P(a,b)
P a b.
,
.

1.4.2.

P(x,y,z) P, x, y, z
(, , ) .
P.
.
(
). , ,

, ,
, (, void C++).
. ,
( , ,
..), . , .
VBA:
26

Function < > (< >)

< >


Procedure < > (< >).

: <
> < >, .
, , n! n,
VBA : Function fact(n As Integer) As Integer.
,
, : Procedure Sum(x As
Single, y As Single).
,
, . ,
fact:
Function fact(n As Integer) As Integer

fact = 1

If n > 1 Then

For i = 1 To n

fact = fact * i

Next

End If

End Function

Sum:

Sub Sum(x As Integer, y As Integer)

Dim S As Integer

S = x + y

MsgBox S

End Sub

, ,
,
, . ,
, Pascal, Modula, Modula 2, Algol.
(
, , , . .).
++, VBA , ..
, .
, ,

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


, .
VBA :
< > < >

Call < >(< >).

:
< > < >.


, .
, , , .
,
, .
, , ,
, .. ,
.
.
; ,
,
(, ).
.
, ( )
, , .
,
, .
, , Sum
fact, ( ):

Sub vysov()

Dim k As Integer

k = fact(5) fact
5, 120
k = fact(4)+10+k fact
4, 64
MsgBox k
Sum 5, 10 Sum
5 10
Call Sum(10,20) Sum
10 20
Sum k, 2 Sum
k 2
End Sub

28

,
,
. ,
Call Sum(2.5, 4) - 2.5
.

, , ,
. VBA.
,
: .

, , .

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

.
,
, (extern-), (common-) .
. VBA ,
,
. , :

Dim a As Integer, b As Integer

Sub P1 (x As Single, y As String) As Single

a = 7

End Sub

Sub P2 ( x As Integer) As Boolean

b = a*3 + x

End Sub

a b
P1 P2 .
29

Public, .. Public a As Integer, b As Integer


( )
.
Public Private,
,
, : , ,
. , Public , Private
. ,
Private.
VBA :
, ,
. Dim Static
.
,
Dim Private, .
.
,
Public, (, )
.

.
() ,
, ,
. ,
,

. ,
,
. , ,
.

. ,
x&y x (x y) x=1, y=0; x=0,
y=1; x=1, y=1:

Sub P ( )

Dim x As Boolean, y As Boolean, res As Boolean

x=1

y=0

res = x And y Or x Or Not( x Imp y)

MsgBox res

x=0

y=1

res = x And y Or x Or Not( x Imp y)

MsgBox res

30

x=1

y=1

res = x And y Or x Or Not( x Imp y)

MsgBox res

End Sub


:
Sub P ( )

MsgBox F(1,0)

MsgBox F(0,1)

MsgBox F(1,1)

End Sub

Function F (x As Boolean, y As Boolean) As Boolean

F = x And y Or x Or Not( x Imp y)

End Function

(, ,
)
F .
, ,
.

2.


, - . ,
Microsoft Word ( ),
Excel , ,
Paint ,
. -, , ,

..

, .
,
, ,
, ,
.
Visual
, .
31
2.1. , , , VBA

VBA . VBA
100 ( (WorkBook), (Work-
Sheet), (Cell), (UserForm),
(TextBox, CommandButton, Label .), . .).
VBA.
, . ,
.
Visible. , ,
. . ,
. , .
, ,
, , , ,
, .
:

,
. , ()
Show Hide .
, ,
.
:

,
, ,
, ,
. VBA
:

Range ( )
Cells
Sheet
Worksheet
DialogSheet

.
. ,
, Worksheets.
: -
32
, (, Delete),
. ,
Worksheets ('''')

.
:

Sheets
DialogSheets
DrawingObjects

,
. -
. ,
.
.
:
. =

Value
_ 0,1 (..
0.1):
Range('' '').Value = 0.1

, MS Excel 0,1
(, ), VBA - (.).
2
Formula ():
Range(''A2'').Formula= ''(1:1)''

, . .
. , ,
. , , , Row
() Column () . ,
, ,
.
:
= .

_
1 :

_ = Range(''Al'').Value
33


_ = Cells(1, 1).Value

, , , .
. , . ,
Clear, .
,
__:
Range('' '').Clear


Range(''10:12'').Select

10:12.
Excel ,
. , ,
. .
Application ().
, MS Excel. ,
Excel, Quit () Ap
plication:
Application.Quit

, ,
.
. ,
5- :
Application.Workbooks('''').Worksheets('''').Rows(5).Delete

, .
:
Application,
;

;
VBA ,
, .


. , .
ActiveCell.Value = ''''
34

ActiveCell ( ), ActiveSheet ( ), Acti


veWorkBook ( ) Selection ( -
) , .

, (
, ),
.
, .

: .
, , Initialize, Load, Click DblClick.
:

Sub UserForm_()

End Sub

, :

Sub UserForm_Load()

Show

End Sub

Unload, MouseDown, MouseUp


Down. :

Private Sub Frm_()

End Sub


(, , , ..).

:

_()

,
Private Sub _Click()

End Sub

35
2.2.

(.1),
:
.
.
.
.
.
.
.
.
.
. 1.
.
.
.
.
.
RefEdit.
2.2.1. (TextBox)

TextBox ,
.
TextBox .

,
.

, , ,
,
. Text
Box1, TextBox2 . .

, ,
, .
:
Txt

, txtAge , , a
txtFirstName , .

Value. Variant.
, txtFirstName :
36

TxtMyFirstName.Value ='''' '

TextBox
:
Dim X As Variant

X=txtFirstName.Value

(,
TextBox
, , ),
Enabled, False.
True, .
TextBox1.Enabled=False

2.2.2. (Label)

Label ,
,
Caption.
.
, .
Caption.
:
Label1.Caption=''''
Label1, Label2 . .

2.2.3. (CommandButton)

CommandButton
, , .
CommandButton1, Com
mandButton2 . .
(Name).
,
CommandButtonN ( N

). Caption. :
CommandButton1.Caption='' ''

CommandButton AutoSize.
, True, , Cap
tion, . :
CommandButton1.AutoSize=True

37

,
. , ,
,
. Enter,
.
. ,
True Default. Default
False.
:
CommandButton1.Default=True

,
, ,
(Click). Click .
, Debug
, Command
Button1.

Private Sub CommandButton1_Click()

Debug.Print " "

End Sub

:
, .
, , .
.
False Enabled.

2.2.4. (ListBox)

ListBox
, .
ListBoxl, ListBox2 . .
:
1 ,
2 ,
3 .
MultiSe
lect, ( )
.


1 0 fmMultiSelect
38

2 1 fmMultiSelectExtended
3 2 fmMultiSelectMulti

,
:

ListBox1.MultiSelect=fmMultiSelectExtended

AddItem.
,
:
ListBox1.AddItem

, , ,
, (a(i)), .
AddItem
:

Public Sub Months()

ListBox1.AddItem "January"

ListBox1.AddItem "February"

ListBox1.AddItem "December"

End Sub

Public Sub NumberList()

For i=1 To 20

ListBox1.AddItem i

Next i

End Sub

,
, .
, Text ListBox
, Text .
ListIndex .
, , Debug

Debug.Print ListBox1.Text

, ,
39

, , .
Selected,
True, , False .
, ,
:

For i = 0 To listBox1.ListCount-1

If ListBox1.Selected(i) = True Then

S = S + ListBox1.List(i)

End If

Next

ListCount ()
. 0, ListCount-1.
List .
RemoveItem,
.
,
:
Public Sub NumberList()

For i=0 To ListBox1.ListCount-1

ListBox1.RemoveItem i

Next i

End Sub

2.2.5. (ComboBox)

,

. ComboBox
,
. ComboBox
ListBox , .
.
, :
; ,
May,
;
; ,
; , ,
.

ComboBox ,
, ,
40
.
ComboBox.
ComboBox Style,
(0, 2), :


0 fmStyleDropDownCombo
6op 2 fmStyleDropDownList

AddItem.
, ComboBox,
Value Text.
,
Debug ,
ComboBox:

Debug.Print ComboBox1.Value

Debug.Print ComboBox1.Text

Text
:
ComboBox (
,
);
ListIndex ComboBox
, .

2.2.6. (Checkbox)

CheckBox .
(). ,
, , ,
, .
( ), Value CheckBox
True, False.
Click
, .
( Enabled),
( Value) Null.
: CheckBox1, Check2 . .
Name . Cap
tion (),
41

CheckBox. ,
, WordWrap
True.
CheckBox , ,
:

Public Sub CheckBoxl_Click()

If CheckBoxl.Value=True Then

Else

2
End If

End Sub

2.2.7. (OptionButton)

OptionButton
.
.
.
OptionButton
.
OptionButton1, Option
Button2 . .
:
(Frame).
OptionButton, ,
.
.
GroupName.
OptionButton
, GroupName
OptionButton.
GroupName
Frame. GroupName
, .
Value True.
, , :
Public Sub CheckOptionButton()

If OptionButton1.Value Then

1
Elself OptionButton2.Value Then

2
42

Elself 0ptionButton3.Value Then

3
End If

End Sub

2.2.8. (Frame)

Frame
. Frame1, Frame2 . .
Name. Cap
tion (), .
:
Frame1.Caption =" "

3.

VBA, ,
, Visual Basic <Alt>+<F11>.
: Insert, Mod
ule, Insert Procedure.
Name ( ),
, Function (. 2), Ok.

. 2.


:
Public Sub ()

End Sub

43

() .
Run
Run Sub/UserForm .
,
.
, .
.
Debug
VBA,
.

1.

1. y,
y = x2 + x - sin(x) x.

. : x, :
y. : 1) x, 2)
y , 3) .
x. -
x:

y = x2 + x - sin(x)

y
x
: , ,
Excel.
, : .
x y Single ( Integer y
, .. sin(x)
).
.
MsgBox.
.
& .
MsgBox (String),
(
Single String).
44


. VBA Sqr(x) Sin(x). ,

( 1/2).

Public Sub Task1()

Dim x As Single, y As Single


x = 2.4
y = x^2 + Sqr(x) - Sin(x) , sqr
MsgBox y

x = InputBox("Vvedi x") c
y = x^2 + x^(1/2)-Sin(x)
MsgBox "y=" & y y=
x = Cells(1, 1) 1 Excel
y = x ^ 2 + Sqr(x) - Sin(x)

MsgBox "y=" & y

End Sub

, ,
- 1 ,
x=Cells(1,1) 0.
: ,
(>0).
, x^(1/2) Sqr(x) ,
.
, , :
, :

if x < 0 Then

MsgBox

Exit Sub

End if

2. .
. . ,
:
. :
x = y !
y = x

,
y=x ,
x=y. , .
,
45


( ).
- rab. :

rab = x

x = y

y = rab

Sub ()

Dim x As Integer, y As Integer, rab As Integer

x = 10

y = 7

rab = x

x = y

y = rab

MsgBox (x)

MsgBox (y)

End Sub

:
1. y , B, ,
Excel, x, z, :
A 3B xA C 1
) y (A C)cos 2 x z ; b) y x 1 x ;
A B B A 1
2. .
.
3. .
.
4. , S,
.

2.

3. , .
. : , a b.
, . : a b: a>b ,
a, - b. -
:
46

a
b. a,b
, .. Integer.
- +
- a > b
,
InputBox
.
MsgBox. a b

Public Sub min2()

Dim a As Integer, b As Integer

a = InputBox("Vvedi a")

b = InputBox("Vvedi b")

If a > b Then

MsgBox "min = " & b

Else

MsgBox "min = " & a

End If

End Sub

4. , .
. : , a, b, c.
: .
.
If Then Else , If
Then, Else. - :

a,b,c

- a > b +

- b > c + - a > c +

Max = c Max = b Max = c Max = a

Max
47

Integer.
:
Else

Max = c
Else: Max = c, VBA.

Public Sub max3()

Dim a As Integer, b As Integer, c As Integer, Max As Integer

a = InputBox ("Vvedi a")

b = InputBox ("Vvedi b")

c = InputBox ("Vvedi c")

If a > b Then
If a > c Then If
Max = a
Else: Max = c
End If If
Else
If b > c Then If
Max = b
Else: Max = c
End If If
End If
MsgBox Max
End Sub

:
1. , , .
: ) ; ) 2:4
Excel; ) .
2. x, y, z. , : )
; ) .
3. 2:11 Excel.
(
), 13%
5000 ., 20% , 5000 40000
. 30%, 40000 .
4. x, y, z.
.

3.

5. N ,
.
. ,
.
48

. S k,
. , N
.
Mod,
: x Mod y -
x y; 0, x y. x y ,
.
: , : ,
S k 1.
N .
, S k .
r, Single
, ..
:
Sub ()

Dim S As Integer, r As Single, k As Integer, a As Integer

N = InputBox(" N ")

S = 0

k = 0

For i = 1 To N

a = InputBox(" ")

If a Mod 2 = 0 Then

S = S + a

k = k + 1

End If

Next

r = S / k

MsgBox (r)

End Sub

, S k
(), . , ..
,
- , - .
S = 0, S = S + a
a S
. ,
, :

For i = 1 To N

S = 0 !
k = 0 !
a = InputBox(" ")

If a Mod 2 = 0 Then

S = S + a

49

k = k + 1

End If

Next

,
S a, k
1.

6. ,
, 0.
. , ,
While:
Sub ()

Dim a As Integer, P As Integer

a = InputBox(" ")

P = 1

While a <> 0

P = P * a

a = InputBox(" ")

Wend

MsgBox (P)

End Sub

,
1, .. P = P *a
.
, 0 .
, . , -
0?
=1. , 1, 0.
? , , ,
Until c :

Sub ()

Dim a As Integer, P As Integer

Do

a = InputBox(" ")

Loop Until a <>0

P = 1

While a <> 0

P = P * a

a = InputBox(" ")

Wend

MsgBox (P)

End Sub

50

,
: a <>0.

7. 10- ,
.
. ,
(max),
. ( -
) max ,
max, (
) max, . ,
, :
,
( , ). (
) max .
max?
, , .
, , 0. ,
(
). , , 0
, . .
,
9- .
, For:

Sub ()

Dim max As Single, a As Single Single


max = InputBox(" ")

For i = 1 To 9

a = InputBox(" ")

If a > max Then

max = a

End If

Next

MsgBox (max)

End Sub

,
< ( ):
max , , min.

:
1.
20 , .
2. sin(tg(n)) n,
: n=1, 2, 3,
51

3. y=sin(x)+4cos(x-2) x,
[-15,10] 1.
4. , , ,
, .

4. ( )


Y,
Yi F (Yi 1 ) , .. Y
. -
.

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

8. Y, :
Y 1024 512 ... 4 2 .
. ,
:

Y 210 29 ... 2 2 21 .
Y ,
. i : 2i ,
,
. , i- : Y 2 i Y .
Y=0. :

Sub ( )

Dim Y As Single

Y=0

For i = 1 To 10

Y = (2^i + Y)^(1/2)

Next

MsgBox Y

End Sub

^ : a b VBA
a^b , a ( a 1/2) a^ (1/ 2) .
52

9. Y, :
10
n x2
Y (1) .
n0 2(n 1)!
. , , ,
11 . Y , Yi ,
: Y Y Yi . ,
Yi Yi-1 .
x ,
, , .
i x2
, i 0 10, Yi (1) . ,
2(i 1)!
x2
Yi 1 (1) i 1 . Yi Yi-1 , , ,
2i!
Y 1
, (i + 1)! = i !(i +1). : i , ,
Yi 1 i 1
1
Yi Yi 1 . Yi +1 Y10 ,
i 1
Yi - Y0 . ,
x2
Yi i : Y0 . , :
2
Sub MyY ( )
Dim Y As Single, yi As Single, x As Single
x = InputBox( )
yi = (x^2)/2 Y0
Y = yi
For i = 1 To 10
yi = (-1/(i+1))*yi Yi -
Y = Y + yi
Next

MsgBox Y

End Sub

, ( !)
: Y = Y +(-1/(i+1))*yi ,
Y yi, Y0, yi
. , i-
i-1-, ,
(i-1)- , .

10. b 20- ,
k , b < 2k.
. 20- ,
: ; , ()
53

; .
? b .
20, .. b < 20? , , k = 0. , 21, ..
b < 21? , , k = 1. , 22, b < 22?
, k. ,
( ) ,
k .
b 2k. :

Sub MinK ( )

Dim b As Integer, k As Integer

For i = 1 To 20 b

b = InputBox(" ")

k = 0 b k !
While b >= 2 ^ k ;

k = k + 1
Wend

MsgBox " b =" & CStr(b) & " k=" & CStr(k)

Next

End Sub

&
CStr , ..
.

1. : y 99 96 9 ... 6 3
1
2. >0: .
n 1 n(n 1)(n 2)
5 n x n 1
k
3. k : .
n 1 (2 n)!
4. a 20- ,
k , a > 3k .

5.
()

11. .

, VBA, Excel. For
54

, ( ) . ,
, 10 .

:
For i = 1 To 10

(i) = InputBox(" ")

Next

Excel ( ,
, A1:A10):
For i = 1 To 10
(i)=Cells(i,1) , ()
Next

VBA
Excel, , B1:B10:
For i = 1 To 10
Cells(i,2)= (i) , ()
Next

MsgBox:
For i = 1 To 10

MsgBox (i)

Next

12. 10 .
. 7 ,
(),
: A(i)
.
A(i), A(1)
, :
Sub maxmas()

Dim i, max, A(10) As Integer

For i = 1 To 10

A(i) = InputBox(" ")

Next

max = A(1)

For i = 2 To 10

If A(i) > max Then

max = A(i)

End If

Next

MsgBox (max)

End Sub

55

,
. ,
, :

Sub maxm()

Dim i, max, A(10) As Integer

max = InputBox(" ")


For i = 2 To 10

A(i) = InputBox (" ")


If A(i) > max Then

max = A(i)

End If

Next

MsgBox (max)

End Sub

13.
10 .
. : 1) , 2)
, 3)
, 4) .
5) .
Excel (, 1:10)
(, 1:10).
,
A. A(k)>0, (
k) . ,
, ,
.
.
A(j). j .
A(k) A(j),
. :
Sub obmen()

Dim i, j, k, rab, A(10) As Integer

For i = 1 To 10 Excel:
A(i) = Cells(i, 1)

Next

For i = 1 To 10 :
If A(i) > 0 Then

k = i

Exit For

End If
Next
For i = 1 To 10 :
56

If A(i) < 0 Then

j = i

Exit For

End If

Next

rab = A(k)

A(k) = A(j)

A(j) = rab

For i = 1 To 10 Excel:

Cells(i, 2) = A(i)

Next

End Sub

14. .
.
. ,
p ,
, , ,
.
. .
().
() .
:
1. , 1-. (
). 1- .
2. , 2-. (
). 2- .
3. . . .
:

Sub _()
Dim c(1 To 100) As Single
Dim k As Integer, i As Integer, j As Integer
Dim vr As Single
k = InputBox('' <= 100'')
If k > 100 Then Exit Sub
Cells(1, 1) = k
For i = 1 To k
c(i) = InputBox('''' & i & '' '')

Cells(i, 2) = c(i) '

Next
For i=1 To k1 ' ,
For j=i+1 To k 'p p i- p
57
, i+1-
If c(j)<c(i) Then ' pp
, i-, :
vr = c(i)
c(i) = c(j)
c(j) = vr
End If
Next
Next
For i = 1 To k
Cells(i, 3) = c(i) ' Excel
Next
End Sub

pp yp.

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

2. [i-1]<[i],

For i = 2 To k
For j = k To i Step -1
If c(j-1)<c(j) Then ' , yp

vr = c(j - 1)

c(j - 1) = c(j)

c(j) = vr

End If

Next

Next

15. A(10).
.
. ,
:

Sub ()

Dim A(10) As Integer, k As Integer, k0 As Integer

For i = 1 To 10

58

A(i) = InputBox(" " & i & "- ")


Next
k = 1

For i = 2 To 10

k0 = 1

For j = 1 To i - 1

If A(j) = A(i) Then

k0 = 0

Exit For

End If

Next

k = k + k0

Next

MsgBox " " & k

End Sub

:
1. N N .

.
2. (10),
(2;14).
3. (15).
4. .
, , .
, , , 0.

6. ,
,

: v = f(x,y,z), x [a,b], x =h1; y


[c,d], y = h2; z [e,k], z = h3. , ,
, ,
, - .
, .. x, y,
z. ? ,
For, :
For x = a To b Step h1

For y = c To d Step h2

For z = e To k Step h3

v = f (x,y,z)

Next

Next

Next

59

, ( y
z) x, y, z.
, , y [c,d), y
While y < d:
For x = a To b Step h1

y = c

While y < d

For z = e To k Step h3

v = f (x,y,z)

Next

y = y + h2

Wend

Next

, , y =
2x + 4, ,
1. ( y)
:
For x = a To b Step h1

y = 2*x + 4

For z = e To k Step h3

v = f (x,y,z)

Next

Next

, x y.
: y ,
x , y
, x.

16. v, :

xy z 2
w x 3
v ; x [2;4], x 1;


x 2 xw - z x 3

0,5 y 3

y [2;6], y ; z =1,2; 1,5; 3; wi 1 0,5wi , w1 2; 1 i 4


1 y 3
. . -
v=f(x,y,z,w) ,
. , ,
- . y
, For
, While. w 4
, , ,
For 2 4.
z , - ,
60

z(j)
For j. v y, ,
,
If. :

Sub KrutoyV( )

Dim x As Single, y As Single, z(3) As Single, w As

Single, v As Single

z(1) = 1.2

z(2) = 1.5

z(3) = 3

For x = 2 To 4 x
For j = 1 To 3 z
y = 2

While y <=6 y
w =2

For i =2 To 4 w
If x <3 Then

v = x*y + z(j)^2 +w

Else

v = x^2 + x*w z(j)


End If

MsgBox v - v
w = w*0.5 w
Next for i w
If y < 3 Then y
y = y + 0.5

Else

y = y + 1

End If
Wend While y y
Next For j z(j)
Next For x
End Sub

:
1. v :
x 3 y 2 (2 x y) z 2 x 4

v 2 1 ; x [2;4], x 1;

x xw x 4

2w
2

0,5 y 3
y [2;6], y ; z = 1,2; 1,5; 2,3; 3;
0,75 y 3
61

w i 1 0,3w i , w1 2,4; 1 i 4

2. v, 5:
( y y )z y 5

v 3w 5 y ; z = 1,1; 1,3; 1,5; 2,1;
y 5
z

w i 1 1,7w i , w 1 0,2; 1 i 4 ;

29, j 3

y ; 1 j 5 .
7, j 3

7.


: , .
,
, ..
. , , 57.

17. ().

For i = 1 To 5

For j = 1 To 7

(i,j) = InputBox(" ")

Next

Next

Excel (
, , A1:G5):
For i = 1 To 5
For j = 1 To 7 A G
(i,j) = Cells(i,j)
Next

Next

. i
( 1),
j, .. i- .
(i = i +1)
j, .
.

i j , ..:
62

For j = 1 To 7

For i = 1 To 5

A(i, j) = Cells(i, j)

Next

Next

,
. .
Excel, , A8:G12:
For i = 8 To 12 ,
For j = 1 To 7 A G
(i,j) = Cells(i,j)
Next

Next

18. A(NM)
B .
. : A;
: ,
B; B .
.
, . A(i,j)
A , ,
For, (i) (j) . A(i,j)
, B .
( ) ,
B. A
, B. ,
NM A.
, B i j.
:
, .
. k.
A(i,j) B(k) k ,
k B.
A N=4, M=6.
:
Sub ()

Dim A(4, 6) As Integer, B(24) As Integer, k As Integer

For i = 1 To 4

For j = 1 To 6

A(i,j)=InputBox(" ")

Cells(i,j)= A(i,j) Excel



Next

Next

63

k = 0

For i = 1 To 4

For j = 1 To 6

If A(i, j) < 0 Then

k = k + 1

B(k) = A(i, j)

End If

Next

Next

For i = 1 To k

Cells(7, i) = B(i) Excel 7


Next

End Sub

,
, ,
:
Sub ()

Dim A(4, 6) As Integer, B(24) As Integer, k As Integer

k = 0

For i = 1 To 4

For j = 1 To 6

A(i,j)=InputBox(" ")

Cells(i, j) = A(i, j)

If A(i, j) < 0 Then

k = k + 1

B(k) = A(i, j)

Cells(7, k) = B(k)

End If

Next

Next

End Sub

19. (5 5).
. ,
, .
(, k)
.
Mod. :

Sub matrixNumber()

Dim i, j, k, A(5, 5) As Integer

For i = 1 To 5 Excel:

For j = 1 To 5

64

A(i, j) = Cells(i, j)

Next j
Next
k = 0
For i = 1 To 5 :
For j = 1 To 5
If A(i,j) Mod 2 = 0 Then

k = k + 1
End If
Next
Next
MsgBox (k)
End Sub

20. ,
(7 7).
. ,
: (2,4), , , (3,4) .
, Mod
i j.
2. :

Sub matrixIndex()

Dim i, j, s, A(7, 7) As Integer

For i = 1 To 7

For j = 1 To 7

A(i, j) = Cells(i, j)

Next

Next

s = 0

For i = 2 To 7 Step 2
,
2
For j = 2 To 7 Step 2
,
2
s = s + A(i, j)
A(i, j)

Next
Next
MsgBox (s)
End Sub
65

21. (5 5).
. , ,
j ( ) i (
). ,
.
, ,
, ..
(i,i). (n-i+1),
n 1.
, . :
Sub topDiagonal()

Dim i, j, A(5, 5) As Integer

For i = 1 To 5 Excel:
For j = 1 To 5

A(i, j) = Cells(i, j)

Next
Next
For i = 1 To 5
A(i, i) = 0
Next
For i = 1 To 5 Excel:
For j = 1 To 5
Cells(i, j) = A(i, j)

Next

Next

End Sub

,
For .

22. 5 5.
, .
.
.
, .. j = i .
, j i, i 1.

Sub ()

Dim M(5, 5) As Integer, S As Integer, k As Integer, Avg

As Single

For i = 1 To 5

For j = 1 To 5
M(i,j) = InputBox(" (" & i & "," & j & ")")
Next

Next

66

S = 0
k = 0
For i = 2 To 5 2
For j = 1 To i 1 1

S = S + M(i, j)

k = k + 1

Next

Next

Avg = S / k
MsgBox " = " & Avg
End Sub

23. (5 5),
, .
. 4 ,
. ,
, , ..
: 1- 1 5, 2- 2 4, 3- - 3 3,
.., , i i 5-i+1. :

Sub matrixKvota()

Dim i, j, A(5, 5) As Integer

For i = 1 To 5

For j = 1 To 5

A(i, j) = Cells(i, j)

Next
Next
For i = 1 To 3 1
For j = i To 5 - i + 1

A(i, j) = 0

Next

Next

For i = 1 To 5

For j = 1 To 5
Cells(i, j) = A(i, j)
Next

Next

End Sub

:
1. (5 5).
2. , , (5 5) .
3. (5 5),
, .
67

4.
(55), .

8.

24. ,
, .

. Asc(), ASCII-
. [49,57], .

Sub _()
Dim s As String
Dim i As Byte, nd As Byte
s = InputBox('' '')
nd = 0

For i = 1 To Len(s)

If Asc(Mid(s, i, 1)) > 57 r Asc(Mid(s, i, 1)) < 49 Then

nd = nd + 1

End If

Next

MsgBox (nd)

End Sub

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

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

. .
,
, k .
,
,
68

,
k .
:
1. len() , ,
Len()=4.
2. LCase() ,
, b=Lcase(). b = .
3. Space(_)
_.
4. Mid(string, start[, length]) ,
, ,
Mid(,4,4)= .

5. InStr([start, ] string1, string2[, compare])


, , k
= InStr(1, XXpXXpXXPXXP, "W") 0, .. "W"
XXpXXpXXPXXP.

Private Sub ()

Const As String = ''''

Const As String = "

"

Dim STR As String, STRS As String


Dim n As Long
STR = " "
STR = LCase(STR) '
n = Len(STR) '
STRS = Space(n) ' n
For i = 1 To n
tmp = Mid(STR, i, 1) '
STR
k = InStr(1, , tmp) '
If k = 0 Then
Mid(STRS, i, 1) = tmp

Else

Mid(STRS, i, 1) = Mid(, k, 1)

End If

Next i

Msgbox STRS

End Sub

26. .
( -
) :
69

.
. , -,
.
,
-.

Private Sub ()

Dim tmp As String, Alf1 As String, Alf2 As String

Dim a As String, b As String

a = InputBox('' '')

Alf1 = ''''

Alf2 = ''''

n = Len(a)

b = Space(n)

For i = 1 To n

tmp = Mid(a, i, 1)

k = InStr(1, Alf1, tmp)

If k = 0 Then

Mid(b, i, 1) = tmp

Else

Mid(b, i, 1) = Mid(Alf2, k, 1)

End If

Next i

MsgBox b

End Sub

:
1.
.
2. ,
. .
3. ,
.
4. -,
.

9.

,
, .
70

, VBA
, Excel.
,
.

.
, , ,
. VBA,
, .
VBA:
1. Excel.
2.
// .
(. 3).

. 3. .

3. ( 1, 2 . .)
OK.
.
, .
4.
. , , ,
,
c/c/, .
c/c/,
. VA
,
.

27. , 1, 1, 1
, .
.
1. // .
2. ClearCell OK.
71

3. 1:1
Delete .
4. , .
5. : /
/. ClearCell
.
VBA :

Sub ClearCell()

ClearCellData

23.05.00 ()

Range("A1:C1").Select

Selection.ClearContents

End Sub

6. (
). :
Range("A1:C1").Select
Selection.ClearContents

7. , ,

Rng(":").Select

8. , .
: Excel, 1:3
, //,
ClearCell .

,
. Excel
:
1. / / Excel.
2. ,
. .
3. Excel,
.
4. .


. ,
4 .
72

:
1. , A1:C4:
, , , .
2. , A1:A12 .
.
3. , ,
D5:J12 .
4. , ,
A1:B11 .

10.
Excel

(. 4),
/ / ,
Excel , Com
mandButton1, CommandButton2, CommandButton3, OptionButton1,
OptionButton2, OptionButton3, TextBox1, Label1.

. 4. Excel

28. :
(
(-5, 5)
A1:A20 Excel),
(
A1:A20 ),
Ok (
,
, , Option-
Button ) (. . 5).

. ,
1 ,
/ / .
, , CommandButton1,
( ) .
73

. 5. Excel c .

Caption
.

VBA, 1.
:
Private Sub CommandButton1_Click()
For i = 1 To 20
Cells(i, 1) = -5 + 10 * Rnd()
Next
End Sub
Private Sub CommandButton2_Click()
Range("A1:A20").Select
Selection.ClearContents
End Sub
Private Sub CommandButton3_Click()
Dim a(1 To 20) As Single, Max As Single
Dim Min As Single, S As Single
For i = 1 To 20
a(i) = Cells(i, 1)
Next

If OptionButton1.Value = True Then

Max = a(1)

For i = 2 To 20

If Max < a(i) Then Max = a(i)

Next

TextBox1.Text = Max

End If

If OptionButton2.Value = True Then

Min = a(1)

74

For i = 2 To 20

If Min > a(i) Then Min = a(i)

Next

TextBox1.Text = Min

End If

If OptionButton3.Value = True Then

s = 0

For i = 1 To 20

s = s + a(i)

Next

TextBox1.Text = s / 20

End If

End Sub

11.

(UserForm)
. , ,
,
.

.
UserForm1, UserForm2, UserForm3 . .
Caption .
//
Visial Basic, Insert/UserForm.
UserForm1 (. 6).

6. UserForm1 .

75

, .
, (Properties)
. ,
.
,
.
. ,
,
.
Hide.
- , ,
:

Sub CommandButton1_Click ()

UserForm1.Hide

End Sub

Excel

Sub _()

UserForm1.Show

End Sub

//,
_,
(, ).
.
Excel, VBA:
, ,
;
Run/ Run Sub /UserForm.

Excel, .


, ControlTip-
Text .
.
ControlTipText

(, . .),
.
76

29. ,
A B B
, B, C z sin . ,
C 2 A A
,
.
.
1. UserForm2 (. 7):

. 7. UserForm2.


(. 8):

. 8. UserForm2
.

2. Ok, ,
:

Private Sub CommandButton1_Click()

A = CInt(TextBox1.Value) a,b,c
,

B = CInt(TextBox2.Value)

C = CInt(TextBox3.Value)

If C ^ 2 - A < 0 Or A = 0 Then

TextBox4.Text = " "

Exit Sub

77

End If

z = (A + B) / (C ^ 2 - A) ^ (1 / 2) + Sin(B / A)
TextBox4.Value = Format(z, "###.00")

End Sub

Private Sub CommandButton2_Click()

UserForm2.Hide

End Sub

, , ,
, , (Com
mandButton1, CommandButton2) .

3. Run/ Run Sub /UserForm.


A, B,C .

30. ,
,
.
:
(. 9).

2 5 (.. ).
,
. ,
.
.
1. UserForm3
(. 10):

. 9. .10. UserForm3.

2. ,
UserForm_Initialize.
,
78

( ).

Private Sub UserForm_Initialize()

For i = 1 To 6

Cells(i+1,2) = Format(2+(5-2)*Rnd(), "###.##")

Next

End Sub

,
(2; 5).

3. CommandButton1 ().

Private Sub CommandButton1_Click()

Max = Cells(2, 2): k = 2

For i = 2 To 6 (B)

If Max < Cells(i, 2) Then


Max = Cells(i, 2)

k = i

End If
Next
TextBox1.Text = Cells(k, 1)
Min = Cells(2, 2): k = 2
For i = 2 To 6
If Min > Cells(i, 2) Then

Min = Cells(i, 2)

k = i

End If
Next
TextBox2.Text = Cells(k, 1)
s = 0
For i = 1 To 6
s = s + Cells(i + 1, 2)
Next

sred = s / 6
TextBox3.Text = Format(sred, "##.##")

End Sub

4. Run/ Run Sub /UserForm.


:
,
.
79

31. ,
, .
.
.
:
,
.
.
1. (. 11):

. 11. .

2. ,
,
, ,

(. 11). , 3,
50% , 2,
30% 50%; 1
10% 30% , , 0 .

. 11. .

80

3.
:

Private Sub UserForm_Initialize()

For i = 1 To 20

Cells(i + 1, 2) = Format(100+900*Rnd(), "###.##")

Next

End Sub

4. Ok ,

. ,
. ,


OptionButton. KolPost,
statys, prozent, .

Private Sub CommandButton1_Click()

= TextBox1.Text

s = 0: k = 0: KolPost = 0

For i = 1 To 20

If Cells(i + 1, 3) = Then

s = s + Cells(i + 1, 2)

k = k + 1

End If

KolPost = KolPost + Cells(i + 1, 2)

Next

If k=0 Then

MsgBox " "

Exit Sub

End If

TextBox2.Text = s

statys = 0

If OptionButton1.Value = True Then

If s >= 0.5 * KolPost Then statys = 3

If s<0.5*KolPost And s>=0.3*KolPost Then

statys=2

End If

If s<0.3*KolPost And s>=0.1*KolPost Then

statys=1

End If

TextBox3.Text = statys

End If

81

If OptionButton2.Value = True Then

prozent = s / KolPost

TextBox3.Text = Format(prozent, "0.#0")

End If

End Sub

5. B1:B21
TextBox1, TextBox2, TextBox3.

Private Sub CommandButton2_Click()

TextBox1.Text = ""

TextBox2.Text = ""

TextBox3.Text = ""

Range("B2:B21").Select

Selection.ClearContents

End Sub

1. (. 31)
.
20% , , 30%
, 40% .

2. ,
.
.
. ,
: ,
.

4.
Visual Basic For Application


VBA .
: [] , |
, , { k1 | k2 }
k1, k2.

4.1.

VBA .
.
, .
82
, . ,
VBA .
. VBA .1.
1




()
Byte 1 1 255
True () False
Boolean 2
()
Integer 2 32768 32768
2147483648
Long 4
2147483648

Single 4
1,4-45 3,4+38
1 100 . 31
Date 8
9999.


Double 8
4,9-324 1,7+308

10+
String () 0 2109


String 0 65400


Variant 16
Double
22+
Variant 0 2109

VBA
:
Dim _ As _

, :
Dim A As Integer

Dim C,D As Integer, E As Single


: , , ..
, . VBA
:
Const _ =

Const _ As _ =

:
Const FileName = ''test.xls''
83

Const PI As Double = 3.14159

, ..
.
-
, :
Dim B(3, 3) As Single

Dim A(12) As Integer

33 ( ),
.
(), 12 ,
(0), (11). ,
0 . ,
Option Base 1.
.
To ,

Dim B(1 To 3,1 To 3) As Single

Dim A(1 To 12) As Integer


rr, , ,
, Variant.
Dim A As Variant

= rr (10,20,30)

,
.
(').
.
4.2.
, ,
. Debug, Compile VBAProject.
, VBA
, , ,
, ..
Visual Basic
<Enter>. ,
, ,
, .

. , VBA
.
84

,
Microsoft Visual Basic ,
.

.
, , , ,
.
Microsoft Visual Basic Debug,
, ,
. Visual Basic
. ,
.

Standart.


.
Start
, Continue.
.
Break
.
.
End
.

(
, ).
, ,
, .

, ,
:
, (Run, Step Into)
<F8>

4.3. VBA

,
:

[1] + [2]
[1] [2]
[]
[1] * [2]
[1] / [2]
85

[1] Mod [2]


[1] ^ [2]
,

, x>y:

[1] < [2]
[1] > [2]
[1] <= [2]
[1] >= [2]
[1] <> [2]
[1] = [2]

, ()
, Not x and y.
[1] And [2]
[1] Or [2]
Not []
[1] Imp [2]

VBA .
( ):
1.
2. ^
3. ( )
4. * , /
5.Mod

6. + ,
7. >, <, >=, <=, <>, =
8.Not

9.And

10.Or

11. Imp

4.4. VBA
:

Abs()
Atn()
Cos()
Exp()
log()
Sin()
Sqr()
86

Tan()
Sgn()
Fix() ,
Int() ;


Asc ASCII- .
: Asc()
Chr ASCII- .
: Chr()
: Chr(13) , Chr(97)= ''a''
Left ,
.
: Left(string, length)
Length ,
String .
Right ,
.
: Right(string, length)
Length ,
String .
Mid ,
..
: Mid(string, start[, length])
Length ,
String ,
Start string,
.
Len .
: len()
String ,
.
: String(number, character)
StrComp .
: StrComp(string1,string2 [, compare])
String1, string2
Compare .( 0
1- )
InStr
.
: InStr([start, ] string1, string2[, com
pare])
Stsrt- , ,
87

. ,

String1 ,
String2
Compare .


IsArray()
IsDate()
IsEmpty() Dim
IsNull()
IsNumeric()
IsObject()



:
Val () , ,

Str () Variant (String),

,

CBool () Boolean
CByte () Byte
CCur () Currency
CDate () Date
CDouble () Double
CDec () Decimal
CInt () Integer
CLng () Long
CSng () Single
CVar () Variant
CStr () String

4.5. VBA

.
, (..
).
. ,
88

a = b: c = d: If a > d Then d = a

, (
)+( ) .

.
, .

:
=

Const =

If...Then...Else..End If (
).

.
:
If Then

_1

[Else

_2]

End If]


:
If <-1> Then

ElseIf <-2> Then

. . .

[ElseIf <-n> Then

[ n]

. . .

[Else

[]

End If

Select Case (
).
, ,
.

:
Select Case

Case -1

_1

. . .

89

Case -n

_n

[Case Else

_n+1]

End Select

-i
( ).

,
Select Case. ,
Case-.
:
=Cells(1,2)

Select Case

Case 1

Cells(2,2)= '' 1''

Case 2, 3

Cells(2,2)= '' 2 3''

Case 4 To 6

Cells(2,2)= '' 4 6''

Case Is >=7

Cells(2,2)= '' 7''

End Select

.
For...ToNext
For-Next ,
. ,
1.
Exit For.
:

For = To [Step ]

[Exit For]

Next []

ForEachNext

.
.
.
90

For Each In

[Exit For]

Next []

:
For Each In WorkSheets

If .Name= '''' Then

Sheets('''').Delete

End If

Next

While-Wend
While-Wend ,
True.
:
While

Wend

While-Wend For-Next
, .

Do Loop
DoLoop ,
True ( While)
True ( Until).
:
Do {While | Until}

[Exit Do]

Loop

Do

[Exit Do]

Loop {While | Until}

91

DoLoop
Exit Do,
DoLoop.
GoTo.
:
Goto

,
.
,
, . GoTo,
,
:
: :

4.6. ,

VBA ,
.
,
, ..
,
, . Sub
Function, .

,
-,
.
:
[Private | Public] [Static] Sub ([] )

[Exit Sub]

End Sub

:
Public , Sub

Private , sub
,
Static , sub



92

-( ),
,
sub .

() -
, VBA.

Run
F5 . ()
, ..
.
Exit Sub
Sub.

,
.
:
[Private|Public][Static]Function

([])As

[Exit Function]

End Function

Function , Sub.
Exit Function
Function. Sub, Function
, (
),
.
. ,
.
Sub, ()
VBA.

: Sub
. Sub:
:

,
. ,
.

, ,
:
93

:
.

sub
Call.

:
Call ()

,
.

4.7.


.
(MsgBox)
, (InputBox) .
,
InputBox
, Cancel.

. ,

, .

Cancel, .

InutB(rmpt[, Title] [, Default]

[, xPos] [, yPos] [,Helpfile, Context])

Prompt ,
. Prompt
.
(Chr(13)),
(Chr(10)) (Chr(13) & Chr(10));
Title ,
. ,
;
Default ,
,
. ,
;
xPos ,
.
,
;
yPos ,

94

. ,
;
Helpfile ,
,
. ,
Context;
Context ,
. ,
Helpfile.
, , -
MsgBox
,

Integer, ,

MsgBox(Prompt[, Buttons] [, Title] [, Helpfile,

Context])

Prompt ,
;
Buttons ,
, ,
, .
0. , ,
, 2;
Title ,
. ,
;
Helpfile ,
, .
,
Context;

Buttons MsgBox,
:


vbOKOnly 0
vbOKCancel 1
vbAbortRetryIgnore 2
vbYesNoCancel 3
vbYesNo 4
vbRetryCancel 5
95
,
.
Buttons MsgBox,
:


vbCritical 16
vbQuestion 32
vbExclamation 48
vbInformation 64
,
,
VBA:

vbOK 1
vbCancel 2 (l)
vbAbort 3 (Abort)
vbRetry 4 (Retry)
vbIgnore 5 (Igr)
vbYes 6 (Yes)
vbNo 7 (No)

4.8. , UserForm


Name
.
Caption ,
.
BorderStyle .
. :
fmorderStyleNone 0 ( );
I
fmorderStyleSingle 1 ( ,
).
BorderColor, .
BackColor
.

, VBA.
Left,
.
Height, Width .
96


.
InsideHeight,
InsideWidth .

Show .
Hide .
Move .
PrintForm .

Initialize ,
.
Load ,
.
Unload , Load.
, ,
.
Click, .
DblClick .

,
:
,
extBox
Label
mmduttn
ListBox
mb
ScrollBar
Sinuttn
tinuttn
CheckBox
ToggleButton
Frame


Name

Оценить