. , .
, Private. , . . :
Sub ()
Var1 = GetRand
Var1
End Sub
Private Sub (ByVal Var1)
MsgBox Var1 * GetRand
End Sub
Private Function GetRand()
GetRand = Int(100 * Rnd())
End Function
Static, . :
Sub ()
End Sub
Static Sub ()
Dim Var1 As String
If Var1 = "" Then
Var1 = "Var1 "
Else
Var1 = " Var1 ."
End If
MsgBox Var1
End Sub
6. : If-Then-Else, Select Case, For-Next, While-Wend, Do-Loop, For-Each-Next
, . :
(GoTo, If-Then-Else, Select Case)
(For-Next, While-Wend, Do-Loop, For-Each-Next)
, 6 .xls .
1. If-Then-Else, , , :
Sub IfThenElse()
Dim Num1 As Integer
Num1 = GetRandomNumber
If Num1 = 7 Then
MsgBox "! ! " & _
Num1 & "."
Else
MsgBox ", . " & _
Num1 & "."
End If
End Sub
Function GetRandomNumber()
GetRandomNumber = Int(10 * Rnd())
End Function
2. :
Sub IfThenElseIf()
Dim Password As String
Password = GetPassword
If Password = "level1" Then
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Visible = True
Sheet.Unprotect
Next
MsgBox " ."
Elself Password = "level2" Then
ActiveWorkbook.Worksheets(l).Visible = True
ActiveWorkbook.Worksheets(1).Unprotect
MsgBox " ."
Elself Password = "level3" Then
ActiveWorkbook.Worksheets(l).Visible = True
|
|
MsgBox " ."
Else
MsgBox " . , !"
End If
End Sub
Function GetPassword()
GetPassword = LCase(InputBox(" :", "Password"))
End Function
3. , Select Case:
Sub SelectCase()
Dim Password As String
Dim Sheet As Object
Password = GetPassword
Select Case Password
Case "level1"
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Visible = True
Sheet.Unprotect
Next
MsgBox " ."
Case "Ievel2"
ActiveWorkbook.Worksheets(1).Visible = True
ActiveWorkbook.Worksheets(1).Unprotect
MsgBox " ."
Case "Ievel3"
ActiveWorkbook.Worksheets(l).Visible = True
MsgBox " ."
Case Else
MsgBox " . ."
End Select
End Sub
4. For-Next 0 n:
Sub ForNext()
Dim As Integer
Dim As Integer
= Int(InputBox(" :"))
= 0
For i = 0 To Step 2
= + i
Next
MsgBox " " & & " = " _
&
End Sub
5. While-Wend.
Sub WhileWend()
Dim LotteryEntry As Integer
LotteryEntry = 0
While LotteryEntry <> 7
LotteryEntry = Int(10 * Rnd())
Beep
Wend
MsgBox " " & LotteryEntry & ". !!"
End Sub
6. Do - While-Loop.
Sub DoWhileLoop()
Dim LotteryEntry As Integer
LotteryEntry = 0
Do While LotteryEntry <> 7
LotteryEntry = Int(10 * Rnd())
Beep
Loop
MsgBox " " & LotteryEntry & ". !!"
End Sub
7. Do - Until-Loop.
Sub DoUntilLoop()
Dim LotteryEntry
LotteryEntry = 0
Do Until LotteryEntry = 7
LotteryEntry = Int(10 * Rnd())
Beep
Loop
MsgBox " " & LotteryEntry & ". !!"
End Sub
8. For-Each-Next .
Option Base 1
Sub ForEachNext()
Dim (5) As String
Dim As Variant
(1) = ""
(2) = ""
(3) = ""
(4) = ""
(5) = ""
For Each In
MsgBox
Next
End Sub
9. For-Each-Next .
9.1. , . :
Sub ForEachNextWorksheet()
Dim SheetVar As Worksheet
For Each SheetVar In ActiveWorkbook.Worksheets
If SheetVar.Name = ". " Then
MsgBox " ! "
Exit Sub
End If
Next
|
|
MsgBox " . ! "
End Sub
9.2. ForEachNextWorkbook , , .
Sub ForEachNextWorkbook()
Dim Book As Workbook
For Each Book In Workbooks
If Book.Name <> ThisWorkbook.Name Then
Book.Close
End If
Next
End Sub
With
, . With , . With:
Sub __With()
With ActiveWorkbook.Worksheets(1)
.Select
.Unprotect
With. Range("A1")
MsgBox.Value
.Value = 200
.RowHeight =60
.ColumnWidth = 20
.Font.Size = 20
.Interior.ColorIndex = 3
End With
End With
End Sub
:
1. 6 .
2. With, __With. .
VBA
, VBA , , .
7 .
1. Sin:
Sub Sin()
Dim Num1
Dim Num2
Num1 =
Num2 = Sin(Num1)
MsgBox "Sin (" & Num1 & ") = " & Num2, " "
End Sub
Function ()
= InputBox(" :", " ")
End Function
. , , , Cancel , .
( ) , IsEmpty, , :
If IsEmpty(Num1) = False Then Exit Sub [10]
IsNumeric. Sin :
Sub Sin()
Dim Num1
Dim Num2
Num1 =
If IsEmpty(Num1) = False Then Exit Sub
If IsNumeric(Num1) = False Then
MsgBox " !", _
vbExclamation, " "
Exit Sub
End If
Num2 = Sin(Num1)
MsgBox "Sin (" & Num1 & ") = " & Num2
End Sub
:
1. , (.. : , ).