, , , :
[Private | Public] Type <typename>
< elementname1> As <type>
< elementname2> As <type>
...
End Type
<typename> ;
< elementname> .
Dim Public , . , .
19.14.
Type Student
FullName As String *20
Group As Single
BirthDay As Date
Hight As Integer...
End Type
Dim Student_Card (1 To 1000) As Student
Student Student_Card 1000 , : , , , , .
, , .
:
Const <const> = <>
<const> ;
<> .
:
Public:
Public Const Datel=#12.12.96#
:
Const Date2=# 12.12.97#
, .
VBA , vb. Excel 5.0 , xl.
VBA
, , .
:
<var>= <formula>
<var> ;
<formula> .
"=" , . , , .
19.15.
Place="d:\windows\system"
File="GameTree.Exe"
Student_Card(100).Group= 133
.
VBA , . () (. 19.5).
19.5. VBA
( , ). .
|
|
19.16.
Disct=(S P)/(dt)^2
S , dt 2, ; Disct.
. '. . , . , .
19.17.
Option Explicit | |
Sub _() | Excel 5.0 |
Dim numrows As Integer; numcols As Integer Dim therow As Integer; thecol As Integer numrows = Selection.Rows.Count numcols = Selection.Columns.Count Randomize | |
For therow = 1 To numrows For thecol = 1 To numcols Selection.Cells(therow; thecol).Value = Rnd | Fr-Next, Selection Cells Value "" |
Next thecol | |
Next therow | |
End Sub |
VBA ( ).
VBA.
:
Sub <_> ()
..................................
()
..................................
End Sub
Sub - , , , . Sub End Sub.
VBA.
, , . ; , . (. 19.22) .
. 19.22.
<> , , , . .
4 :
(), , ;
|
|
, ;
(, Excel, , ..);
, , VBA.
( ).
1.
2. () , ( )
3. ( Excel 5.0) ,
. , .
, . :
: Option Private Module
- , , .
, , .
:
Sub Proc_B (Val1 As Single; Val2 As Date;...)
( Windows ) .
, .. , Optional.
19.18.
Sub SquarPr (L As Single; H As Single; S As Single; Optional F) If IsMissing (F) Then F=100 S=L*H End Sub | SquarPr VBA IsMissing F, F , F 100 (F Variant) |
.
1-
Sub Proc_A () Dim Sq as Single SquarPr 12; 23; Sq End Sub | , Sq |
2- ,
Sub Proc_A () Dim Sq as Single Dim LL as Single Dim HH as Single LL=12 HH=23 SquarPr LL HH Sq End Sub | , Sq |
3-
Sub Proc_A () Dim Sq as Single Dim LL as Single Dim HH as Single LL=12 HH=23 SquarPr L:=LL; H:=HH; S:=Sq End Sub | , : |
|
|
, .
:
Static <var> As <type>
, ( ). . . , .
By Val , .
19.19.
.
19.20.
Sub Proc_A () Dim Sq as Single Dim LL as Single Dim HH as Single LL=12 HH=23 SquarPr L:=(LL); H:=(HH); S:=Sq End Sub |
VBA
VBA , . , , , . , VBA (. 19.6) ( ).
VBA , Variant, String. (String), :
;
;
.
VBA , $. , $:
19.6. VBA
ABS() ; RND() ; INT() ; . | ||
LCASE() ; UCASE() ; LEFT() ; RIGHT() ; LEN() . | ||
DATE() ; CDATE() /; I() ; NOW() ( ); DAY() ; MONTH() ; WEEKDAY() ; YEAR() . | ||
IN() ( ); CSTR() ; CVAR() VARIANT; FORMAT() , , , ; STRCOMP() ; VAL() . | ||
FILEATRR() ; FILEDATETIME() () ; FILELEN() ; FREEFILE() . |
|
|
VBA , , , (); .
:
! As <type>. .
19.21.
Sub Proc_A () Dim S as Single Dim LL as Single Dim HH as Single LL=12 HH=23 S=Sq(LL;H)/2+1200 End Sub Function Sq (H As Single; L As Single As Single) Sq=H*L End Function | Sq Sq Sq |
VBA , : , , .
If. : .
:
If <> Then <>
(). (True), , Then; (False), If .
, : ().
.
19.22.
If MaxPrice > 23,000.00$ Then MaxPrice=23,500.00$
If Student_Card(100).FullName Like "*" Then number=Student_Card(100).Group
VBA.
:
Not And Or | "" "" | r Imp Eqv | "" |
. 1 , 1, 2 ..
, Else, , , End If.
19.23. If
If AvgPrice > 12000 Then DiffPrice=Full(234, 45600) | 1 1 , |
ElseIf AvgPrice > 24000 Then DiffPrice=Full(12000, 45000) ElseIf AvgPrice > 36000 Then DiffPrice=Full(24000, 50000) Else DiffPrice=Full(36000, 70000) End If | 2 2 , 3 3 , 1 3 , If |
Select Case. , ():
: Case 45 Case 3, 4, 5, Case 5 12.
, Else, Else , , End Case.
19.24.
Select CDemse | Select, , Demse |
CaseDemse21 | |
Dem=21 | , |
Case Demse 22; 25; 28 | |
Dem=31 | , |
Case Demse 45 To 48 | , Cdemse 45 48 |
Dem=41 | , |
Case Else | |
Dem=51 | |
End Case | Select |
For-Next. , , , () , ..:
|
|
, Next. ( ), ( ) .
, , Next.
Exit For. , Next. For-Next .
19.25.
Sub stickRandom() Dim numrows As Integer; numcols As Integer Dim therow As Integer; thecol As Integer numrows = Selection.Rows.Count numcols = Selection.Columns.Count Debug. Print numrows; numcols Randomize Debug.Print Rnd For therow = 1 To numrows For thecol = 1 To numcols Selection. Cells(therow; thecol).Value = Rnd Next thecol Next therow End Sub | Selection Cells Value "" |
Do-Loop. , Do , Loop ; 4 :
1) True
Do While <>
< >
Exit Do
< >
Loop
, <>. 0. Exit Do Loop .
2) True
Do
< >
Exit Do
< >
Loop While <>
, <>.
3) False
Do Until <>
< >
Exit Do
< >
Loop
Until , , <> . 0.
4) False
Do
< >
Exit Do
< >
Loop Until <>
, , . 1.
19.26.
Option Explicit Sub Do_Rnd() Dim a As Single Dim i As Integer, Randomize Do While True a=Rnd | Do. | |
Debug.Print a If a > 0.99 Then Exit Do Loop End Sub | ||
While-Wend. Do While-Loop. (Exit Do ), .
While <>
< >
Wend
For Each. , .. () . , . .
For Each <> In <>
< >
Exit For
< >
<> Variant, <> . Exit For.
VBA : .
, , ANSI. : .
: .
, . , . FileFree, ( 1-511).
:
Open <__> [For <>] [Access <_>]
[<>] As [#] [Ln=<_>]
<__> - , ;
<> - :
Append
Binary
Input
Output
Random ;
<_> - :
Read
Write
Read Write ;
<> - :
Shared
Lock Read
Lock Write
Lock Read Write ;
- ;
<_> - , 32767
. (512
).
; . Binary, Input, Random , ; Append, Output .
19.27.
Open "FILE" For Input As #1 Open "FILE" For Binary Access Write As #1 Type Record Define userdefined type ID As Integer Name As String * 20 End Type Dim MyRecord As Record Open "FILE" For Random As #1 Len = Len(MyRecord) Open "FILE" For Output Shared As #1 Open "FILE" For Binary Access Read Lock Read As #1 | . Len ( ) , |
:
Close [<__>]
, .
19.28.
Close #1; #2; #4 ' , 1,2,4
Close '
.
1.
:
Print #_; [<__>]
:
[{Spc(n) | Tab[(n)]}] [<>] [charpos]
Spc(n) - ;
Tab(n) - ;
<> - ;
charpos - . ;
( ).
, Print.
19.29.
Open "FILE" For Output As #1 Print # 1; " " Print #1; Print #1; ""; Tab; "" Print #1; ""; " "; "" Print #l;Spc(5); "" Print #l;Tab(10); "" MyBool = False MyDate = #February 12, 1969# MyNull = Null MyError = CVErr(32767) Print #1; MyBool; Print # 1; MyDate; " " Print # 1; MyNull; " " Print #1; My Error; " " Close #1 | 5 10 |
2.
:
Write #_; [<__>]
. Print -:
;
.
( ).
19.30.
3.
:
Put [#_; [#],<>
# - , , . , Get, Put, Seek.
19.31.
Type Record ID As Integer Name As String * 20 End Type Dim MyRecord As Record Open "TESTFILE" For Random As #1 Len = Len(MyRecord) For RecordNumber = 1 To 5 MyRecord.ID = RecordNumber MyRecord.Name = " -" & RecordNumber Put #1, RecordNumber, MyRecord Next RecordNumber Close #1 | , |
1.
:
Input #_; <_>
, .
19.32.
Open "FILE" For Input As #1 Do While Not EOF(l) Input # 1; MyString; MyNumber Debug.Print MyString; MyNumber Loop Close #1 |
Line Input #_; <>
" " (Chr(13)) .
19.33.
Open "TESTFILE" For Input As #1 Do While Not EOF(l) Line Input #1, TextLine Debug.Print TextLine Loop Close #1 | , Debug |
2.
:
Get Put [#_; [#],<>
19.34.