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

()

VISUAL BASIC
FOR APPLICATIONS EXCEL


2012
519.682


-

11 7 2012


10 17 2012 .

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


. . , .. ()

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

Visual Basic for Applications Excel: /


. . , . . : , 2012. 153.


,
. ,
.

.

()
, 2012

............................................................................................................ 4
1. VBA............................................................................. 4
1.1. VBA ................................................................... 4
1.2. .......................................................................... 8
2. , , ...................................... 17
3. VBA .............................................................. 27
3.1. ..................................................................... 27
3.2. ....................................................... 29
3.3. ......................................................................... 29
3.4. ................................................................................ 40
4. .......................................................................................................... 47
5. ....................................................................................................... 52
5.1. ......................................................................... 52
5.2. ........................................................................... 53
5.3. ...................................................................... 61
6. ................................................................................ 65
6.1. ....................................... 65
6.2. .......................................... 68
6.3. ................ 77
7. VBA Excel ................................................................ 86
7.1. Application .............................................................................. 86
7.2. Workbook Workbooks ....................................... 87
7.3. Worksheet Worksheets ..................................... 88
7.4. Range Selection ................................................................. 90
7.5. ActiveCell ............................................................................... 93
8. Range, Excel ........................ 100
8.1. GoalSeek................................................................................. 100
8.2. AutoFill................................................................................... 105
8.3. Sort ......................................................................................... 107
8.4. AutoFilter ............................................................................... 109
9. .................................................................................... 118
10. VBA
Excel ............................................................................................................... 126
10.1.
................................................................................................................... 127
10.2. , ,
................................................................. 138
..................................................................................................... 153

3

Visual Basic for Applications -
,
, .
VBA

Microsoft Office, , Microsoft Office Excel
, , ,
.. VBA ,
Microsoft
Office
.
VBA , ..

.
1. VBA
1.1. VBA
VBA Visual Basic Editor.
,
(. 1.1) Visual Basic:
Visual Basic (. 1.3).
Microsoft Office Excel 2010
, :
1. .
2. Excel
,
- .
3. (. 1.2)
.


VBA
<Alt>+<F11>. VBA
View Microsoft Excel .

4
. 1.1.

. 1.2. Excel

VBA :
, , , ,
.
VBA
View, Project Explorer Project Explorer .
(VBAProject)

5
. VBA
.
.

.
Properties Window ,
View, Properties Window.
: .
,
, .
: Alphabetic ( ) Categorized ( ),
.
.
, (,
, ),
(module). ,
, .
Insert, Module.
, ,
(. 1.3).

. 1.3.

,
. ,
TextBox1.T

6
T (. 1.4),
.
<Tab>
.

. 1.4.
VBA
.
.
Insert, UserForm Insert
UserForm .
(Toolbox) (. 1.5).

.

. 1.5.
7
1.2.
VBA .
.
, , , .
Controls
Toolbox (. 1.5).
, ,
.
, .

,
.
:
ActiveX (. 1.6).
, ,
Excel.
ActiveX (ActiveX Controls)
, , Excel.


. ,
-,

.
. 1.6.
,
, ,
.
,
,
. , ,
, , ..
,
..
(CommandButton)
ActiveX, , Click,
.
,
1, 2.
8
.1.8.
. 1.7.
:
1. Microsoft Office Excel 2010
1.
2.
.
3. (CommandButton)
ActiveX .


() (. 1.8).
4.

(Properties).
Caption
2.
5. ,
.
2. :
(

): VBA
,

(Click).
: Sheets("2").Select, :
Private Sub CommandButton1_Click()
Sheets("2").Select
End Sub
6.
.

9

.
7. (. 1.7).
2.


Label - .
,
.
:
Caption ,
Visible : True (
) False (
)
Multiline : True (
) False
( )
WordWrap : True (
) False (
)
AutoSize : True (
,
) False
( )
Alignment : Left (), Right (),
Center ( )
Font , ,
ToolTipText ,

ForeColor
BackColor

- TextBox - ,
,
.

10
:
Text ,
ScrollBars
. :
fmScrollBarsNone

fmScrollBarsHorizontal

fmScrollBarsVertical

fmScrollBarsBoth -


"".

- Frame - ,
.
Caption.
, ,
.
Command Button -
,
,
, ..
:
Caption ,
Cancel : True (
, ..
<Esc> ,
) False ( )
Visible : True (
) False (
)
Enabled : True (
) False ( )
Picture . ,
CommandButton1.Picture = _

11
LoadPicture("c:\my_doc\.bmp")
LoadPicture()
.

- OptionButton -

. ,
.
:
Value True, False

Enabled : True (
) False ( )
Visible : True (
) False (
)
Capture ,

CheckBox -
. :
) .
. Value, Enabled,
Visible, Capture, .
TripleState,
. TripleState
: False ( ), True (
True, False Null).
Image -
bmp, cur, gif, ico, jpg, wmf.
:
AutoSize ,

,
Picture .
LoadPicture
PictureSizeMode .
12
:
fmPictureSizeModeClip (

);
fmPictureSizeModeStretch (
,
),;
fmPictureSizeModeZoom (

,
)
PictureAlignment
. :
fmPictureAlignmentTopLeft (
);
fmPictureAlignmentTopRight (
);
fmPictureAlignmentTopCenter ( );
fmPictureAlignmentBottomLeft (
);
fmPictureAlignmentBottomRight (
)

ComboBox.
, TextBox, , ,
, .
:
Text - ,
.
List - ( ).
ListIndex - (
, ,
-1).

ListBox -
.

13
,
.
:
ListIndex ,

ListCount
TopIndex
ColumnCount
Text Column ,
text
Text
List ,
, :
List (row, column)
RowSource ,

Multiselect .
:
fmMultiSelectSingle
;
fmMultiSelectMulti
, ,
;
fmMultiSelectExtended
shift

Selected Boolean ,
.
,
.
Selected True,
.
: True ( )
False ( ),
,
Multiselect fmMultiSelectMulti
fmMultiSelectExtended
ColumnWidths ColumnWidths = string, string - ,
14
.

:
With ListBox1
.ColumnCount = 3
.ColumnWidths = 20;30;30
End With
ColumnHeads : true (
) false (
)
ListStyle :
fmListStylePlain
;
fmListStyleOption
,

:
, With ListBox1
. AddItem ListBox
. AddItem ComboBox
. AddItem OptionButton
.ListIndex=0
End With
, With ListBox1
.List Array(1,2,3,4,5,6,7,8)
.ListIndex=1
End With
1:4, With ListBox1
.ColumnCount = 2
. .RowSource = (A1:B4)
End With
, With ListBox1
, , .ColumnCount = 2
.List(0,0)=
.List(0,1)=
.List(1,0)=
.List(1,1)=
End With
15
, With ListBox1
, , .ColumnCount = 2
.List =
End With

ListBox:
Clear ;
RemoveItem (index) ,
index ;
AddItem ([item [,varIndex]]) , item
( ), , varIndex
.
UserForm
UserForm
.
,

.
UserForms ,
UserForm .
, UserForms : Count (
), Item (
), Add ( ).
UserForm:
Name
Caption ,

BackColor
BorderStyle
Picture ,
UserForm:
Show
Hide
Move
PrintForm
16
UserForm:
Activate ,

Initialize ,

Load Show
Resize
Terminate ,

2. , ,
,
. VBA
:


()
Byte () 1 1 255
Boolean () 2 True False
Integer ( ) 2 -32 768 32 767
Long ( 4 -2 147 483 648 2 147 483 647
)
Single ( 4 -3,40282338 -1,401298-45
;
) 1,401298-45 3,40282338

Double ( 8 -1,7976931346232308 -
4,9406565841247-324
) ;
4,9406565841247-324
1,7976931346232308

Currency () 8 -992 337 203 685 477,5808
992 337 203 685 477,5807
Object () 4
String ( 10+ 0 2
)
Variant ()
17

: /,
, ,
, .
Variant
Null, ,
,
.
,
Variant.

, VBA
, .
-
: ,

Set.
:
Dim <> As Object
Set <> = <>
,
Set
Nothing, ,
, :
Set txt = Nothing
,
, ,
.

:
Dim [WithEvents+ *(*+)+ *As [New+ + _
[,[WithEvents+ *(*+)+ *As [New+ ++ ...
:
WithEvents , ,

,
18
, ActiveX (..

)

;
60 .

:
* + *,* + + ...
,
Option Base.
Option Base,
.
New ,
.
New ,

.

, Dim
, .
, ,
.
. VBA
, , , :
1. 255 .
2. ,
: %, !, &, #, @, $.
3. , ,
, .
4. ,
.
5. ,
VBA .
,
( ), ,
(General) .

19
.
. :
[Public |Private] Const [As ] =
:
Public ,
,
. .
Private ,
,
, .
.

.
, ,

, Is.
VBA
:
.
.
.
.
.
.
.
[1] + [2]
[1] - [2]
-[]
[1] * [2]
[1] / [2]
[1] \ [2]
[1] Mod [2]
[1] ^ [2]

20

[1] <[2]
[1] > [2]
[1] <= [2]
[1] >= [2]
[1] <> [2]
[1] = [2]
[1] Is [2] ,

[1] Like [2]


[1] And [2]
[1] Or [2]
[1] Xor [2] Or ()
[1] Not [2]
VBA .

1
2 ^
3 - ( )
4 *, /
5 \
6 Mod
7 +, -
8 >, <, >=, <=, <>, =
9 And
10 Or
11 Xor
12 Not


Abs() ( )
Atn()

21
Cos()
Exp() , ..

Log()
Rnd() [0,1]
Sgn()
Sin()
Sqr()
Tan()
Fix()
Int() . :

Int
, , Fix -
,


,
.
IsArray()
IsDate()
IsEmpty()
Dim
IsError()
IsNull()
(Null)
IsNumeric()

IsObject()




Str(N) , String
N.

22
.

(, )

CStr
Val(S) , Variant
,
S,

, VBA
.
VBA S
, Val 0
Asc(S) , Integer

S. , 65
Chr(N) ,
N,
0
255. 66
B
, ,
,
Format, Variant (String),
, ,
.
:
Format ( *,+)
-
-
.
Fixed

.
:
0 .
;

23
# - .
;
. ;
% - ;
: - ,
Time;
/ - ,
Date;
E+, E-, e+, e- -
.
Val Str VBA
:
,
CBool() Boolean
CByte() Byte
CCur() Currency
CDate() Date
CDbl() Double
CDec() Decimal
CInt() Integer
CLng() Long
CSng() Single
CVar() Variant
CStr() String

VBA
: . VBA.
(MsgBox)
, (InputBox) .

MsgBox() ,
,
. :
MsgBox ( *,+ *,+)

24
, ,
.
.
, ..
, .
:
, , ,
.


:
= 1 + 2.
1
. 2.1
:
2.1.
1
0 ""
1 "" ""
2 "",
"", ""
3 "", "", ""
4 "", ""
5 "",""
2.2.
2

16

32
()

48
()

64

25
, VBA ,
"".
2 ,
( 2.2).
.
, "Microsoft
Excel".

InputBox() ,
, ,
OK Cancel.
, ,
OK, String, ,
. Cancel
.
:
InputBox(*, + *, +)
;
,
.
.
,
, .
, .
,
.
, ,
..

. 2.1.

26
,
. 2.1. OK
, . 2.2.
Private Sub CommandButton1_Click()
Dim s As String
s = InputBox(" ? ", " ")
MsgBox " - " & s, 1, ""
End Sub

. 2.2
3. VBA
, VBA
,
.
,
( ).
, -

,
, , , .

: .
3.1.

, .
(=).
:
[Let+ ( ) =

27
,
, ,
.

Set. Set
1:3:
Set = Range("A1:B3")
Set :
Set ,*New] |
Nothing}
New
;
Nothing
,
, (
).

With. With

.
, :
With Range("A1")
.Value = 3
.Font.Italic = True
End With
With.
. {}+{
} ,
.
.

.
:
=+1
=+2

=+1:=+2
28
. ,
. VBA
:
( ' ).
. , ,
.
Rem .
3.2.
GoTo
.
.
GoTo
,
, .
.
GoTo
.
3.3.
()
,
, .
VBA
If Select Case.

If
, ,
.
, .
:
If <> Then <>

:
If <> Then
<1>
<2>
...
29
End if
True,
, False,
.
If ,
,
. If
.
If Then
<1>
Else
<2>
End if
, ,
.
1. ax + b = 0.
b
TextBox, (. 3.1).

. 3.1.

Private Sub CommandButton1_Click()
Dim a, b, x As Single
Dim s As String
a = TextBox1.Text
b = TextBox2.Text
If (a = 0) And (b <> 0) Then s = " "
If (a = 0) And (b = 0) Then s = " "
If a <> 0 Then s = " =" + (Str(Round(-b / a, 3)))
Label2.Caption = s
30
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
Label2.Caption = ""
End Sub
2. , ,
, ,
.
TextBox.
CheckBox.
. -
, , ,
, , ,
. (. 3.2).

. 3.2.
1
Dim a, b As Long
Private Sub CheckBox1_Change()
If CheckBox1.Value Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
31
End Sub
Private Sub CommandButton1_Click()
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
MsgBox Str(a) + "+" + Str(b) + " = " + Str(a + b), , ""
End Sub
Private Sub CommandButton2_Click()
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
MsgBox Str(a) + "-" + Str(b) + " = " + Str(a - b), , ""
End Sub
Private Sub CommandButton3_Click()
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
MsgBox Str(a) + "*" + Str(b) + " = " + Str(a * b), , ""
End Sub
Private Sub CommandButton4_Click()
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
If (b = 0) And (a <> 0) Then
MsgBox Str(a) + "/" + Str(b), 48, " "
Else
If (b = 0) And (a = 0) Then
MsgBox Str(a) + "/" + Str(b), 16, " "
Else
MsgBox Str(a) + "/" + Str(b) + " = " + Str(a / b), , ""
End If
End If
End Sub
Private Sub CommandButton5_Click()
a = Val(TextBox1.Text)
MsgBox Str(a) + "*" + Str(a) + " = " + Str(a * a), , " "
End Sub
Private Sub CommandButton6_Click()

32
a = Val(TextBox1.Text)
If a < 0 Then
MsgBox Str(a), 48, " "
Else
MsgBox " " + Str(a) + " = " & Sqr(a), , "
"
End If
End Sub
Private Sub CommandButton7_Click()
TextBox1.Text = ""
TextBox2.Text = ""
End Sub


.

Select Case,
:
Select Case <>
Case <c1>
<1>
Case <c2>
<2>
...
[Case Else
<Else>]
End Select
select, case, of, else, end -
(, , , , ).

Select Case. ,
, , .

, .
,
.
33

,
, Select Case
, , ..
.

,
Else, .
1. ,

.
ComboBox.
TextBox.
ListBox (.3.3). :
, , , .
.
ListBox.

. 3.3.

.
.. , ,
ListBox
.

On Error, ,

34
.
:
On Error Goto
,
,
, .
,
.
.
Err, .
Number Err .
6.1.

, 11.


, , 1-38,
6.
ListBox. ,
.
Description Err ,
.
.


6.3.

' Initialize Userform
Sub Userform_Initialize()
With ComboBox1
.List = Array("+", "-", "*", "/")
.ListIndex = -1
End With
End Sub
Private Sub CommandButton1_Click()
Dim x, y, z As Single
Dim d, s, s1 As String

35
Dim k As Integer
' ,
'
On Error GoTo
' ,
If IsNumeric(TextBox1.Text) = False Then
s = " " + TextBox1.Text
ListBox1.AddItem (s)
TextBox1.SetFocus
Exit Sub
End If
If IsNumeric(TextBox2.Text) = False Then
s = " " + TextBox2.Text
ListBox1.AddItem (s)
TextBox2.SetFocus
Exit Sub
End If
x = CSng(TextBox1.Text)
y = CSng(TextBox2.Text)
d = ComboBox1.Text
s1 = "": k = 0
Select Case d
Case "+"
z=x+y
Case "-"
z=x-y
Case "*"
z=x*y
Case "/"
z=x/y
Case Else
s1 = "": k = 1
End Select
If k = 0 Then
s = Str(x) + " " + d + Str(y) + " = " + Str(z)
Else
s = Str(x) + " " + d + Str(y) + " = " + s1
36
End If
ListBox1.AddItem (s)
'
'
Exit Sub
'
:
Select Case Err.Number
Case 11
ListBox1.AddItem (Str(x) + " " + d + Str(y) + " = " + " ")
Case 6
ListBox1.AddItem (Str(x) + " " + d + Str(y) + " = " + "")
Case Else
MsgBox " : " & Err.Description
End Select
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
ComboBox1.ListIndex = -1
End Sub
Private Sub CommandButton3_Click()
UserForm1.Hide
End Sub
.
1. A(x,y) (. 3.5)?

.
2

-2 4

. 3.5.

37
2.
: , , ,
, .
TextBox.

CheckBox.
. -
, ,
, , ,
, .
(. 3.6).

. 3.6.
3. , :
n 1 12 . ,

TextBox,
.
4. .
TextBox.
Image1
. Picture
LoadPicture: Image1.Picture = LoadPicture("\
") (.3.7). .
,
.
38
. 3.7.
5. , ,
ComboBox.
.
, , .
, ,
, ,
.
.
Rnd.
Rnd
Int.
ListCount.
ListBox. (. 3.8).

. 3.8.

39
3.4.
, ,
. ,
,
, .
VBA ,
:
1. For Next
2. Do Loop
For Next
.
Do Loop ,

, .
, .
:
1. Do While Loop
2. Do Until Loop.
For Next
:
For <> = <> To <>
[Step <>]
<>
[Exit For]
[<>]
Next [<c>]
:
<> ,
.
.
For Next ,
,
,
, ,
, .
,
.
40
Exit For .
:
,
. ,
1.
tg 2 1 tg 2 2 tg 2 k
1. S = ..... .
12 1 2 2 1 k 2 1
i ( integer) 1 k .
tg 2 i
: .
i2 1
TextBox,
(.3.9).

Private Sub CommandButton1_Click()
Dim s As Single, i, k As Integer
k = Val(TextBox1.Text)
'
'
If k < 0 Then
MsgBox " ", 48
Exit Sub
End If
s=0
For i = 1 To k Step 1
s = s + Tan(i) ^ 2 / (i ^ 2 + 1)
Next i
Label2.Caption = "S = " & s
End Sub
Private Sub CommandButton2_Click()
UserForm1.Hide
End Sub
Private Sub CommandButton3_Click()
TextBox1.Text = ""
Label1.Caption = ""

41
End Sub

. 3.9.
2. y = 3x 4lnx 5
[a,b] h. a, b, h
TextBox,
ListBox,
(. 3.10).

. 3.10.
1
Private Sub CommandButton1_Click()
Dim x, y, a, b, h As Single, s As String
s = ""
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
h = Val(TextBox3.Text)
' ,
' ,
'
If (h > 0) And (a > b) Then
MsgBox " a <= b", 48
Exit Sub
42
End If
If (h < 0) And (a < b) Then
MsgBox " a >= b", 48
Exit Sub
End If
If (a>0) And (b>0) Then
For x = a To b Step h
y = 3 * x - 4 * Log(x) 5
'
s = "x = " + Format(x, "####0.00") + " y = " + Format(y,"####0.00")
ListBox1.AddItem (s)
Next
Else
MsgBox "
lnx"
End If
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
ListBox1.Clear
End Sub
Do while
.
:
Do while <>
<>
[Exit Do]
[<>]
Loop
:
, ,

. ,

43
false. false,
Do while .
,
.
,
, .

Exit Do.
.
n. n
TextBox, - (. 3.11).

. 3.11.

Private Sub CommandButton1_Click()
Dim n As Long, p, a As Integer
n = Val(TextBox1.Text)
' n
'
If n< 0 Then
MsgBox " ", 48
Exit Sub
End If
p=1
Do While n > 0
a = n Mod 10
n = n \ 10
If a <> 0 Then p = p * a
Loop
Label2.Caption = "p = " & p
44
End Sub
Private Sub CommandButton2_Click()
UserForm1.Hide
End Sub
Private Sub CommandButton3_Click()
TextBox1.Text = ""
Label2.Caption = ""
End Sub
Do until
,
.
:
Do until <>
<>
[Exit Do]
[<>]
Loop
:
,
, . ,
.
, .
true, .
true ,
. do until
,
, .

1
. S = i
i 1
2

.
1 1 1 1
S = 1 2
2 2 ... 2 ....
2 3 4 n

TextBox, 2 (. 3.12).

45
: i,
.
i,
1
. ,
i2
, .
1
Private Sub CommandButton1_Click()
Dim i As Integer, a, s, eps As Single
eps = Val(TextBox1.Text)
a=1:s=1:i=2
Do Until a < eps
a=1/i^2
s=s+a
i=i+1
Loop
Range("B2").Value = Round(s, 3)
End Sub

. 3.12.
.
1.
sin 1 sin 2 sin 3 sin n
s= (1) n 1 . n
1 2 3 n
TextBox,
.
100
a(i 1)
2. s = bi2.
i 3


46
. a, b
TextBox, .

x
3. s = xi
i 0
2

.
TextBox,
.
x sin 3x x 3 5 cos x
4. y =
x2 3
[a,b] h.
.
a, b, h TextBox,
.
1
5. y = x 1 [a,b]
x
h. a, b, h
TextBox,
ListBox.
6.
'z a. Chr(N),
,
N. N 0 255.
97 122.
7. n.
.
. n
TextBox,
.
8. n. ,
, .

. n TextBox,
.
4.
- .

47
,
.
: a = ""
:
Dim _ As string

String
& (). ,
Dim a As Single
a = 12.456
Label1.Caption = "a= " & a
= 12.456
(+)
. :
Label1.Caption = "a= " + Str(a) , .
,
(
),
String.

,
.

Like, ,
, Like *
True,
. (*)
. , Like
:
? ;
# - (0 -9);
[<>] ,
;
[!<>] - ,
.
:
Dim a As String
a = "information"
48
If a Like "informatio[t,i,o,n,h]" Then Label1.Caption = "yes" Else
Label1.Caption = "no"
yes.
a Like "informa[t,i,o,n,h]ion"
True.

.
:
LCase (<>)
, , LCase()
.
UCase (<>)
.
:
Spae (<>) ,
.
String (<>,<>) - ,
.
.


Len((<>)
Left(<>,<>) <>

Right(<>,<>) <>

Mid(<>, <>
<>,<>)
, <>
Mid(<>, <>
<>)
LTrim(<>)
RTrim(<>)
Trim(<>)
InStr(<>, .
<1>, <2>
*,<>+) <2> <1>,

49
<> -
. ,
.
InStrRev(*<>,+ ,
<1>, <2> .
*,<>+)
.
<>
.
Replace(<>,
< >, .
< >) < >
< >
< >

1. , .
.
TextBox, 2 (.4.1).

. 4.1.
1
Private Sub CommandButton1_Click()
Dim s1, s2 As String, i, s As Integer
s1 = TextBox1.Text :s = 0
For i = 1 To Len(s1)
s2 = Mid(s1, i, 1)
If (s2 >= "0" And s2 <= "9") Then s = s + Val(s2)
Next
Range("B2").Value = s
End Sub

50
2. . ,
ab *ab*.

TextBox (. 4.2).

Private Sub CommandButton1_Click()
Dim s1, s2 As String
s1 = TextBox1.Text
s2 = Replace(s1, "ab", "*ab*")
TextBox2.Text = s2
End Sub

. 4.2.
.
1. .

TextBox.
2. . ,
"" "" "".

TextBox.
3. , .
"" ,
.
TextBox,
.
4. . ,
"".
TextBox.

51
5. .
"$" "%"?
TextBox,
.
6. . "x" "y",
"z" "t".

TextBox.
5.
5.1.
,
,
( ). ,

.
Dim:
Dim <> (<1>,<2>,) As
<1>,<2>,

.
.
: Dim A(3) As Integer
,
Integer.


() .
, Option Base.
, Option Base 1,

, .

To :
Dim A (1 To 3) As Integer

, ,
.
Dim Sotrudnik (5) As String, i As Integer
52
For i=0 to 5
Sotrudnik(i) = ""
Next i

. ,
, Object:
Dim A (2) As Object
:
A (0) = ""
(1) = 56
(2) = 3.1415
5.2.

.
Dim B (1 To 2, 1 To 2) As Single
B (1, 1) = 2
B (1, 2) = 5
B (2, 1) = 4
B (2, 2) = 3
2 5
:
4 3

.
Array ( ) Variant.

, .
VBA ,
:
Lbound ( *,+)
Ubound ( *,+)
,
, ,
.
1, - 2 ..
, 1.
Erase .
,
53
, , VBA
.
Erase :
Erase array1 [,array2, +
array1 ,array2 VBA.
.
Split (<> *,<>+) -
. .

.
.
Join (< >*,<>+)
.
Filter (< >,< > *,<>+ *,<>+)

, .
:
;
( ), ,
, ,
,
.
VBA ,
,
For Next. ,

. :
For Each <> in <>
<>
Next *<>+
<> - ,
;
<> - .
1. .
. TextBox,
(. 5.1).

54

Private Sub CommandButton1_Click()
Dim a, b As Variant, k As Boolean
Dim s1, s As String
'
a = Array("", "", "", "", "")
s = TextBox1.Text '
k = True '
If TextBox2.Text = "true" Then
k = True
Else
If TextBox2.Text = "false" Then k = False Else MsgBox "
", 48
End If
' ,
'
b = Filter(a, s, k)
' s1
s1 = Join(b)
'
Label1.Caption = s1
End Sub

. 5.1.
2 . A(n).
.
TextBox.
. (. 5.2).

Private Sub CommandButton1_Click()
55
Dim a As Variant, s, i, n As Integer
If TextBox1.Text <> "" Then
'
a = Split(TextBox1.Text)
'
n = UBound(a)
s=0
'
For Each b in a
s=s+b
Next
Label1.Caption = " : " & Format(s / (n + 1),
"###0.00")
Else
MsgBox " ", 48
End If
End Sub

. 5.2.
3 . A(n).
.
TextBox,
ListBox,
(. 5.3).
1
Private Sub CommandButton1_Click()
Dim i, n, max, min, k, m As Integer
Dim a As Variant
If TextBox1.Text = "" Then
56
MsgBox " ", 48
Exit Sub
End If
'
a = Split(TextBox1.Text)
'
n = UBound(a)
'
'
max = Val(a(0)): min = Val(a(0))
k = 0: m = 0
For i = 1 To n
If Val(a(i)) > max Then
max = Val(a(i)): k = i
End If
If Val(a(i)) < min Then
min = Val(a(i)): m = i
End If
Next
a(k) = Str(min): a(m) = Str(max)
For i = 0 To n
ListBox1.AddItem (a(i))
Next
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
ListBox1.Clear
End Sub

. 5.3.
57
4. Excel ,
2 (. 5.4).
ListBox.
TextBox.
: ,

.
OptionButton.

. 5.4.
. 5.5.
,
ListBox .

.

. 5.5.

Dim n, m, i, j, sum As Integer
Dim sr As Double, p As Long
Dim s1 As String
Private Sub CommandButton1_Click()
n = Val(TextBox1.Text)
m = Val(TextBox2.Text)
58
' ,
'
If (n<= 0) Or (m<=0) Then
MsgBox " ", 48
Exit Sub
End If
' s1 ,
s1 = "B2:" + Chr(66 + m - 1) + Chr(49 + n)
' - 48 57
' A Z - 65 90
Range(s1).Select '
With ListBox1
.ColumnCount = m '
.RowSource = (s1) '
End With
End Sub
Private Sub CommandButton2_Click()
Dim s2 As String
If OptionButton1.Value = True Then
sum = o
With ListBox1
For i = 0 To n - 1
If .Selected(i) = True Then
For j = 0 To m - 1
sum = sum + .List(i, j)
Next j
End If
Next i
End With
Label3.Caption = Label3.Caption + " " + Str(sum)
End If
If OptionButton2.Value = True Then
p=1
With ListBox1
For i = 0 To n - 1
If .Selected(i) = True Then

59
For j = 0 To m - 1
p = p * .List(i, j)
Next j
End If
Next i
End With
Label3.Caption = Label3.Caption + " " + Str(p)
End If
If OptionButton3.Value = True Then
sum = o
With ListBox1
For i = 0 To n - 1
If .Selected(i) = True Then
For j = 0 To m - 1
sum = sum + .List(i, j)
Next j
End If
Next i
End With
sr = sum / m
Label3.Caption = Label3.Caption + " " + Str(sr)
End If
End Sub
:
1. (n).
.
TextBox.
, - ,
k p (
). .
2. . -
(. . ,
)? , .

TextBox, ListBox.

60
3. A(n). , ,
.
TextBox. .
4. A(n).
, .
TextBox,
.
5. . .

TextBox, .
6. .
.
TextBox,
ListBox.
7. Excel ,
1. ListBox.

TextBox1 TextBox2.
:
,

.

OptionButton.
5.3.
, ,
,
.
Dim, Private,
Public, ,
ReDim
.
ReDim :
ReDim [Preserve] () [As ] _
[, () [As ] ]
Preserve ,
VBA ;
- ;
61
;
VBA .
:
1) Dim Month( ) As String - Month;
2) Redim Month(1 To 30) 30
;
3) Redim Preserve Month(1 To 31) 31
, ;
4) Dim Table( ) As Integer ;
5) ReDim Table(3, 15) ;
6) ReDim Table(4, 20) ;
7) ReDim Preserve Table(4, 24)
;
8) Dim Mas As Variant Variant;
9) ReDim Mas(20) As Integer 20
Variant.
1. A(n,m)
. 100
. .
.
ListBox (. 5.6).

'
Dim a() As Single
Dim m, n, i, j As Integer, s As Single
Private Sub CommandButton1_Click()
n = Val(TextBox1.Text)
m = Val(TextBox2.Text)
' ,
'
If (n<= 0) Or (m<=0) Then
MsgBox " ", 48
Exit Sub
End If
'
ReDim a(1 To n, 1 To m)

62
'
For i = 1 To n
For j = 1 To m
a(i, j) = Int(Rnd(20) * 100)
Next
Next
' ListBox
With ListBox1
.ColumnCount = m
.List = a
End With
End Sub
Private Sub CommandButton2_Click()
' ListBox
ListBox1.Clear
' 1,
'
ReDim Preserve a(1 To n, 1 To m + 1)
'
'
For i = 1 To n
s=0
For j = 1 To m
s = s + a(i, j)
Next
a(i, m + 1) = s
Next
' ListBox
With ListBox1
.ColumnCount = m + 1
.List = a
End With
End Sub
Private Sub CommandButton3_Click()
ListBox1.Clear
TextBox1.Text = ""

63
TextBox2.Text = ""
End Sub
Private Sub CommandButton4_Click()
UserForm1.Hide
End Sub

. 5.6.

1. n*n.
,
.
ListBox, .
2. n*n.
.

. ListBox.
3. x(n,m) x(i,j) = i2 +
j2. .

.
ListBox.
4. n*n.
3.
ListBox.
5. y(n,m) y(i,j) =
(i+1)*(j+2). .

64
.
ListBox.
6. n*m.
, 5.
ListBox,
.
6.

,
.
().

.
VBA :
;
.
,
, ,
. , CommandButton1_Click
CommandButton1.
.
, .
-
.
, ,
. General
( ) .
Sub End Sub,
Function End Function.

6.1.
:
[Private | Public] Sub < > [(<>)]
<>
[Exit Sub]
<>
End Sub
65
<> - ,

. .
*(<>)+
:
[Optional] [ByVal|ByRef] [ParamArray] [As ]
Optional , ,
.
, <>,
,
Optional. , Optional,
Variant.
Optional ,
ParamArray.
ByVal - , ,
.
ByRef - , ,
. ByRef .
ParamArray - ,
<> ,
Optional
Variant. ParamArray
.
ByVal, ByRef Optional.
, .
: Byte, Boolean, Integer, Long, Currency. Single,
Double, Data, String, Object, Variant.
Optional,
.
:
[Private | Public] Function < > *(<>)+ _ [As
< >+
<>
< > = < >
[Exit Function]
<>
< > = < >
66
End Function

.
.
Variant.

,
. :
< >

Call < > ( )

, .

.
,
.
:
< > = < > (
)

.

: .
, ,
.

.
:
,
.
,

, ,
.

, .

67

:
. ByRef
, ByVal .
. , Primer1(x, ByVal y,
ByRef z). ( ),
, .
,
,
, ,
.
, ,
,
, ..
.
Public Private
.
Private
, ,
. ,
Public,
, .. .

Private. ,
. ,
, .

Private.
, Public,
,
. Private Public
, .
6.2.

.
Insert, Module.
.

68
n!
C .
m !(n m)!

0, n 0

F 1, n 0
n !, n 0

. 6.1.
.
1. VBA ,
Insert, Module.
2. :
Function F(n As Integer)
Dim f1 As Long
If n < 0 Then f1 = 0
If n = 0 Then f1 = 1
If n > 0 Then
f1 = 1
For i = 1 To n
f1 = f1 * i
Next
End If
F = f1
End Function

69

.
3. 1, 2 2
n m (.6.1).
4. 2, n!.
5. ,
.
6. ,
, F
.
D2 E2 m! (n-m)!,
F2
=((E2>0;D2>0);C2/(D2*E2);" ")

.
.
.
Sumd.
. 5
. 5 Sumd(A1:D4) (. 6.2).

. 6.2.
Module1
Function sumd(rng As Range)
Dim c As Range
Dim s As Double
s=0
For Each c in rng.Cells
s = s + c.Value
70
Next
sumd = s
End Function
1.
. .

(. 6.3).

. 6.3.

c a 2 b 2 2ab cos , a,
b, c , - a b.

st3.

s p( p a)( p b)( p c) , a, b, c
abc
, p= .
2
Geron.

Const pi As Double = 3.14
'
Function st3(a, b, x As Variant) As Single
st3 = Sqr(a ^ 2 + b ^ 2 - 2 * a * b * Cos(pi * x / 180))
End Function

71
'
Sub Geron(a, b, c, s As Variant)
Dim p As Single
p = (a + b + c) / 2
s = Sqr(p * (p - a) * (p - b) * (p - c))
End Sub
Private Sub CommandButton1_Click()
Dim s As String
Dim a1, b1, a2, b2, s1, s2, c1, c2 As Single, x1, x2 As Integer
s = ""
'
a1 = Val(TextBox1.Text)
b1 = Val(TextBox2.Text)
x1 = Val(TextBox3.Text)
a2 = Val(TextBox4.Text)
b2 = Val(TextBox5.Text)
x2 = Val(TextBox6.Text)
c1 = st3(a1, b1, x1)
c2 = st3(a2, b2, x2)
Geron a1, b1, c1, s1
Geron a2, b2, c2, s2
'
s = s + "s1=" + Format(s1, "###0.00") + " s2=" + Format(s2, "###0.00") +
Chr(13) + Chr(13)
If s1 > s2 Then s = s + " " Else If s1
= s2 Then s = s + " " Else s = s + "
"
Label7.Caption = s
End Sub
2. x(n), y(m), z(k) .
.

TextBox,
ListBox,
(. 6.4).
VBA
. ,
72
,
.
, .
:
1.
ListBox. : ,
ListBox.
2. .
: , .

. 6.4.
1
'
' ListBox
Sub form(a() As Single, l As Variant, obj1 As Object)
Dim i As Integer
For i = 1 To l
a(i) = Round(Rnd(1) * 100) - 25
obj1.AddItem (Str(a(i)))
Next
End Sub
'
Sub maxim(a() As Single, max As Variant)
max = a(1)
For Each c In a

73
If c > max Then max = c
Next
End Sub
Private Sub CommandButton1_Click()
Dim x() As Single, y() As Single, z() As Single
Dim mx, my, mz, crmax As Single
Dim n, m, k As Integer
n = (TextBox1.Text)
m = (TextBox2.Text)
k = (TextBox3.Text)
' ,
'
If (n<= 0) Or (m<=0) Or (k<=0) Then
MsgBox " ", 48
Exit Sub
End If
'
ReDim x(1 To n) As Single
ReDim y(1 To m) As Single
ReDim z(1 To k) As Single
' ListBox
Call form(x, n, ListBox1)
Call form(y, m, ListBox2)
Call form(z, k, ListBox3)
'
Call maxim(x, mx)
Call maxim(y, my)
Call maxim(z, mz)
'

crmax = (mx + my + mz) / 3


Label7.Caption = "
" + Format(crmax, "###00.00")
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""

74
TextBox2.Text = ""
TextBox3.Text = ""
ListBox1.Clear
ListBox2.Clear
ListBox3.Clear
Label7.Caption = ""
End Sub
4. z x1 x2 x3 ,
n1 n2 n3

a i b i c i
x1 i 1
, x2 i 1
, x3 i 1
ai , bi , ci -
n1! n2! n3!
a, b, c; n1, n2, n3 a, b, c
.
.
,
- TextBox (. 6.5).

. 6.5.
Module1
'
Function Summa(d)
Dim s As Single
s=0
For Each b In d
s=s+b
Next
75
Summa = s
End Function
Function Fact(n)
Dim f As Long, i As Integer
f=1
For i = 1 To n
f=f*i
Next
Fact = f
End Function
1
Private Sub CommandButton1_Click()
Dim a, b, c As Variant
Dim z, x1, x2, x3 As Double
Dim n1, n2, n3 As Integer
a = Split(TextBox1.Text)
b = Split(TextBox2.Text)
c = Split(TextBox3.Text)
If (TextBox1.Text = "") Or (TextBox2.Text = "") Or (TextBox3.Text = "") Then
MsgBox " ", 48
Exit Sub
End If
'
n1 = UBound(a)+1
n2 = UBound(b)+1
n3 = UBound(c)+1
x1 = Summa(a) / Fact(n1)
x2 = Summa(b) / Fact(n2)
x3 = Summa(c) / Fact(n3)
' 1, 2, 3 1, 2, 3
Range("B1").Value = Round(x1, 3)
Range("B2").Value = Round(x2, 3)
Range("B3").Value = Round(x3, 3)
z = x1 + x2 + x3
' z 4
Range("B4").Value = Round(z, 3)
76
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Range("B1:B4").Clear
End Sub
6.3.
,
,
, .
:
1. :
,
, ,
.
2. : ,
,
.
,
:
a(n).
.
(. 6.6).

. 6.6.

.

77
.. , ,
.
,
,
Microsoft
Visual Basic : Overflow (. 6.7).
.

.

. 6.7.

Private Sub CommandButton1_Click()
Dim a As Variant
Dim i, k As Integer
Dim s As Single
If TextBox1.Text = "" Then
MsgBox " ", 48, "
"
Exit Sub
End If
a = Split(TextBox1.Text)
' ,
For Each b In a
If IsNumeric(b) = False Then
MsgBox " "+b, 48, _
" "
TextBox1.SetFocus
78
Exit Sub
End If
Next
s = 0: k = 0
For Each b In a
If Val(b) < 0 Then
s = s + Val(b) : k = k + 1
End If
Next
If k = 0 Then
MsgBox " ", 48, _ "
"
Exit Sub
End If
Label2.Caption = Label2.Caption + " " + Str(Round(s / k, 3))
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
Label2.Caption = ""
TextBox1.SetFocus
End Sub
Private Sub CommandButton3_Click()
UserForm1.Hide
End Sub

.
6.1.
6.1.

5
6
7
9
11
79
13
18 ,
52
53
54
55
56 -
61
76


, ,
.
.
On Error. ,
,
. .
1: On Error Goto
,
,
, .
,

2: On Error Resume Next
,
,
, .
3: On Error Goto 0

.
Resume.
.
.
1: Resume
,
.
2: Resume

80
,
.
.
3: Resume Next
,
, .
Exit. .
:
Exit Sub
Exit Function
Exit Property
Err,
.
Err:
Number
Source VBA
Description ,

HelpFile VBA
HelpContext VBA,
,
Number

Err Clear.
Err. Clear
Err .
, , ,
On Error Resume Next.
,
On Error Err .
. ,
.
ComboBox
.
Image1 PictureSizeMode
fmPictureSizeModeZoom.
, r
81
ComboBox. ,
, ListBox.
ListBox .

, - .
.
, (. 3.4).

. 3.4.

'
Sub _()
Select Case Err.Number
Case 53
MsgBox " ", 48, " "
Case 76
MsgBox " ", 48, " "
Case Else
MsgBox " : " & Err.Description
End Select
End Sub
' Initialize Userform
Private Sub userform_initialize()
' ,
82
' 1
On Error GoTo 1
' ListBox
ListBox1.Visible = False
With ListBox1
.ColumnCount = 1
.RowSource = ("B1:B20")
End With
With ComboBox1
.ColumnCount = 1
.RowSource = ("A1:A20")
'
.ListIndex = 0
End With
'
Image1.Picture = LoadPicture(ListBox1.List(0, 0))
Exit Sub
'
1:
'
Call _
End Sub
Private Sub CommandButton1_Click()
Dim i As Integer
' ,
' 2
On Error GoTo 2
i = ComboBox1.ListIndex '
If ListBox1.List(i, 0) = "" Then
MsgBox " ", 48, " "
End If
Image1.Picture = LoadPicture(ListBox1.List(i, 0))
'
Exit Sub
'
2:
'
83
Call _
End Sub
.
:
;
Initialize Userform;
CommandButton1_ Click.
_
. ,
53,
- 76.
. ,
,
. .
Initialize Userform
ComboBox 1:20,
, ListBox -
1:20 .
,
- .
_.
CommandButton1_ Click

.
, _.

1. ABCD AB = x, BC = y, CD = z, DA =t,
ABC= . ,


.
.

2. V1, V2, V3 , r1, r2, r3.


.
,
,
84
. :
4
V= R 3 .
3
TextBox,
.
3.
M P
, .
N Q

.
s1 s 2
4. z . s1 , k1 -
k1 * k 2
x(n), s 2 , k 2 -
y(m).
,
.
TextBox,
.
5.
a(n), b(m), c(k). ,
.

.
6.
.

: C p a bT cT 2 dT 3 .
a, b, c, d ,
.
(
),
.
300,
500, 750. :
a 33, 46 x103 , b 0,6880 x105 , c 0,7607 x108 , d 3,593x1012

85
7. VBA Excel
7.1. Application
Application ()
Excel Excel.
120 40 .
Excel. ,
Application 400
:
Application. ()
:
Application.Pi()
Application.Pmt()


()
Application.Sum()

Application
ActiveWorkbook : , ,
ActiveSheet , , .
ActiveCell
ActiveChart
ActiveDialog :
With ActiveCell
.Font.Bold = true
.Value = " "
End With
Application
Run .
:
Run (Macro, Arg1, Arg2, )
Macro
Arg1, Arg2, -
:
Application.Run.Macro:= ""
86

Wait
. :
Wait(Time)
, ,
17
:
Application. Wait "17:00:00"
OnKey
. :
OnKey (Key, Procedure)
Procedure -

Key - , ,
.

.
:
<Shift> - +
<Ctrl> - ^
<Alt> - %

<Ctrl>+<+>,
- <Shift>+<Ctrl>+<>:
Application. OnKey "^,+-", """
Application. OnKey "+^{RIGHT-", " "
Quit .
7.2. Workbook Workbooks
Excel Workbook ( )
Application .
XLSX (
), XLSM ( ), XLS (
Excel97-2003) XLA ( ).
Workbook Workbooks
ActiveSheet . :
MsgBox " " & ActiveSheet.Name
-
87

ActiveChart
Sheets
Worksheets
Count Workbooks
Workbook Workbooks
Activate ,
.
Add .
Close .
Open .
Save .
SaveAs . :
SaveAs (Filename)
Filename - , ,

7.3. Worksheet Worksheets
Excel Worksheet
Workbook .
Worksheet Worksheets
Name
UsedRange , .. Range,
.

:
Worksheets(1). UsedRange.Clear
ActiveCell

Intersect ,
. :
Intersect (range1, range2, ...)

A1:D3 C3:D4, .. C3:D3
Intersect (Range("A1:D3"), Range ("C3:D4")).Select
Union ,
88
. :
Union (range1, range2, ...)

A1:B2 C3:D4
Union (Range("A1:B2"), Range ("C3:D4")).Select
Worksheet Worksheets
Activate
Add . :
Add (Before, After, Count, Type)
Before - ,

After - ,
. Before After
,

Count - , 1
Type - .
: x1Worksheet ( ),
x1ExcelMacroSheet, x1ExcelIntMacroSheet
:
ActiveWorkbook. Worksheets.Add
-

Delete .
:
Worksheets(1). Delete
-

Copy .
:
Copy (Before, After)
Before -

After -


. 1

89
3
:
Worksheets("1"). Copy After:= Worksheets("3")
7.4. Range Selection
Excel Range
Worksheet. Range
VBA. Selection () VBA
Select, Selection.
.
Range Selection ,
.

Range , Excel
.
:
. :
1 ( 256 A, B, . .
. Z, AA, AB, . . . IV) (1, . . . 16384)

R1C1 . , R2C3, R4C5
:
A1
$,
. , $A10, A$10, $A$10

R1C1 . ,
. ,
R2C3 , R[1]C[-1]
R3C2.

,
.
!, .
,
1
2!1
90
[1.xlsx] 2!1

,
Range ,
. , Range("A:C") ,
, , , Range("2:2") .
Rows () Columns
(). Columns(1),
Rows(2).
Range Cells
,
, Range .
Cells () . ,
2 Range("A2") Cells(1,2).
, Cells Range,
. Range("A2:3") Range(Cells(1,2), Cells(3,3))
.
Range
Value
.
1:
= Range("1").Value
Name .
1:2 :
Range("1:2"). Name=""
Count .
,
1:2 :
= Range("1:2").Rows.Count
Formula 1. ,
2

=$A$4+$A$10: Range("2"). Formula =
"=$A$4+$A$10"
FormulaLocal ()
1. ,
91
2 =(1:4) :
Range("2"). FormulaLocal = "=(1:4)"
FormulaR1C1 R1C1. ,
Range("2"). Formula R1C1 = "=SQR(R3C2) "
FormulaR1C1Local ()
R1C1.
Text
.
Range
Adress . :
Adress(rowAbsolute,colomnAbsolute,referencesStyle,
external, relativeTo)
:
rowAbsolute True
False. True ,
;
colomnAbsolute True
False. True ,
;
referencesStyle - 11
1R1C1, 11
,
1;
external - True False.
False ,
;
relativeTo , rowAbsolute
colomnAbsolute False, referencesStyle -
1R1C1,
,
.

.
MsgBox Cells(1, 1). Adress()
' $A$1
MsgBox Cells(1, 1). Adress(rowAbsolute:=False)
' $A1
MsgBox Cells(1, 1). Adress(referencesStyle:= 1R1C1)
92
' R1C1
Clear
Copy
. : Copy (destination)
A1:D4 1
E5:H8 2:
Worksheets("1").Range("A1:D4").copy _
destination:= Worksheets("2").Range("E5")
Columns (),
Rows
I=Selection. Columns.Count
J= Selection. Rows.Count
Delete
Rows(3).Delete
Insert
Worksheets("1").Rows(4).Insert

1
Offset ,
.
:
Offset(rowOffset, columnOffset)
:
rowOffset ,
;
columnOffset - ,
.
, ,

:
ActiveCell.Offset(3,-2). Activate
Select
7.5. ActiveCell

ActiveCell
,
.
93
, ActiveCell.Value. ,
5,
100, .
If ActiveCell.Value >= 5 Then
ActiveCell.Value = 100
Else
ActiveCell.Value =
EndIf


Font.
ActiveCell.Font.Color .ColorIndex ;
Color=RGB(red, green, blue), red, green, blue
, ,
0 255. ColorIndex 0 255.
, ActiveCell.Font.Color = RGB(0,255,0)
.
ActiveCell.Font.Name , ,
, Courier.
ActiveCell.Font.Size , ,
ActiveCell.Font.Size = 24.

Offset ActiveCell ,

. Activate
.
Offset(rowOffset, columnOffset) :
rowOffset , ;
columnOffset - , .


Offset
.

94
1. y=sinx,
0 180 30,
(. 7.3).
1
Private Sub CommandButton1_Click()
Const pi = 3.14
Dim x, dx As Integer
Dim y As Double
' dx 1
'
x = 0: dx = Cells(1, 3).Value
' 2
Cells(2, 2).Activate
For x = 0 To 180 Step dx
'

ActiveCell.Offset(0, -1).Value = x
y = Sin(x * pi / 180)
' y
ActiveCell.Value = y
'
ActiveCell.Offset(1, 0).Activate
Next
End Sub

. 7.3.

95
2. Excel
(. 7.4).
, ,
. I2
.

. 7.4.
1
Dim n, i, j As Integer, st As Single
Private Sub CommandButton1_Click()
st = Range("I2").Value
'
n = Application.CountA(ActiveSheet.Columns(1)) - 2
' 5
Cells(5, 5).Activate
For i = 0 To n 1
'
'
ActiveCell.FormulaR1C1 = "=Round(SUM(RC[-3]:RC[-1])/3,2)"
' ,

If ActiveCell.Value >= 4 Then
ActiveCell.Offset(0, 1).Value = st
Else
ActiveCell.Offset(0, 1).Value = " "
End If
'
ActiveCell.Offset(1, 0).Activate
96
Next
End Sub
Private Sub CommandButton2_Click()
m=n+5
Range(Cells(5, 5), Cells(m, 6)).Clear
End Sub

VBA .

, ,
.
Variant.

:
Dim r As Range
Set r = Range("A1:D3")
Dim m As Variant
Dim i, j As Integer
m = r.Value

:
Dim r As Range
Dim m(1 To 9, 1 To 9)
Dim i, j As Integer
For i = 1 To 9
For j = 1 To 9
m(i, j) = i * j
Next
Next
Set r = Range(Cells(1, 1), Cells(9, 9))
r.Value = m

1. Excel a (n,n).
.
(.7.6).

97
. 7.6.
1
Private Sub CommandButton1_Click()
Dim r As Range
Dim a As Variant
Dim i, j, n As Integer
Dim s As Single
' r ,
Set r = Worksheets(1).UsedRange
'
n = r.Rows.Count
a = r.Value
'

s=0
For i = 1 To n
For j = 1 To n
If j < i Then s = s + a(i, j)
Next j
Next i
Label2.Caption = "s= " + Str(s)
End Sub
Private Sub CommandButton2_Click()
Label2.Caption = ""
Worksheets(1).UsedRange.Clear
End Sub
2. Excel a (n,m ).
, 7, .

98

(. 7.7).

. 7.7.
1
Private Sub CommandButton1_Click()
Dim r As Range
Dim a As Variant
Dim i, j, n, m As Integer
Set r = ActiveSheet.UsedRange
'
n = r.Rows.Count
m = r.Columns.Count
a = r.Value
For i = 1 To n
For j = 1 To m
If a(i, j) Mod 7 = 0 Then a(i, j) = a(i, j) + i + j
Next j
Next i
Set r = Range(Cells(n + 4, 1), Cells(2 * n + 3, 1 + m - 1))
r.Value = a
'
Range("B1").Value = " "
Range("B" + Trim(Str(n + 3))).Value = " "
End Sub
99
Private Sub CommandButton2_Click()
ActiveSheet.UsedRange.Clear
End Sub

1. Excel a (n,m).
.
.
2. Excel a (n,m).
20,
5.
.
3. Excel a (n,m).
, , -
.
.
4. Excel -
, 5 .
.
,
.

.
5. Excel

.
,
: 370
, 280 .
0,25
.
.
8. Range, Excel
8.1. GoalSeek
,
,
,
. ,
100

. GoalSeek ( )

.
, .
,
.
GoalSeek (
), .
, :
, ,
.
: . GoalSeek(Goal, ChangingCell)
:
, ,
.
( )
, ChangingCell
Goal ,

ChangingCell ,
( ). ,
GoalSeek,


, ,
,
.
- ,
.
Excel , ..
, ,
.
GoalSeek .
:
(ChangingCell), .
(Goal)
,
101
, ,
(, , ).
,
, ,
MaxChange MaxIterations
Application. , 0,0001
1000 :
With Application
. MaxIterations = 1000
. MaxChange = 0.0001
End With
1.
(22+3)(1-sinx)-lnx=2.
Excel.
GoalSeek,
, ,
.
5 , , .
3 (
). (. 8.1).

. 8.1.
Click
CommandButton1 ( ). 4
.
GoalSeek: 4 2,
5, .. :
Range("B4").GoalSeek goal:=2, CchangingCell:=Range("B5")

5 ( 0,635796191)
4 ( )
102
1,999809434) (. 8.2).
4
1,999809434, 2.
.
1
Private Sub CommandButton1_Click()
Range("B4").FormulaLocal = "=(2*B5^2+3)*(1-sin(B5))-ln(B5)"
Range("B4").GoalSeek goal:=2, changingCell:=Range("B5")
End Sub

. 8.2.
MS Excel 2010
.
3, 5

4, ,

"-"
.

.
2. 5%
$150 .

.

GoalSeek.

. :
103
(; ; ; ; )

, .
:

,
. , 0.
.
0 1, ,
. ,
0.

0
1

2 , 3
(. 8.2). 4
:
Range("B5").FormulaLocal = "=(B2;B3;B4)"
GoalSeek: 5
150000, 4, .. :
Range("B5").GoalSeek goal:=150000, changingCell:=Range("B4")

4 - $27146,22
5 $150000. (. 8.3).

. 8.3.
1
Private Sub CommandButton1_Click()
Range("B5").FormulaLocal = "=(B2;B3;B4)"
104
Range("B5").GoalSeek goal:=150000, changingCell:=Range("B4")
End Sub
8.2. AutoFill
AutoFill ()
.
, .

(
, )
,
.
:
.AutoFill(destination, type)
:
,
destination ,
type : x1FillDefault, x1FillSries,
x1FillCopy, x1FillFormats, x1FillValues, x1FillDays,
x1FillWeekDays, x1FillMonths, x1FillYears,
x1LinearTrend, x1GrowthTrend.
x1FillDefault

1.
= 2 - 8 -3 3 1.

. 8.4.
. 2
, 3
- . 2:3,
105
. ,
,
8. 2 y: =2^2-8.
8
(. 8.4).
VBA :
destination
Range("A2:A8"), type x1FillDefault,
Range("A2:A3").
destination
Range("2:8"), type x1FillDefault,
Range("2").
, :
Range("A2:A3").AutoFill Destination:=Range("A2:A8"), _
Type:=x1FillDefault
Range("B2").FormulaLocal = "=A2^2-8"
Range("2").AutoFill Destination:=Range("B2:B8"), _
Type:=x1FillDefault

2. , n
, n, 1
10. -2, d = 0,725, n
: n=a1 + d(n-1), : sn =
(a1+an)*n/2.
. (. 8.5).
5 d. 5 d,
,
,
, d.

, 5 , D5 S1.
AutoFill Range("B5:B6"),
destination Range("5:14"). 6

R1C1. AutoFill Range("6"),
destination Range("6:14"). D6

106
R1C1. AutoFill Range("D6"),
destination Range("D6:D14").

.8.5.
1
Private Sub CommandButton1_Click()
Dim d As Single
d = Range("A5").Value
Range("B5:B6").AutoFill Destination:=Range("B5:B14")
Cells(6, 3).Activate
ActiveCell.FormulaR1C1 = "=R[-1]C+d"
Range("C6").AutoFill Destination:=Range("C6:C14")
Cells(6, 4).Activate
ActiveCell.FormulaR1C1 = "=(R5C3+RC[-1])*RC[-2]/2"
Range("D6").AutoFill Destination:=Range("D6:D14")
End Sub
8.3. Sort

. Microsoft Excel
, .

, .

, - .
107
,
, ,
( ),
(. 8.6).

. 8.6.

VBA
, ,
Sort, ,
, .
:
. Sort (key1, order1, key2, order2, key3, order3, header,
orderCustom, matchCase, orientation)
:
,
key1
order1 .
:
x1Ascending ( )
x1Descending ( )
key2
order2 .
:
x1Ascending ( )
x1Descending ( )

108
Key3
Order3 .
:
x1Ascending ( )
x1Descending ( )
header :
x1Yes (
, )
x1No (
)
x1Guess (Excel , )
orderCustom .
Normal
matchCase : True ( )
Fulse ( )
orientation :
x1TopToBottom (
, .. ),
x1LeftToRight ( )

8.4. AutoFilter
MS Excel
.
;
.
,
, .
, ,
, .
,

.
,
() ,
.

109
. 8.7.

.
1. .
2. ,
.
.
3. .
4.
(. 8.7).
AutoFilter () Range

, .
AutoFilter .
1:
. AutoFilter
AutoFilter
, ,
.
2:
.AutoFilter(field,criteria1,operator, criteria2)
110
AutoFilter ,
, .
:

field , ,

criteria1 .
criteria2 ,
101, >, <, >=, <=, =, <>
operator :
x1And (
)
x1Or (
)
x1Top10Items (
)
ShowAllData
FilterMode AutoFilterMode.


ShowAllData
: True (
FilterMode
), Fulse ( )
: True (
AutoFilterMode
AutoFilter), Fulse ( )

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

(. 8.9). ,
, .
, .
AutoFilter ,
.
111
. 8.8.
, 4500 ,
- 1600 .
L2 L3. L2 min, L3 - st.

. 8.9.
: ,
.
:
nom = Cells(1, 1).CurrentRegion.Rows.Count + 1
Range("A3:I" + Trim(Str(nom))).Select
Sort ,
, Key1.
112
Key1
Range("C4").
Order1. ,
Order1 xlAscending.
,
Key2 Order2 Range("D4") xlAscending .

, Key3 Order3 Range("4")
xlAscending . Header
,
, ,
, xlYes.
, MatchCase
False. Orientation
, ,
xlTopToBottom.
:
Range("A3:I" + Trim(Str(nom))).Select
Selection.Sort Key1:=Range("C4"), Order1:=xlAscending, _
Key2:=Range("D4") , Order2:=xlAscending, Key3:=Range("B4"), _
Order3:=xlAscending, Header:=xlYes, OrderCustom:=1,_
MatchCase:=False, Orientation:=xlTopToBottom
,
, AutoFilter.
"A3:I3", .
AutoFilter.
, .
AutoFilter , ,
field. field 9
( ). ,
, criteria1
criteria2.
- , criteria1
">0", operator xlAnd.
, :
Range("A3:I3").Select
Selection.AutoFilter
Selection.AutoFilter field:=9, Criteria1:=">0", Operator:=xlAnd
113
. 8.10.
:
Range("A3:I3").AutoFilter
:
userform_Initialize() ,
Load
Show.
.
.

CommandButton1_Click().
,
.

.

CommandButton3_Click().
: ,
.

CommandButton4_Click().
,
.

CommandButton5_Click().
.

114

CommandButton2_Click(). .

CommandButton6_Click().
.

Dim nomer, st, min As Integer
Private Sub userform_Initialize()
'
'
st = Range("L3").Value
min = Range("L2").Value
'
With ComboBox1
.List = Array(1, 2, 3, 4, 5)
.ListIndex = -1
End With
With ComboBox2
.List = Array(971,981, 991, 951)
.ListIndex = -1
End With
End Sub
Private Sub CommandButton1_Click()
' ,
' ,
'
' nomer -
nomer = Cells(1, 1).CurrentRegion.Rows.Count + 1
'
Range("A" + Trim(Str(nomer))).Value = nomer - 3
Range("B" + Trim(Str(nomer))).Value = TextBox1.Text
If ComboBox1.ListIndex <> -1 Then
Range("C" + Trim(Str(nomer))).Value = _
ComboBox1.List(ComboBox1.ListIndex, 0)
Else
115
MsgBox " ", 48
Exit Sub
End If
Range("D" + Trim(Str(nomer))).Value = ComboBox2.Text Range("E" +
Trim(Str(nomer))).Value = TextBox4.Text
Range("F" + Trim(Str(nomer))).Value = TextBox5.Text
Range("G" + Trim(Str(nomer))).Value = TextBox6.Text
'
TextBox1.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
ComboBox1.ListIndex = -1
ComboBox2.ListIndex = -1
Range("L5").Value = nomer
'
Cells(nomer, 8).Activate
ActiveCell.FormulaR1C1 = "=Round((RC[-3]+RC[-2])/RC[-1],2)"
'
Cells(nomer, 9).Activate
ActiveCell.FormulaR1C1 = "=IF(RC[-1]<=min,st,0)"
End Sub
Private Sub CommandButton3_Click()
' :
'
Dim nom As Integer
nom = Cells(1, 1).CurrentRegion.Rows.Count
Range("A3:I" + Trim(Str(nom))).Select
Selection.Sort Key1:=Range("C4"), Order1:=xlAscending, _
Key2:=Range("D4") , Order2:=xlAscending, Key3:=Range("B4"), _
Order3:=xlAscending, Header:=xlYes, OrderCustom:=1,_
MatchCase:=False, Orientation:=xlTopToBottom
End Sub
'
Private Sub CommandButton4_Click()
' ,
'
116
Range("A3:I3").Select
Selection.AutoFilter
Selection.AutoFilter field:=9, Criteria1:=">0", Operator:=xlAnd
End Sub
Private Sub CommandButton2_Click()
'
Hide
End Sub
Private Sub CommandButton5_Click()
'
Range("A3:I3").AutoFilter
End Sub
Private Sub CommandButton6_Click()
nomer = Cells(1, 1).CurrentRegion.Rows.Count
Range("A4:I" + Trim(Str(nomer))).Clear
End Sub
:
1.
GoalSeek,
Excel:
a) ax3 + 3bsinx = 0
b) 5ax^(1/3) + 3btg4x = 0
c) ln(ax3) + 3bcos(ex) = 0
2. ,
AutoFill:
x 2 3x 9, 3

a) F ( x) 1 [-5; 5]
, x 3
x 6 3

0,4.
9, 3

b) F ( x) 1 [-10; 10]
, x 3
x 2 1
1.

117
3x 9, 7

c) F ( x) 1 [-7; 17]
, x 7
x 2 4
1.
3. ,
. :
, , , ,
, .
.
13% ,
1%. .
AutoFilter :
a) ;
b) 30000 .
4. .
:
, ,
, .
.

.
75, .
.
AutoFilter :
a) ;
b) 75.

9.
, VBA
MacroRecorder.
MacroRecorder ,
() VBA,
VBA
.
. ,

.

118
,
,

VBA.
,
(,
).
,
.
.

,
VBA.
:
1. ,
.
2. (. 9.1)
( , ,
)
.

(. 9.2).
3. , .
4.
.

. 9.2.

. 9.1.

119
:
1.
, (.
9.3)
, Microsoft Visual
Basic (Module) .
2.
.

. 9.3.
:
,

.
. :
y1 sin x cos x y2 3sin x 0,35 x 1,8 [-5;5]
h=0,5.

:
1. . .xlsm,

Excel .
2. (. 9.4).
3. 1:22.
, .
(.9.1).
.
4. .
.
120
,
,
,
.
5. .
,
,

. ,
,
9.5.
,
9.6.
6.
.

. 9.4.

121
. 9.5.

. 9.6.


.
Excel .
.

. Properties,
.

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

(. 9.4).


.
122
. 9.7. Excel
1.
.
2. Excel

(.9.7).
3.
>> .
.
4.
,

.


,
(. 9.8).
,
,
, .


, , ,
VBA,
(. 9.9).
123
. 9.8. ,

. 9.9. ,

.
VBA.

124
:
1. 100000 5% .
.
N .
.
.
(. 9.10).
4
.
5 .
1:1,
5:5 Ctrl.

. .
Ctrl+q.

. 9.10.
x 2 1
e
2. : y1
10
y2 cos 2 x 2 1 [-3;3] h=0,5.
.
3. (. 9.11).
: , , , ,
. .
1)
.
125
2)
.
3) :
A1F1 , 16,
, ;
211 ;
211 ;
211 ;
D2D11 ;
E2E11 ;
F2F11 .
4) .
5) .

. 9.11.

10.
VBA Excel

.
VBA Excel
:
;
, ,
.

126

. , ,
, (
, , .. )

F
: a .
m
,

. v (t ) ,
s (t ) ,

ds dv F (t , v , s )
v (t ), (1)
dt dt m(t , v , s )

10.1.

[6]:
2000 .
(m=80 ), , ,
.
.
- -.
(. 10.1).
.

,
:

F F F , F k1v k2v 2 , F mg .
.

F , v h ,
, (1)

127
dh
v,
dt
(2)
dv mg k1v k2v 2

dt m

, .
,
k1v
. (2)
:
dh
v,
dt
(3)
dv mg k2v 2

dt m
v , t , h , m , g
, k2
.
: ;
; ,
.
2000, v0=0.
k2 S,
, ,
. k2=0,5cS,, c
. c .

=1,22. =
1,29 / 3
S
. ( ) =1.7 ,
( ) = 0.5 , S 1.7 0.5 0.85 2 .
c 10.1.

128
10.1

= 1,11

= 1,33

= 0,4


= 0,045

,
, , (3),
. , ,
- ,
.
.
,
.
: - -.
-
:
v i 1 vi f ti , vi ,

vi 1 vi

f ti , vi f t i 1 , vi1
.
(4)
2

mg k2vi2
vi 1 vi f ti , vi , f (ti , vi )
m
.
129

( xi1 , yi*1 ) :
mg k2vi2 2

f (ti 1 , v ) mg k2 (vi
*
) /m
i 1
m

(4)
:

vi1 vi

f ti , vi f ti1 , vi1
.
2
,
- :

vi 1 vi [(mg k2 vi 2 ) / m (mg k2 (vi (mg k2 vi 2 ) / m)2 ) / m] (5)
2

-
. :

vi 1 vi (k1 2k2 2k3 k4 ),
6

k1 f (ti , vi ), k2 f (ti , vi k1 ), (6)
2 2

k3 f (ti , vi k2 ), k4 f (ti , vi k3 ).
2 2
vi 1 -
k1, k2 , k3 , k4
.
,
.
vi 1
i 1 - .
- -

VBA Excel.
VBA-.
v vk
grafic.
130
.
2:34, SetSourceData :
ActiveChart.SetSourceData Source:= Range("1!$A$2:$C$34").
, ,
, . , h=3000
dt=1. CommandButton1_Click
2:94. SetSourceData
.
, (General)
r Range:
Public r As Range
, ,
CommandButton1_Click :
Set r = Range("A2:C" + Trim(Str(nom))).
SetSourceData r:
ActiveChart.SetSourceData Source:=r
F5:K19
:
With ActiveSheet.ChartObjects(1)
.Top = Range("F5").Top
.Left = Range("F5").Left
.Width = Range("F5:L19").Width
.Height = Range("F5:L19").Height
End With
Excel.
Excel .
1
.
, . VBA
, :
UserForm1.Show. ,
. 10.1.

2, ,
.

.
.
131
.10.1

: h .
dt,

- (6) - (5).
- (6) rkhv,

, . rkhv
. p1=1,
h, p1=2,
v.
vk - (5).
Excel.
Excel

v vk . 1
.
, .
,


.
.

Const g = 9.8, c = 1.22, ro = 1.29
' v -, vk
-
132
Dim h, v, v0, vk As Double
Dim rost, gr, k2, m, t, dt, a, x As Single
Dim nom As Integer
'
Function fv(hn, vn As Variant) As Double
fv = g - (k2 * vn ^ 2) / m
End Function
'
Function fh(hn, vn As Variant) As Double
fh = vn
End Function
' h v -
,
' p1=1, h
' p1=2, v
Function rkhv(p1, dt, hn, vn As Variant) As Double
Dim D(3) As Double
Dim dD(4) As Double
Dim dsr As Double
Select Case p1
Case 1
dD(1) = fh(hn, vn)
D(1) = hn - dD(1) * dt / 2
dD(2) = fh(D(1), vn)
D(2) = hn - dD(2) * dt / 2
dD(3) = fh(D(2), vn)
D(3) = hn - dD(3) * dt
dD(4) = fh(D(3), vn)
dsr = (1 / 6) * (dD(1) + 2 * dD(2) + 2 * dD(3) + dD(4))
rkhv = hn - dsr * dt
Case 2
dD(1) = fv(hn, vn)
D(1) = vn + dD(1) * dt / 2
dD(2) = fv(hn, D(1))
D(2) = vn + dD(2) * dt / 2
dD(3) = fv(hn, D(2))
133
D(3) = vn + dD(3) * dt
dD(4) = fv(hn, D(3))
dsr = (1 / 6) * (dD(1) + 2 * dD(2) + 2 * dD(3) + dD(4))
rkhv = vn + dsr * dt
End Select
End Function
' Click CommandButton1
' ( )
Private Sub CommandButton1_Click()
m = Val(TextBox1.Text)
rost = Val(TextBox2.Text)
gr = Val(TextBox3.Text)
h = Val(TextBox4.Text)
dt = Val(TextBox5.Text)
k2 = 0.5 * rost * gr * c * ro
a = k2 / m
t = 0 : v0 = 0 : v = v0
vk = 0 : nom = 3
' t, h, v
Range("A3").Value = t
Range("B3").Value = v
Range("C3").Value = vk
Range("D3").Value = h
Do While h > 0 ' h > 0
t = t + dt
h = rkhv(1, dt, h, v) ' h
' -
v = rkhv(2, dt, h, v) ' v
' -
x = (m * g - k2 * v ^ 2) / m
vk = vk + dt / 2 * (x + (m * g - k2 * (v + dt * x) ^ 2) / m)
' vk
nom = nom + 1
'
Range("A" + Trim(Str(nom))).Value = t
Range("B" + Trim(Str(nom))).Value = v
Range("C" + Trim(Str(nom))).Value = vk
134
Range("D" + Trim(Str(nom))).Value = h
Loop
'
Range("J3").Value = nom - 3
Set r = Range("A2:C" + Trim(Str(nom)))
r.Select
'
'
Call grafic
'
Range("J3").Value = nom 3
End Sub
' Click CommandButton2
' ( )
Nom=Cells(3,1).Current Region.Rows.Count
Range(Cells(3,1), Cells(nom,4)). Clear
Range("J2").Clear
Range("J3").Clear
'
If ActiveSheet.ChartObjects.Count > 0 Then
ActiveSheet.ChartObjects.Delete
End If
TextBox4.Text = ""
TextBox5.Text = ""
End Sub
' Click CommandButton3
' ( )
Private Sub CommandButton3_Click()
UserForm1.Hide
End Sub
- -
.10.2.

135
. 10.2.

1
2000 .
, , (
), , ,
.

.
.


() () ()
1 60 1,75 0,45 -
2 50 1,40 0,3 -
3 80 1,65 0,33 -
4 65 1,75 0,55 -
5 80 1,8 0,45 -
6 70 1,65 0,4 -
7 60 1,7 0,45 -
8 64 1,53 0,4 -
9 65 1,8 0,5 -
10 80 1,62 0,37 -
11 50 1,65 0,35 -
136
12 60 1,75 0,45 -
13 67 1,72 0,32 -
14 60 1,65 0,35 -
15 54 1,85 0,5 -

2
-.

1. .
,
R = 3 , m = 90 .
2. .
,
R = 2,5 , m = 80 .
3. .
,
R = 2,7 , m = 80 .
4. .
,
R = 2,5 , m = 70 .
5. .
,
R = 2 , m = 60 .
6. .
,
R = 2,2 , m = 65 .
7. .
,
R = 2,5 , m = 75 .
8. ,
, ,
R = 3,5 , m = 80 .
9. ,
, ,
R = 4,7 , m = 80 .
10. ,
, ,
R = 4,2 , m = 70 .

137
11. ,
, ,
R = 5,3 , m = 65 .
12. ,
, ,
R = 6 , m = 95 .
13. ,
, ,
R = 5,1 , m = 80 .
14. ,
, ,
R = 5 , m = 85 .
15. ,
, ,
R = 3,1 , m = 80 .

10.2. ,
,

v0,
.

.
:

F k1v k2v 2
v x , v y :
dv


k1 k 2 v x2 v y2
x
vx
dt m
(7)
dv y
k1 k 2 v x2 v y2
g vy
dt m
,
(7) :
dx dy
vx , v y, (8)
dt dt

138
:
vx 0 v0 cos , vy 0 v0 sin , x 0 0, y 0 0.
(7) (8) :
v x t , v y t , xt , yt . (7) (8)
.
.
, , (s,
V , t) .. ,
.
(7), (8)

Vx , Vy , X , Y :
dVx
d

a sin Vx b sin Vx2 Vy2 Vx ,
dX Vx
, (9)
d 2 cos
dV y
d

sin a sin Vy b sin Vx2 Vy2 Vy ,
dY 2Vy
,
d sin
a, b -- ,
:
k1v0 k v2
a , b 2 0. (10)
mg mg
:
Vx 0 cos , Vy 0 sin , X 0 0, Y 0 0 (11)
(9)-(11)
a, b, -
VBA Excel,
( a)
( b) .
VBA :

139
1.
a (a<>0, b=0),
2.
b (a=0, b<>0),
3.
a b (a<>0, b<>0),

.10.3
- (.10.3)
:
1. -,
2. , (0,900 ) ,
3. a, b :
1 a(1)<>0, b(1)=0, (
, . .
Enabled False).
2 a(2)=0, b(2)<>0,
3 a(3)<>0, b(3)<>0,
, , .
Excel
. 1
(.10.4) :
t
.
B, C x(1), y(1),
.

140
D, E x(2), y(2),
.
F, G x(3), y(3),
.

.10.4
2 Excel
, (.10.5).
1 2 Excel
.
1
2.
:
1
Private Sub CommandButton1_Click()
Sheets("1").Activate
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
141
Sheets("2").Activate
End Sub

.10.5
2
1.
:
2
Private Sub CommandButton1_Click()
Sheets(" 1").Select
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
Sheets(" 1").Select
End Sub

Const pi = 3.14
Dim s(1 To 3) As String
142
Dim a, b, C, vx, vy, dt, t As Single
Dim y0(0 To 3), y(0 To 3) As Single
Dim pa(1 To 3), pb(1 To 3) As Single
Dim p1, i, j, m, n, L, nom As Integer

' Ff - . (9)
Function Ff(i, y As Variant) As Single
Select Case i
Case 0 ' vx
Ff = -a * Sin(C) * y(0) - b * Sin(C) * (y(0) ^ 2 + y(1) ^ 2) ^ (1 / 2) * y(0)
Case 1 ' vy
Ff = -Sin(C) - a * Sin(C) * y(1) - b * Sin(C) * (y(0) ^ 2 + y(1) ^ 2) ^ (1 / 2) * y(1)
Case 2 ' x
Ff = y(0) / (2 * Cos(C))
Case 3 ' y
Ff = 2 * y(1) / Sin(C)
End Select
End Function ' Ff

' right -
Sub right(y, F As Variant)
For j = 0 To n - 1
F(j) = Ff(j, y)
Next
End Sub ' right

' rk4 - -
Sub rk4(dt, y0, y As Variant)
Dim z(0 To 3), k1(0 To 3), k2(0 To 3), k3(0 To 3), k4(0 To 3) As Single
Call right(y0, k1)
For i = 0 To n - 1
z(i) = y0(i) + k1(i) * dt / 2
Next
Call right(z, k2)
For i = 0 To n - 1
z(i) = y0(i) + k2(i) * dt / 2
Next
143
Call right(z, k3)
For i = 0 To n - 1
z(i) = y0(i) + k3(i) * dt
Next
Call right(z, k4)
For i = o To n - 1
y(i) = y0(i) + (1 / 6) * dt * (k1(i) + 2 * k2(i) + 2 * k3(i) + k4(i))
Next
End Sub ' rk4
' Show
Private Sub userform_initialize()
TextBox4.Text = 0
TextBox5.Text = 0
TextBox4.Enabled = False
TextBox5.Enabled = False
Sheets(" 1").Activate
End Sub
'
Private Sub CommandButton1_Click()
C = Val(TextBox1.Text) * pi / 180
dt = Val(TextBox2.Text)
pa(1) = Val(TextBox3.Text)
pb(1) = Val(TextBox4.Text)
pa(2) = Val(TextBox5.Text)
pb(2) = Val(TextBox6.Text)
pa(3) = Val(TextBox7.Text)
pb(3) = Val(TextBox8.Text)
n = 4: m = 1: x = 0
For L = 1 To 3 '
a = pa(L): b = pb(L)
y0(0) = Cos(C): y0(1) = Sin(C): y0(2) = 0: y0(3) = 0: t = 0
Cells(3, L + m).Activate
ActiveCell.Value = y0(2)
ActiveCell.Offset(0, 1).Value = y0(3)
If L = 1 Then ActiveCell.Offset(0, -1).Value = t
ActiveCell.Offset(1, 0).Activate

144
y(3) = y0(3)
Do While y(3) >= 0 ' y>= 0
t = t + dt
Call rk4(dt, y0, y):
For i = 0 To n - 1: y0(i) = y(i): Next
ActiveCell.Value = y(2)
ActiveCell.Offset(0, 1).Value = y(3)
If L = 1 Then
ActiveCell.Offset(0, -1).Value = t
End If
ActiveCell.Offset(1, 0).Activate
Loop '
' ,
'
s(L) = ActiveCell.Offset(-1, 1).Address(rowabsolute:=False,
columnabsolute:=False)
m=m+1
Next L '
' (1), (1)
Set r = Sheets(" 1").Range("B2:" + s(1))
r.Select
Call Grafic1
Sheets(" 1").Select
' (2), (2)
Set r = Sheets(" 1").Range("D2:" + s(2))
r.Select
Call Grafic2
Sheets(" 1").Select
' (3), (3)
Set r = Sheets(" 1").Range("F2:" + s(3))
r.Select
Call Grafic3
End Sub ' CommandButton1_Click()
'
Private Sub CommandButton3_Click()
Sheets(" 1").Select
nom = Cells(3, 1).CurrentRegion.Rows.Count
145
Range(Cells(3, 1), Cells(nom + 1, 7)).Clear
'
With Worksheets(" 2")
If .ChartObjects.Count > 0 Then
.ChartObjects.Delete
End If
End With
End Sub ' CommandButton3_Click()

'
Private Sub CommandButton2_Click()
UserForm1.Hide
End Sub ' CommandButton2_Click()

:
1. .
.
.
2. .
, .
3. ,
: 1,
0.1; :
1, 0.2 (. 10.6 10.7 ).
4. ,
,
, ,
,
.
5.
.
6. .
,
,
.
7.
,
. :
146
)
2.
,
2, , ,
:

2
A2:F16
ActiveChart.Location Where:=xlLocationAsObject, Name:="2"
With ActiveSheet.ChartObjects(1)
.Top = Range("A2").Top
.Left = Range("A2").Left
.Width = Range("A2:F16").Width
.Height = Range("A2:F16").Height
End With
2
H2:M16
ActiveChart.Location Where:=xlLocationAsObject, Name:="2"
With ActiveSheet.ChartObjects(2)
.Top = Range("H2").Top
.Left = Range("H2").Left
.Width = Range("I2:M16").Width
.Height = Range("I2:M16").Height
End With
2
A18:F33
ActiveChart.Location Where:=xlLocationAsObject, Name:="2"
With ActiveSheet.ChartObjects(3)
.Top = Range("A18").Top
.Left = Range("A18").Left
.Width = Range("A18:F33").Width
.Height = Range("A18:F33").Height
End With

147
. 10.6

. 10.7

) , ,
, .
,
(General) r
Range:
Public r As Range
, ,
CommandButton1_Click:
Set r = Sheets(" 1").Range("B2:" + s(1))
r.Select '

.
SetSourceData r.
ActiveChart.SetSourceData Source:=r
)
,
Characters
148
(, Characters(1, 82)).
(ForeColor.RGB Size).

Grafic1 :
Public r As Range
Sub Grafic1()
' Grafic1 '
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=r '
ActiveChart.ChartStyle = 1 '
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = "
(a<>0, b=0)"
Selection.Format.TextFrame2.TextRange.Characters.Text = "

(a<>0, b=0)"
With Selection.Format.TextFrame2.TextRange.Characters(1,
82).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 82).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 255) '
.Fill.Transparency = 0
.Fill.Solid
.Size = 13 '
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
149
End With
'
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MaximumScale = 1
ActiveChart.Axes(xlCategory).MajorUnit = 0.1
ActiveChart.HasAxis(xlValue) = True
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MaximumScale = 1
ActiveChart.Axes(xlValue).MajorUnit = 0.2
ActiveChart.PlotArea.Select
' 2
A2:F16
ActiveChart.Location Where:=xlLocationAsObject, Name:=" 2"
With ActiveSheet.ChartObjects(1)
.Top = Range("A2").Top
.Left = Range("A2").Left
.Width = Range("A2:F16").Width
.Height = Range("A2:F16").Height
End With
End Sub

1
R=0,1 ,
v0 = 60 / = 45
. , ,
.
(9), .
b (10),
(9)
(11) -. ,
= 7800 /3.
k1v0 6 Rv0 9 v
a 2 0 0,0063,
mg 4 R3 g 2r g

3

150
1
k2v02 2 C R v0 3C v02
2 2

b 0, 034.
mg 4 3
R g 8 R g
3
0.1,
:
1. a,
2. b,
3. a b.
, , ..
Y 0 ( Do While
y(3) >= 0).
:
10.2

, /3 , /3
1,29 7,3103
1,2103 4,5103
2,7103 7,7103
10,5103 8,8103
7,8103 19,3103
2,21103 8,89103

2
1. R=0,3 ,
v0 = 50 / = 10
. , ,
.
2. R=0,5 ,
v0 = 40 / = 35
. ,
, .
3. R=0,3 ,
v0 = 30 / = 15
151
. ,
, .
4. R=0,1 ,
v0 = 40 / = 65
. ,
, .
5. R=0,3 ,
v0 = 10 / = 30
. , ,
.
6. R=0,8 ,
v0 = 70 / = 75
. , ,
.
7. R=0,7 ,
v0 = 60 / = 25
. , ,
.
8. R=0,1 ,
v0 = 80 / = 65
. ,
, .
9. R=0,2 ,
v0 = 40 / = 25
. , ,
.
10. R=0,1 ,
v0 = 100 / = 30
. , ,
.
11. R=0,5 ,
v0 = 70 / = 30
. ,
, .
12. R=0,2 ,
v0 = 80 / = 75
. , ,
.

152
13. R=0,4 ,
v0 = 100 / =
45 . ,
, .
14. R=0,1 ,
v0 = 100 / = 10
. , ,
.
15. R=0,6 ,
v0 = 80 / = 60
. ,
, .

1. .. , .. Microsoft Office Excel 2010:
. .: -, 2011. 528.
2. . . VBA. -, -
-, 2002. 512.
3. . Excel. .: -
, 2004. 544.
4. .. . .
--: 2010. 240 .
5. .., .., ..
: . . . / .
.. . .: . "", 2001. -608.
6. . ., , .
. II:
. , , 2009
48.
7. .., .. Microsoft Word, Excel, Access,
HTML :
: - ,
2011. 84.

153

Оценить