Use of VBA for Excel XP 23/06/2003 In this session, you will learn how to: 1. Create a lookup table for grading students’ scores 2. Create buttons to control the contents in a cell 3. Create a user interface by making use of forms to input and retrieve information A. Basics ingredients of EXCEL spreadsheet Cell and Range Reference The address or reference of a cell comes in four types: Relative: The reference is fully relative. When the formula is copied, the cell reference adjusts to its new location. Example: A1. Absolute: The reference is fully absolute. When the formula is copied, the cell reference does not change. Example:$A$1. Row Absolute: The reference is partially absolute. When the formula is copied, the column part adjusts, but the row part does not change. Example: A$1. Column Absolute: The reference is partially absolute. When the formula is copied, the row part adjusts, but the column part does not change. Example: $A1. Referencing other sheets or workbooks References to cells and ranges need not be in the same sheet as the formula. To refer to a cell in a different worksheet, precede the cell reference with the sheet name followed by an exclamation point. For example: =Sheet2!A1 + 1 You can also create link formulas that refer to a cell in a different workbook. For examples: =[examination.xls]Sheet1A1 + 1 =’[Examination for 2003]Sheet1’!A1 + 1 (if workbook name in the reference includes one or more space) =’C:\temp\excel\[Examination for 2003]Sheet1’!A1 + 1 (if linked workbook is closed) B. Creating a LookUp table for grading Exercise 1: Download the file marking1.xls On the Qth column, calculate the grand total of each student’s score Exercise 2: Download the file marking2.xls On the Rth column, assign a grade for each student according to the table shown on Sheet2. Hints: Using the IF Function 1. Basic Structure: IF(condition, action, else) 2. Nested IF Structure: IF(condition1, action1, IF(condition2, action2, IF(condition3, action3,….) =IF(Q5>Sheet2!$B$2, Sheet2!$C$1, IF(Q5>Sheet2!$B$3, Sheet2!$C$2, IF(Q5>Sheet2!$B$4, Sheet2!$C$3, IF(Q5>Sheet2!$B$5, Sheet2!$C$4, IF(Q5>Sheet2!$B$6, Sheet2!$C$5, IF(Q5>Sheet2!$B$7, Sheet2!$C$6, IF(Q5>Sheet2!$B$8, Sheet2!$C$7, IF(Q5>Sheet2!$B$9, Sheet2!$C$8, Sheet2!$C$9)))))))) Question: Why do we need to use a relative reference for Q5 and absolute references for Sheet2!$B$ and Sheet2!$C$? C. Creating buttons to control the Content of a Cell Exercise 3 1. 2. 3. 4. Open the file quad-0.xls Assign the value of B2 to B12 Assign the value of B4 to C13 Assign the value of B7 to D12 5. Type in a formula in C9 =$B$4^2-4*($B$2)*($B$7) 14. Type in a formula in B17 = (- $B$4 + SQRT( $C$9))/(2*$B$2) Type in a formula in D17 = (- $B$4 - SQRT( $C$9))/(2*$B$2) Type in a formula in B21 = $B$12*A21^2+$D$12*A21+$C$13 To add a scrollbar: click View > ToolBox, then select the scrollbar and drag it to the appropriate location of the worksheet. Highlight and right click the scrollbar and change its properties. Set the maximum value to 10 and the minimum value to 0 Set the linked cell as $C$2. Then type in a formula in cell B2 =$C$2 – 5 Repeat Steps 9 – 13 for the cell B4 and B7 15. Plot the graph for the x-y values shown in the range A21:B41 6. 7. 8. 9. 10. 11. 12. 13. D. Creating an user interface for data input and retrieval Exercise 4 1. Open the file sandy-wordbank0.xls 2. Open the toolbox by select View > ToolBox 3. On the ToolBox, select the CommandButton and draw it on the worksheet 4. To change the Button’s properties: highlight and right-click it. 5. 6. 7. 8. 9. Change the caption name to be “Input” Similarly, Draw another Button and name it as “Look Up” To create a form (with name “UserForm1”) for the worksheet: Choose Tools > Macros > Visual Basic Editor On the menu bar, choose Insert > Form On the form editor panel, draw two text boxes with two labels and two buttons (‘OK’ button and ‘Cancel’ button) as shown below: 10. Similarly, create another form with name “UserForm2” as shown below: 11. Open the excel file as shown: 12. Double-click the Input Button. 13. Type in the code for the “Input” Button: Private Sub CommandButton1_Click() UserForm1.Show End Sub 14. Double-click the “Look Up” Button. Private Sub CommandButton2_Click() UserForm2.Show End Sub 15. Open the Visual Basic editor by choosing Tools > Macros > Visual Basic Editor 16. Open the Form: UserForm1 17. We are going to type in the codes for the “Cancel” button and the “OK” button respectively: Private Sub CancelButton_Click() Unload Me End Sub ----------------------------------------------------------------------Private Sub OKButton_Click() Dim NextRow As Long ' Make sure Sheet1 is active Sheets("Sheet1").Activate ' Determine the next empty row NextRow = Application.WorksheetFunction.CountA(Range("C:C")) + 1 Transfer the name ' Cells(NextRow, 3) = TextBox1.Text Cells(NextRow, 5) = TextBox2.Text ' Clear the controls for the next entry TextBox1.Text = "" TextBox2.Text = "" TextBox1.SetFocus End Sub 18. Similarly, open the Form: UserForm2 19. We are going to type in the codes for the “Cancel” button and the “OK” button respectively: Private Sub CancelButton_Click() Unload Me End Sub -------------------------------------------------------------------------------Private Sub OKButton_Click() Dim NextRow As Long ' Make sure Sheet1 is active Sheets("Sheet1").Activate ' Determine the next empty row NextRow = Application.WorksheetFunction.CountA(Range("C:C")) Transfer the name Cells(NextRow, 3) = TextBox1.Text ' ' ' Cells(NextRow, 5) = TextBox2.Text For i = 1 To NextRow If Cells(i, 3) = TextBox1.Text Then TextBox2.Text = Cells(i, 5) End If Next ' ' ' Clear the controls for the next entry TextBox1.Text = "" TextBox2.Text = "" TextBox1.SetFocus End Sub 20. To improve word searching, you may examine the file sandy-wordbank2.xls in which a function called “ISLIKE” is employed to compare two string variables.