《excel_vba_编程教程(完整版)》

下载本书

添加书签

excel_vba_编程教程(完整版)- 第74节


按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
     里的随机数语句产生的数字设置的。VBA每次看到对对象emp的引用时,它就会调用位于类模 
     块里的适当Property Let过程。  
     本章的最后部分示范如何一步一步地跟踪过程的运行,准确地查看什么时候运行属性过程。 
     设置完对象的属性值后,VBA将员工数据转移到工作表里。With emp结构里面的最后一条语句 
     将用户定义的对象emp添加到一个叫做CEmployee的自定义集合。  
     接着,VB将窗体文字框里的输入清除并且激活开始在UserForm_Initialize过程里关闭的命令 
     按钮。注意,本代码块的第一条指令:cmdEmployeeList。Value = True,该语句导致自动执 
     行cmdEmployeeList_Click过程,该过程附加于按钮Update List(顺便说一下,这是唯一用 
     户从未见到的控件)。该过程的代码如下所示。  
9。  输入cmdEmployeeList_Click过程,如下所示:  
     Private Sub cmdEmployeeList_Click()   
         lboxPeople。Clear   
         For Each emp In CEmployees   
              lboxPeople。AddItem emp。Id & 〃; 〃 & _   
                emp。LastName & 〃; 〃 & emp。FirstName & 〃; 〃 & _   
                Format(emp。Salary; 〃0。00〃)  
         Next emp   
     End Sub   
     cmdEmployeeList_Click过程附加在命令按钮Update List之上,该按钮由cmdSave_Click过程 
     控制,并且导致新的员工数据添加到列表框控件里。cmdEmployeeList_Click过程以清除列表 
     框的内容开始,然后用自定义集合CEmployees的成员来填充列表框。  

                                          225 

… 页面 242…

                                                                              
图11…4 列表框控件显示员工数据,正如在自定义集合输入的一样  
10。 输入下述过程cmdClose_Click:  
    Private Sub cmdClose_Click()   
          Unload Me   
    End Sub   
    cmdClose_Click过程让你将用户窗体从屏幕上清除,并结束使用员工的自定义集合。当你再 
    次运行窗体,你输入的员工将会成为新集合CEmployees的成员。  
11。 输入下述过程cmdDelete_Click:  
    Private Sub cmdDelete_Click()   
         ' make sure that an employee is highlighted in the  
         ' list control   
         If lboxPeople。ListIndex 》 …1 Then   
             MsgBox 〃Selected item number: 〃 & lboxPeople。ListIndex   
              extract = CEmployees。Item(lboxPeople。ListIndex + 1)。Id   
             MsgBox extract   
             Call FindId   
             MsgBox empLoc   
             Range(〃A〃 & empLoc)。Delete (3)   
             MsgBox 〃There are 〃 & CEmployeesunt & _   
               〃 items in the CEmployees collection。 〃   
             CEmployees。Remove lboxPeople。ListIndex + 1   
             MsgBox 〃The CEmployees collection has now 〃 & _   
               CEmployeesunt & 〃 items。〃   
              cmdEmployeeList。Value = True   
              If CEmployeesunt = 0 Then   
                   Call UserForm_Initialize   
             End If   
             Else   
             MsgBox 〃Click the item you want to remove。〃   
         End If   
    End Sub   
    过程cmdDelete_Click让你从自定义集合CEmployees里面清除员工。要删除员工的话,你必须 
    点击列表框的适当成员。当你点击一个列表成员(和按钮Delete Employee), 
    cmdEmployeeList_Click过程就自动执行。该过程确保更新列表框的内容。员工将同时从集合 
    和列表框里删除。如果列表框里面只有一个员工,那么VBA将调用过程UserForm_Initialize 
    在清除最后一个员工后将某些控件失活。cmdDelete_Click过程里有好几个MsgBox语句,让你 
    在做决定的时候检查列表框控件的内容。除了从自定义集合里删除员工之外,过程 

                                         226 

… 页面 243…

    cmdDelete_Click也必须从工作表的相应行删除员工信息。使用函数FindId可以很方便地在工 
    作表里找到员工数据(该过程的代码见下面的第12步)。该函数将要删除的行号返回到过程 
    cmdDelete_Click。  
12。 输入下述函数过程:  
    Private Function FindId()   
         Set ws = ActiveWorkbook。Sheets(〃Salaries〃)   
         startRow = ActiveSheet。UsedRange。Rowsunt + _   
               1 … CEmployeesunt   
         endRow = ActiveSheet。UsedRange。Rowsunt   
         For Each cell In ws。Range(Cells(startRow; 1); _   
              Cells(endRow; 1))   
              If cell。Value = extract Then   
                empLoc = cell。Row   
                FindId = empLoc   
                Exit Function   
              End If   
         Next   
    End Function   
     函数过程FindId将含有窗体列表框中当前选择的员工数据的行号返回到主调过程。工作表中 
     的数据搜索基于变量extract的内容,它存储员工的唯一号码。员工ID的搜索被限制与工作表 
     的第一列,并从集合的第一个成员放置的位置开始搜索,这样使搜索更快一些。你不要在工 
    作表整个使用的区域里搜索。想想如果你不只一次地使用了窗体,但是,自定义集合的内容 
    不会包含前面输入的员工。  
13。 输入下述过程cmdUpdate_Click:  
    Private Sub cmdUpdate_Click()   
          If optHighlighted = False And optAll = False Then   
              MsgBox 〃Click the 'Highlighted Employee' or 〃 _   
               & 〃 'All Employees' option button。〃   
              Exit Sub   
          End If   
          If Not IsNumeric(txtRaise) Then   
               MsgBox 〃This field requires a number。〃   
               txtRaise。SetFocus   
               Exit Sub   
          End If   
          If optHighlighted = True And _   
               lboxPeople。ListIndex = …1 Then   
               MsgBox 〃Click the name of the employee。〃   
               Exit Sub   
          End If   
          If lboxPeople。ListIndex  …1 And _   
               optHighlighted = True And _   
               optAmount。Value = True And _   
               txtRaise。Value  〃〃 Then   
               extract = CEmployees。Item(lboxPeople。ListIndex + 1)。Id   
               MsgBox extract   
               Call FindId   
               MsgBox empLoc   
               choice = 2   
               amount = txtRaise   

                                         227 

… 页面 244…

           CEmployees。Item(lboxPeople。ListIndex + 1)。Salary = _   
                 emp。CalcNewSalary(choice; _   
                 CEmployees。Item(lboxPeople。ListIndex + 1)。Salary; amount)   
           Range(〃D〃 & empLoc)。Formula = CEmployees。 _   
                 Item(lboxPeople。ListIndex + 1)。Salary   
           cmdEmployeeList。Value = True   
      ElseIf lboxPeople。ListIndex  …1 And _   
           optHighlighted = True And _   
           optPercent。Value = True And _   
           txtRaise。Value  〃〃 Then   
           extract = CEmployees。Item(lboxPeople。ListIndex + 1)。Id   
           MsgBox extract   
           Call FindId   
           MsgBox empLoc   
           CEmployees。Item(lboxPeople。ListIndex + 1)。Salary = _   
                 CEmployees。Item(lboxPeople。ListIndex + 1)。Salary + _   
                 (CEmployees。Item(lboxPeople。ListIndex + 1)。Salary * _   
                 txtRaise / 100)   
           Range(〃D〃 & empLoc)。Formula = CEmployees。 _   
                 Item(lboxPeople。ListIndex + 1)。Salary   
           cmdEmployeeList。Value = True   
      ElseIf optAll = True And _   
           optPercent。Value = True And _   
           txtRaise。Value  〃〃 Then   
           For Each emp In CEmployees   
                 emp。Salary = emp。Salary + ((emp。Salary * txtRaise) _   
                      / 100)   
                 extract = emp。Id   
                 MsgBox extract   
                 Call FindId   
                 MsgBox empLoc   
                 Range(〃D〃 & empLoc)。Formula = emp。Salary   
           Next   
           emp cmdEmployeeList。Value = True   
      ElseIf optAll = True And _   
           optAmount。Value = True And _   
           txtRaise。Value  〃〃 Then   
           For Each emp In CEmployees   
                 emp。Salary = emp。Salary + txtRaise   
                 extract = emp。Id   
                 MsgBox extract   
                 Call FindId   
                 MsgBox empLoc   
                 Range(〃D〃 & empLoc)。Formula = emp。Salary   
           Next emp   
           cmdEmployeeList。Value = True   
      Else   
           MsgBox 〃Enter data or select an option。〃   
      End If   
End Sub   

                                      228 

… 页面 245…

有了过程cmdUpdate_Click,你就可以使用确定的百分比或者特定量来修改薪水。可以为选定的员 
工或者列表框和集合里面列出的所有员工更新薪水。cmdUpdate_Click过程核实用户是否选择了适 
当的选项按钮,然后在文字框里输入增加
小提示:按 回车 [Enter] 键 返回书目,按 ← 键 返回上一页, 按 → 键 进入下一页。 赞一下 添加书签加入书架