VBA/Excel/Access/Word/Language Basics/Call

Материал из VB Эксперт
Перейти к: навигация, поиск

Use Call to invoke another sub module

 
Sub Func1()
   Dim intTemp As Integer
   intTemp = 10
   Debug.Print "We Are Now In Func1()"
   Debug.Print intTemp
   Call Func2
End Sub
Sub Func2()
   Dim strName As String
   strName = "Bill Gates"
   Debug.Print "We Are Now In Func2()"
   Debug.Print strName
   Call Func3
End Sub
Sub Func3()
   Debug.Print "We Are Now In Func3()"
   msgBox "Hi There From The Func3() Sub Procedure"
End Sub



Using Parameter Arrays

 
Sub GetAverageSalary(strDepartment As String, _
    ParamArray currSalaries() As Variant)
    Dim sngTotalSalary As Single
    Dim sngAverageSalary As Single
    Dim intCounter As Integer
    For intCounter = 0 To UBound(currSalaries())
        sngTotalSalary = sngTotalSalary + currSalaries(intCounter)
    Next intCounter
    sngAverageSalary = sngTotalSalary / (UBound(currSalaries()) + 1)
    msgBox strDepartment & " has an average salary of " & _
        sngAverageSalary
End Sub
Sub ParaArray()
    Call GetAverageSalary("Accounting", 6, 2, 3, 2, 8)
End Sub



Using Procedures and Subprocedures

 
Sub AboutUserMaster()
    Dim first As String, last As String, full As String
    Call GetUserName(full)
    first = GetFirst(full)
    last = GetLast(full)
    Call DisplayLastFirst(first, last)
End Sub
Sub GetUserName(fullName As String)
    fullName = InputBox("Enter first and last name:")
End Sub
Function GetFirst(fullName As String)
    Dim space As Integer
    space = InStr(fullName, " ")
    GetFirst = Left(fullName, space - 1)
End Function
Function GetLast(fullName As String)
    Dim space As Integer
    space = InStr(fullName, " ")
    GetLast = Right(fullName, Len(fullName) - space)
End Function
Sub DisplayLastFirst(firstName As String, lastName As String)
    MsgBox lastName & ", " & firstName
End Sub