Введение в программирование на vba - edrive

Click here to load reader

Post on 13-Feb-2017

238 views

Category:

Documents

8 download

Embed Size (px)

TRANSCRIPT

  • . .

    VBA

    - 2003

  • 681.3.016 ... VBA. 64 c. Visual Basic for Applications

    Visual Basic. , Microsoft Office.

    .., 2003.

  • ..............................................................................4 ....................................................................4 Windows......................................................................................6 Windows ...........................................................................9

    WINDOWS .................................................................11 ...................................................................................11 .......................................................................................12 .........................................................................................................14

    .....................................................................20 .........................................................................20 ......................................................................................................22 Visual Basic ...................................................................................23 .....................................................................................................25

    .................................................................30 ...............................................................................................30 ............................................................................32 ..........................................................................................................35 .............................................................................................................36

    ..................................................................38 ....................................................................................38 .................................................................................40 .................................................................................46 ..............................................................................49

    ....................................................................54 .............................................................................................................54 ...................................................................................55 .................................................................................64

  • 4

    . , . , . , , , .

    , , , . , . , , , . , , . , . , . - , , .

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

  • 5

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

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

    . , , . . . , , , , .

    , , .

    , , , , . , ,

  • 6

    , , , .

    , . , . , , , . , .

    . , , . . , , , - ., . , . , .

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

    Windows

    (, , ) , , . Microsoft Windows.

    Windows :

    , .. Windows , , , ,

  • 7

    ., ;

    , ;

    , ;

    , ;

    .

    Windows .

    , , , Windows . , , . , .

    Windows. , , .

    :

    ; ; , , .

    , Windows , , Windows .

    Windows . , , , . Windows . . , , , . .

  • 8

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

    ; ; () ; ; .

    . , , , . Windows . . , , , . -, .

    Windows . 200 Windows. , , , . , .

    , Windows : . , . , Windows , , . . , Windows, .

    Windows . , . , , ( ).

  • 9

    , . - , .

    Windows , . , . , . , Windows . .

    Windows

    . Windows , . Plug & Play. , , , . , Plug & Play, Windows . . .

    Windows , : , CD-ROM , .. , . Windows .

    , , , , ,

  • 10

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

    , ; , ; . , . , , . : , , . , , . . , , .

    , Windows , . . , . , . , , MS DOS. Windows , , . , .

    Windows , (OLE) (DDE). . OLE DDE ,

  • 11

    , . , , (), (), OLE . , , . . , , . , . . , . , (link), (embed). , . , . OLE :

    ; ; , ;

    , , ;

    , , , .

    DDE.

    , , .

    Windows

    Windows . , , .

  • 12

    . , . , .. . , , , , . Windows - , , . , , , , , , .

    . , , . . Windows . , . , . . , . , . , . .

    , Windows. - Windows .

    Windows ; ; ; , ; ; .

    , . ,

  • 13

    . , . , . . Windows ..

    , , . . . :

    , , , ;

    , , , , , , , , , ;

    .

    ., . . . ; , ; (, , , ); ( Web); ( ).

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

    . , . , . 800 , , ,

  • 14

    . , . , Windows Microsoft Office. .

    . . , , , . .. Windows ,

    , , . , , Microsoft Office.

    , . . .

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

  • 15

    . .

    Word Web-. , , , , . . ., .

    . , , . Microsoft Office. , . . Word : . . . , , - . . , , MacroButton ..

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

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

    Excel .

  • 16

    . . , . . , , .

    Access , .

    Access , . .. . , , , . . . , . . , , . , : . , Access .

    , , Access . , . . , .

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

  • 17

    . ..

    , :

    DblClick , Click Click MouseDown MouseUp MouseMove KeyDown KeyUp KeyPress

    Open Load UnLoad Close Resize ..

    Enter GotFocus Exit , ApplyFilter Activate Deactivate Current BeforeInsert AfterInsert Delete

    Del BeforeDel-Confirm ,

    Del

    AfterDel-Confirm

    ,

    Before-Update ,

    AfterUpdate , Change

  • 18

    Updated

    NotInList , Filter LostFocus , Exit Format Print Retreat NoDate Page Timer

    , Windows. , . . . , . . .

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

    , , :

    CopyObject StopAllMacros

  • 19

    Rename Quit DeleteObject ConcelEvent OpenTable ApplyFilter OpenQuery FindNext OpenForm FindRecord - ShowToolBar

    GoToControl

    OpenModule GoToRecord Close GoToPage Save OutputAs Print SendObject - SetWarnings

    Transfer Database

    RepaintObject TransfSpread Sheet

    Require TransferText ShowAllRecords AddMenu SetValue RunMacro

    Beep DoMenuItem SQL RunSQL - SetMenuItem

    SendKeys

    RunCode Haurglass RunApp Echo StopMacro SelectObject OpenReport MsgBox

    . , , . : , , , . Access , , . . , .

  • 20

    , . , . , .

    . : {Backspace}, {Bs}, {BkSp}; {CapsLock}; {Clear}; {Delete}, {Del}; {End}; {Enter}, {~}; {Escape}, {Esc}; {Help}; {Home}; {Insert};{Fn} (n = 1,.. 16); {Down}; {Left}; {PgDn}; {PgUp}; {Tab}; {Up}. +, ^, %. , , {^Down 3}. , . , , .

    SQL SQL 256 . , . . SQL.

    Fortran (FORmula TRANslator), 1957 . , . 1963 . . . 14 Basic. Basic (Beginner's All-purpose Symbolic Instruction Code) . 60- 70- General Electric, HP DEC, - Basic-. Microsoft ., Basic . .

  • 21

    Basic, 1976 . Visual Basic, Visual Basic for Applications (VBA), Microsoft Office.

    , Basic . . , , , . , ; , ..

    (top-down design). , , . Al Khowarizmi, , , 300 400 . .. .

    :

    , ;

    , , ;

    , , , ;

    , ;

    , .

    . , , . , ,

  • 22

    , .

    . , , . , . , , , .

    , . . , . , , .

    ,

    , , . , . . - , , , , ++ . .

    , . . . , . Word Excel :

    ThisDocument ThisWorksheet Normal ;

    ;

    , ;

  • 23

    , , .

    .

    Microsoft Office , .

    Visual Basic

    Visual Basic Microsoft Office. Visual Basic.

    . : . . Visual Basic () , . VBAProject.

    Visual Basic Microsoft Office , , , , , , , , . , ., . , . . , . , . . , , , , .

    . , . . , . , .

    . , . , . .

    . .

  • 24

    . . . ( .) . . ..

    . VBAProject . . . , :

    ; ; Stop; . () , .

    , ( .) . , . ;

    . . ..

    . , Visual Basic . , , , .

    , Access, .

    Access , . . . . . / Visual Basic. , , , . , Access , , . Access ,

  • 25

    ..

    , , . , . , : , , . . , . . . . , .

    Access .

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

    . Visual Basic , . :

    Input # () Input #1, , Line Input # Line Input #1, 1

    SendKeys SendKeys "%{F4}", True

    Width Width #1, 5

  • 26

    + y=3+2 5 - , y=50-30 20 * y=7*3 21 / y=7/3 2.333333 \ y=7\3 2 Mod y=7 Mod 3 1 ^ y=7^3 343

    & y="sc" & "an" scan ! .xls!a . , Application.Open Rset Rset

    = = 5 := := 5 < 123= "" >= "" """b"

    And True And True Or True Or False Not Not True Xor True Xor False Eqv False Eqv False Imp False Imp False

    Write Write #1, "" Print # Print #1, "e" Put Put #1, , Seek

    Seek #1, ChDir ChDir "D:\WIN\S" ChDrive ChDrive "D" Close Close Kill Kill "" MkDir MkDir "D:\WIN\S" Name Name x As y

  • 27

    Open Open "" For Input As #1 RmDir RmDir ""

    Beep Beep Call Call (2, 3) Const Const Dim Dim Function Function () Let Let = "" Private Private Propety Property Let () Public Public

    Randomize Randomize

    ReDim ReDim (5) Rem, Rem Reset Reset Resume Resume Set C Set = Static Static Stop Stop Sub Sub () Type Type End Type

    :

    Input = Input (1, #1) InputBox = InputBox (1, , Def) MsgBox = MsgBox (1, , 2)

    Date = Date Day = Day ()

    Format Msgbox Format (Now, "d mmmm yyyy .") Hour = Hour () Minute = Minute () Month = Month ()

  • 28

    Now = Now Time = Time Timer = Timer Year = Year ()

    Abs = Abs (-50.3) ' 50.3 Int, Fix = Fix (56.5) ' 56 Sgn = Sgn (-5) ' -1 Sqr = Sqr (4) ' 2 Rnd = 6 * Rnd

    Asc = Asc ("A") ' 65 Chr = Chr (65) ' A IsNull = IsNull () Left = Left (, 1) Len = Len () Mid = Mid (, 1, 3) Right = Right (, 1) Space = Space (10) String = String (5, "*") ' *****

    StrComp = StrComp ("", "") 1 Val = Val (" 2 45 7") ' 2457 RGB = RGB (255, 0, 0)

    CurDir = CurDir ("D") ' "D:\Word" Dir = Dir ("C:\WIN\*.ini") EOF Do While Not EOF (1) FileLen = FileLen ("") LOF = LOF (1) Err = Err

    VBA Choose x = Choose (a, "0", "1", "2", "3")

    IIf x = IIf (, "2000", "0")

    IsArray ? x = IsArray () IsDate ? x = IsDate (Forms!!) IsEmpty ? x = IsEmpty (Forms!!) IsError ? x = IsError (Forms!!) IsMissing ? x = IsMissing ()

  • 29

    IsNumeric ? x = IsNumeric (Forms!!) IsObject OLE? x = IsObject (Forms!!) Partition ? MsgBox Partition (a, 0, 100, 50) Switch x = Switch (Forms!!=1,"1") TypeName Variant x = TypeName (a)

    Visual Basic , , .

    :

    vbOKOnly 0 OK vbOKCancel 1 OK

    vbAbortRetryIgnore 2 , , vbYesNoCancel 3 , , . vbYesNo 4 vbRetryCancel 5 vbCritical 16 vbQuestion 32 vbExclamation 48 vbInformation 64 vbDefaultButton1 0 vbDefaultButton2 256 vbDefaultButton3 512 vbSystemModal 4096

    vbOK 1 OK vbCancel 2 vbAbort 3 vbRetry 4 vbIgnore 5 vbYes 6 vbNo 7

    vbUpperCase 1 vbLowerCase 2 vbProperCase 3

  • 30

    msoBarLeft 0 msoBarTop 1 msoBarRight 2 msoBarBottom 3 msoBarFloating 4 msoBarPopup 5

    Visual Basic, Yes, No, On, Off, True, False, Null.

    Visual Basic, :

    Sub ( )

    [ ]

    End Sub , ([ ]) , () . ().

    Sub End Sub . : , , . . . , , .. .

    Visual Basic ( ' ) Rem . , .

    Visual Basic . . , , ,

  • 31

    . : [] Sub ( [] )

    [ ]

    End Sub Const, Dim, Static, Public, Private, , . , , . ( ) , . , Call. :

    [.] [] [Call] [.]

    [ () ] [.]

    := [,] , . .

    , , , . , .

    , , . . Call . , , , . , , :=.

  • 32

    Sub ()

    ' 1 2 End Sub Sub () Call Call End Sub Sub () 1, 2 Call (1, 2) := 2, := 1 End Sub

    . :

    MsgBox [( ][, [,]] [ )]

    . MsgBox Visual Basic, , .

    Sub () MsgBox ", " MsgBox (", ") MsgBox "," & Chr (13) & " " End Sub Sub ()

    MsgBox " " & Now MsgBox " : " & Day (Date) & Space (2) _ & Month (Date) & Space (2) & Year (Date) End Sub Sub ()

    MsgBox " !", vbExclamation, " " End Sub

  • 33

    : , : = . (=). . , , #.

    , , Const. Dim .

    , , , , . . , .

    , Static.

    , Dim Private, . . , , , , Public .

    Visual Basic

    InputBox ( [, [, [, X, Y]]] ) X, Y .

    Sub ()

    1 = 2: 2 = 3 = " " MsgBox & Sqr (1 ^ 2 + 2 ^ 2)

    End Sub Sub ()

    = #2/12/69# ' , ,

  • 34

    MsgBox " " & Day () & Space (2) _ & Month () & Space (2) & Year ()

    End Sub Sub ()

    MsgBox " " & Time = #5:12:00# ' , , MsgBox " " & Hour () & " " & Minute () _

    & " " & Second () & " " End Sub Sub ()

    Const pi = 3.14159 = 2.5 MsgBox 2 * pi *

    End Sub Sub ()

    Const pi = 3.14159 = InputBox (" ") = pi * ^ 2 MsgBox

    End Sub Sub ()

    = MsgBox (" ", vbYesNo + vbQuestion, _ " ?")

    MsgBox " " & End Sub Sub () = InputBox (" ") MsgBox " " & Year (Now) Val () End Sub Sub ()

    = 5: = 10 Call (, )

    :=7, :=3 End Sub Sub (, ) = Chr (13) MsgBox Prompt := " " & & & " " _ & & & : & + End Sub Sub ()

    MsgBox " " & & End Sub Sub () Call

  • 35

    Call Call Call Call End Sub Sub () Static = + 1 MsgBox " " & & "-" End Sub Dim , Sub ()

    = 5: = 10 Call

    End Sub Sub ()

    MsgBox " " & & "," & End Sub

    , , . : [] Function ( [] )

    [ ] [ = ]

    End Function , , , . .

    Call. , , . :

    [] [.] [ () ] , , . . , ,

  • 36

    ByVal, , . ByRef . , Optional Paramarray.

    Sub () MsgBox Sqr ( (2, 3) ) End Sub Function (1, 2)

    = 1 ^ 2 + 2 ^ 2 End Function Const pi = 3.14159 Sub ()

    = InputBox (" ") = () 1 = ' = () 2 = ' MsgBox " " & 1 & " " _

    & & Chr (13) & " " & _ 2 & " " &

    End Sub Function (, Optional = " ") = * 2 ' = 2 * pi * MsgBox End Function Function (ByVal ) = / 2 ' = pi * ^ 2 End Function

    Visual Basic , : , . , . , . .

  • 37

    . :

    Open [For Input] [For Output] [For Random] _ [Access Read,Write] [Read] [Write] [] _ As [#] [Len=]

    :

    Print # [, ] Write # [, ] Line Input #,

    :

    Close [#,] ,

    , . Open Random Len, : Get [#], [], : Put [#], [],

    :

    Name As Kill

    Sub ()

    = InputBox (" ?", " ", _ ".txt", 100, 100)

    MsgBox , vbInformation MsgBox (), vbInformation End Sub Function ()

    Open For Output As 1 Print #1, " " Close 1 Open For Input As 1 Line Input #1,

  • 38

    Close 1 =

    End Function Sub ()

    Open " .txt" For Random As 1 Len = 20 1 = " 1" 2 = " 2" Put 1, 1, 1 Put 1, 2, 2 Close 1 Open ".txt" For Random As 1 Len = 20 Get 1,2, Close 1

    MsgBox , vbInformation End Sub Sub ()

    Name ".txt" As " .dat" MsgBox " " Kill ".dat"

    End Sub

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

    , Algol . , ., . , , .

    , :

  • 39

    , - ;

    , ;

    , ;

    , .

    , ;

    , .

    , , , .. , , . , .

    Ada, Modula 2, Fort, Pascal.

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

    . , . , . , ,

  • 40

    , . :

    , ;

    ;

    ;

    , , , .

    , .

    Visual Basic , , , , .

    :

    If [= True] Then []

    [ElseIf [= True] Then ]

    [Else ]

    End If . If , True, False. (True),

  • 41

    Then , . (False), Else, , . .

    , End If.

    Sub ()

    = InputBox (" ?") If = "" Then

    MsgBox ", " & End If

    End Sub Sub ()

    = InputBox (" ?") If = "" Then MsgBox ", " & _

    Else Beep End Sub Sub ()

    = InputBox(" ") If Asc () > 63 And Asc () < 91 Then

    MsgBox " " ElseIf Asc () > 96 And Asc () < 123 Then

    MsgBox " " Else

    MsgBox " " End If

    End Sub Sub ()

    = Application.InputBox (prompt := " ", Type: = 1) If Then

    Application.OnKey "^{RIGHT}", "" Else

    Application.OnKey "^{RIGHT}", "" Application.OnKey "^{RIGHT}"

    End If End Sub Sub () MsgBox End Sub

    :

  • 42

    Select Case Case 1 To 2

    [] [Case

    ] [Case

    ] [Case Else

    ]

    End Select

    Sub ()

    = InputBox (" ?") = Left (, 3) Select Case Case "", ""

    MsgBox " " Case "", ""

    MsgBox " " Case Else

    MsgBox " , " End Select

    End Sub Sub ()

    = InputBox (" ") Select Case Case 10 To 500

    MsgBox " " Case 501 To 50000

    MsgBox " " Case Is > 50000

    MsgBox " " Case Else

    MsgBox " " End Select

    End Sub

  • 43

    - :

    For = 1 To 2 [Step 3]

    []

    Next [] -, . For . 3 , Step 3 , , 2.

    VBA :

    For Each In

    []

    Next []

    Sub ()

    = "" For = 1 To 26

    = & Space(1) & Chr ( + 64) Next MsgBox

    End Sub Exit

    . GoTo . , . On Error : On Error GoTo ; On Error Resume Next ; On Error GoTo 0 . On Error , Visual Basic , . Err ( ), Number ( ,

  • 44

    ), Source ( ), Description ( ), Raise ( ), Clear ( ).

    Sub ()

    For = 1 To 5 MsgBox If = 3 Then Exit For

    Next End Sub Sub ()

    On Error GoTo Err.Clear = InputBox (" , ") = + 5 MsgBox Exit Sub

    : sg = " " & Err.Number & " " _

    & Err.Source & Chr(13) & Err.Description MsgBox sg, vbExclamation, ""

    End Sub

    , . :

    Do While [=True]

    []

    Loop Do

    []

    Loop While [=True] Do Until [=False]

    []

    Loop Do

  • 45

    []

    Loop Until [=False] Do . Loop, Do . , , , . While Until, Do, Loop. While Until . While Until, . .

    , , WhileWend:

    While [=True]

    [] Wend

    Sub ()

    Do While < 100 = InputBox (" ?") = InputBox (" ?") = + + 1

    Loop MsgBox " . " &

    End Sub Sub ()

    = InputBox (" ?") = () Do While = 13

    = + = ()

    = + 1 If > 13 Then Exit Do

    Loop MsgBox " " & _

    - 1 & " " End Sub

  • 46

    Function () If 12 Then Exit Do

    Loop While = 12 MsgBox " " & _

    - 1 & " " End Sub Sub ()

    Do Beep = MsgBox (" ?", vbYesNoCancel)

    Loop Until = vbNo End Sub Sub ()

    = While Len ()

  • 47

    ,

    , ()

    Long & 4 2147483648 2147483647

    Single ! - 4 3,402823E38 1,401298E45

    Double # - 8 1,79E308 1,79E308

    Currency @ 8 0 922337203685477.58

    Decimal - 14 10E28 10E28

    Date , 8 1/1/100 31/12/9999. Object 4 String $ 1 0 2 . Type

    Variant - 16 + 1 ,

    Variant vbEmpty 0 vbNull 1 vbInteger 2 vbLong 3 vbSingle 4 vbDouble 5 vbCurrency 6 vbDate 7 vbString 8 vbObject 9 OLE Automation vbError 10 vbBoolean 11 vbVariant 12 vbDataObject 13 OLE Automation vbDecimal 14 vbByte 17 vbArray 8192

    ,

    , .

    , Visual Basic Variant. Variant

  • 48

    . Variant , Null Empty. Null , , . . IsNull , Null Variant. : = Null. Empty Variant. IsEmpty, : = Empty. Empty , .

    Object . Object Nothing , ( Empty Variant). : = Nothing.

    Set, . , .

    VarType : 0 vbEmpty, 1 vbNull, 2 vbInteger ..

    Sub ()

    Dim As Boolean, As Boolean = 5 > 7 = 5 < 7 MsgBox Or = "Y" > "Z" = "Y" < "Z" MsgBox And

    End Sub Sub ()

    ' () ' , .

    Dim a As Integer, b As Integer

    a = InputBox (" ") b = InputBox (" ") Do

    If a > b Then a = a - b If b > a Then b = b - a

    Loop Until a = b

  • 49

    MsgBox "= " & a End Sub

    . . . : [] ( 1[To 2][,] ) [As ] , , . Visual Basic . , , .

    , . , , : [] Function ( () [As ],) [As ] :

    MsgBox " = " & (x ()).

    , . , .

    .

    , , (0) , spaceArray (2,3) (2,3)- .

    Sub ()

    Dim , , (2) As Integer = InputBox (" ") (0) = \ 3: (1) = (0): (2) = (0) = Mod 3

  • 50

    If > 0 Then (0) = (0) + 1 If > 1 Then (1) = (1) + 1 MsgBox & "=" & (0) & "+" & (1) & "+" & (2)

    End Sub Dim As Integer Sub ()

    Dim (10) As Integer, As Integer = 0 For = 0 To 10 Step 2

    () = MsgBox ( (), )

    Next End Sub Function (1 () As Integer, 2 As Integer) As Integer

    = + 1 (2) =

    End Function ,

    , ReDim . , , , .

    ReDim . , ReDim Preserve. .

    Sub ()

    Dim () As String, i As Integer, As Integer = InputBox (" ?") ReDim (1 To ) For i = 1 To

    (i) = InputBox (Str(i) & " - ?") Next

    End Sub Sub ()

    Dim () As Integer, As Integer, As String For = 10 To 20

    ReDim () As Integer () =

    Next

    For = 10 To 20 = & () () =

  • 51

    Next MsgBox

    End Sub Sub ()

    Dim () As Integer, As Integer, As String For = 10 To 20

    ReDim Preserve () As Integer () = = & ()

    Next MsgBox

    End Sub Dim () As Integer Sub ()

    : , 21, . , Rnd, Randomize, .

    Static i As Integer ReDim Preserve (i) As Integer Randomize (i) = Rnd * 10 = Info ( (), i) i = i + 1 If 0 Then = MsgBox ( & " ?", vbYesNo) If = vbYes Then Else End ' End Sub Function Info ( () As Integer, As Integer) As Integer Dim As Integer, x As Integer = 0 For x = 0 To = + (x) Next If > 21 Then MsgBox Str () & " - !" Info = 0 Else If = 21 Then Beep MsgBox Str () & " - !" Info = 0 Else Info = End If End If End Function

  • 52

    Visual Basic , . , - . , , , .

    Type End Type , , . . , , , . ., . With End With.

    Type

    As Single As Single As String As Integer

    End Type Type

    As String As String As String As Integer

    End Type Sub ()

    Dim As , As . = 1.85 . = "" MsgBox " " & .

    End Sub Type

    As String As Integer As Integer

    End Type Sub ()

    Dim IBMPC As With IBMPC

  • 53

    . = "Pentium"

    . = 800

    . = 256 End With MsgBox IBMPC. & "-" & IBMPC. _

    & "-" & IBMPC. End Sub

    , . - :

    [] [Property Let] ( [] )

    []

    End Property

    [] [Property Get] ( [] )

    []

    End Property Let , Get .

    , VBA , . . . , Propety Let Propety Get , . : , , Set. , Nothing. Dim r Public Property Get ()

    = r End Property

    Public Property Let ()

    If Not (IsNumeric ()) Then MsgBox " " Exit Property End If

  • 54

    r = End Property Public Function () As Double

    = 3.14159 * ^ 2 End Function Public Sub ()

    Dim As Class1 Set = New Class1 . = InputBox (" ") MsgBox . Set = Nothing

    End Sub

    Word Excel UserForm, (Show) (Hide). , .Userform, (Caption), (Height, Width), (BorderStyle, SpecialEffect), (BackColor, Picture), (Left, Top) (Tag) . , , : (Click), (DblClick), (Activate) .

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

    (Name), Me.

    Sub () Call End Sub

  • 55

    Sub () With UserForm1

    .Caption = " "

    .Width = 270

    .Height = 270

    .Picture = LoadPicture ("c:\user\scan.bmp")

    .BackColor = vbBlue

    .Tag = " " End With

    End Sub Sub UserForm_Click ()

    Beep: Beep: Beep Call

    End Sub Sub ()

    MsgBox " " UserForm1.Caption = " "

    End Sub Sub UserForm_DblClick ()

    Me.Hide UserForm2.Show

    End Sub

    . , , . , .

    Label TextBox

    Frame Command Button

    CheckBox - , -

  • 56

    Option Button

    -

    . , ,

    ListBox .

    ComboBox

    ScrollBar

    Spinner Toggle Button

    Image

    TabStrip

    MultiPage

    RefEdit

    msoControlEdit msoControlButton msoControlDropDown msoControlComboBox msoButtonAutomatic msoButtonIcon msoButtonCaption msoButtonIconCaption

    , . Visual Basic , Name . , . . , . ,

  • 57

    . , . . .

    . , (Click), (Change). , , , , . Visual Basic .frm, .cls, .bas . , . . ().

    .

    (Label). Caption ( ) .

    (TextBox). Value . Enabled. SelText , SelStart SelLengh . Name . . Zorder , , . Passwordhar , , .

    (CommandButton) (, , ..). Default , Enabled . Click . Accelerator , .

    (OptionButtons) GroupName. Value . (CheckBox) Value (), Caption (), WordWrap ( ). Value , , (ToggleButton), (Frame), (ScrollBar) (SpinButton).

    (ListBox) , Value, List, ListIndex, ListCount.

  • 58

    List . MultiSelect : 0 , 1 , 2 . AddItem , RemoveItem . TopIndex , TextColumn , Text. Matchntry , . ControlTipText .

    (ComboBox) Text Value, . ListIndex . Selected . MatchRequired (False) (True) . MatchFound, (True) , .

    (MultiPage) Value, BoundValue ( 0), SelectedItem , Item , Add , Clear, Remove .

    (Image). , ,

    Initialize. Screen ,

    . .

    Sub ()

    UserForm1.Label1.Caption = " " UserForm1.Show

    End Sub Sub ()

    UserForm1.Show Call UserForm1.TextBox1_Change

    End Sub Sub TextBox1_Change ()

    UserForm1. TextBox2 = UserForm1. TextBox1 End Sub Sub ()

    = "" With UserForm1.TextBox1

    .Value =

  • 59

    .Enabled = False End With UserForm1.Show

    End Sub Sub () Dim = Array ("1000", "2000", "3000", "4000") With UserForm1 .ListBox1.List = Array ("", "", "", "") .ListBox1.List (2, 0) = "" .ComboBox1.List = .Show End With End Sub Sub ()

    If MsgBox (" ?", vbYesNo) = vbYes Then Set = Me!TextBox .SetFocus MsgBox (.Text) End If End Sub Sub ()

    Set = Me! .SetFocus

    If .Text = "" Then MsgBox ", " Else Beep End Sub ' , . ' ' Sub _KeyUp (ByVal KeyCode As MSForms.ReturnInteger, _

    ByVal Shift As Integer) If = "" Then = 0

    = * 9 / 5 + 32 = + 273.15

    End Sub Sub _KeyUp (ByVal KeyCode As MSForms.ReturnInteger, _

    ByVal Shift As Integer) If = "" Then = 0

    = ( - 32) * 5 / 9 = ( - 32) * 5 / 9 - 273.15

    End Sub Sub _ KeyUp (ByVal KeyCode As MSForms.ReturnInteger, _

    ByVal Shift As Integer) If = "" Then = 0

    = - 273.15

  • 60

    = ( - 273.15) * 9 / 5 + 32 End Sub Sub ()

    Me!.SetFocus = Me!.Text = Left (, 3) Select Case

    Case "", "" MsgBox " "

    Case "", "" MsgBox " "

    Case Else MsgBox " , "

    End Select End Sub Sub ()

    Me!.SetFocus = Me!.Text Select Case Case 10 To 500

    MsgBox " " Case 501 To 50000

    MsgBox " " Case Is > 50000

    MsgBox " " Case Else

    MsgBox " " End Select

    End Sub Sub _AfterUpdate ()

    Select Case Case "Apple"

    Me! = 1975 Me! = ""

    Case "Motorola" Me! = 1937 Me! = ""

    Case "Intel" Me! = 1967 Me! = ""

    End Select End Sub Sub _Exit ()

    MsgBox " " Me.Undo

    End Sub Sub _Exit ()

    If Len () 4 Then

  • 61

    MsgBox " " Me.Undo

    End If End Sub Sub _Open ()

    = 0 Do

    Me.Controls ().Properties ("BorderColor") = * 1000 + 1 = + 1

    Loop While < Controls.Count End Sub Sub _Open ()

    .Value = Null With

    .Caption = ""

    .Value = False End With

    End Sub Sub UserForm_Activate ()

    With CheckBox1 .Value = Null .Caption = " " .WordWrap = True

    End With With ScrollBar1

    .Min = 20 .Max = 50 .Value = 30

    End With With ToggleButton1

    .Caption = "" .Value = False

    End With With SpinButton1

    .Value = 50 .Max = 100 .Min = 1

    End With End Sub Sub ()

    For i = 0 To 10 UserForm2.ListBox1.AddItem "" & i

    Next End Sub Sub ()

    For i = 0 To (UserForm2.ListBox1.ListCount - 1) If UserForm2.ListBox1.Selected (i) Then _

  • 62

    UserForm2.ListBox2.AddItem _ UserForm2.ListBox1.List (i)

    Next End Sub Sub ()

    For i = (UserForm2.ListBox2.ListCount - 1) To 0 Step -1 UserForm2.ListBox2.RemoveItem i

    Next End Sub ' . . Sub ListBox1_Click ()

    = ListBox1.ListIndex ListBox2.AddItem (ListBox1.List ())

    End Sub Sub ListBox2_Click ()

    = ListBox2.ListIndex ListBox1.AddItem (ListBox2.List ())

    End Sub Sub UserForm_Activate ()

    ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.AddItem "" ListBox1.SetFocus

    End Sub Sub 1_Click ()

    = 1.ListIndex MsgBox (1.ItemData ())

    End Sub Sub _Change ()

    MsgBox " - :-( End Sub ' , . Sub UserForm_Activate ()

    ComboBox1.Text = "" Label1 = "" ComboBox1.SetFocus

    End Sub

  • 63

    Sub CommandButton1_Click () ComboBox1.AddItem ComboBox1.Text ComboBox1.Text = "" ComboBox1.SetFocus

    End Sub Sub CommandButton2_Click ()

    = 0: = 1 Do While 60 Then = 60 End If End Sub Sub _SpinDown () If UserForm1. > 1 Then UserForm1. = UserForm1. - 1 End If End Sub Sub _SpinUp () If UserForm1. < 60 Then UserForm1. = UserForm1. + 1 End If End Sub Sub UserForm_Initialize ()

    Set = UserForm.Controls.Add ("Forms.MultiPage.1") TextBox1 = Format (Now, "d mmmm yy .") CheckBox1 = True

  • 64

    CommandButton1.SetFocus End Sub

    Shape , Picture .

    Sub ()

    With ActiveDocument For = 1 To .Shapes.Count If < .Shapes.Count Then

    .Shapes ().Left = .Shapes ( + 1).Left

    .Shapes ().Top = .Shapes ( + 1).Top Else

    .Shapes ().Left = .Shapes (1).Left

    .Shapes ().Top = .Shapes (1).Top End If Next

    End With End Sub Sub ()

    With UserForm1.Image1 .Picture = LoadPicture ("C:\User\scan.bmp") .PictureAlignment = fmPictureAlignmentTopLeft .PictureSizeMode = fmPictureSizeModeStretch .Visible = True If .Top > 0 And .Left > 0 Then

    .Move .Left - 5, .Top - 6 Else

    .Visible = False End If

    End With End Sub ' Sub _Open ()

    = 1 For Each In Controls

    .Properties ("BorderColor") = = + 2000

    Next End Sub