Exercise 6 Download the files from http://www.peter-lo.com/teaching/x4-xt-cdp-0071-a/source6.zip, you will use these files to finish the following exercises. 1. This exercise will guide you how to create a simple game using VBA, please ensure your security level set to Medium or Low before start this exercise. A) Open a new workbook, enter I have a number between 1-9, please guess in cell A1. Then create a Textbox from the Active X Controls in the Developer tab. B) Create a Command Button from the Active X Controls in the Developer tab X4-XT-CDP-0071-A @ Peter Lo 2016 1
C) Right click the command button, and select Properties. D) Change the Caption for the command button to New Game. X4-XT-CDP-0071-A @ Peter Lo 2016 2
E) Double click the command button New Game to open the Visual Basic Editor, and then enter the following code. Private Sub CommandButton1_Click() ' Initialize the random-number generator. Randomize End Sub ' Generate random value between 1 and 9. RandomNumber = Int((9 * Rnd()) + 1) F) Select File Close and Return to Microsoft Excel in the Visual Basic Editor. G) Create another Command Button from the Active X Controls in the Developer tab X4-XT-CDP-0071-A @ Peter Lo 2016 3
H) Right click the Button, and select Properties. I) Change the Caption to Guess. X4-XT-CDP-0071-A @ Peter Lo 2016 4
J) Double click the command button Guess to open the Visual Basic Editor, and then enter the following code. Private Sub CommandButton2_Click() If TextBox1.Text > RandomNumber Then MsgBox ("Too Big!") ElseIf TextBox1.Text < RandomNumber Then MsgBox ("Too Small!") ElseIf TextBox1.Text = RandomNumber Then MsgBox ("Correct!") End If End Sub K) Add the variable declaration statement on the top of the program. Dim RandomNumber As Integer Private Sub CommandButton1_Click() If TextBox1.Text > RandomNumber Then MsgBox ("Too Big!") ElseIf TextBox1.Text < RandomNumber Then MsgBox ("Too Small!") ElseIf TextBox1.Text = RandomNumber Then MsgBox ("Correct!") End If End Sub X4-XT-CDP-0071-A @ Peter Lo 2016 5
Private Sub CommandButton2_Click() ' Initialize the random-number generator. Randomize End Sub ' Generate random value between 1 and 9. RandomNumber = Int((9 * Rnd()) + 1) L) Select File Close and Return to Microsoft Excel in the Visual Basic Editor. X4-XT-CDP-0071-A @ Peter Lo 2016 6
M) Save the file as Guess Number.xlsm and close it. Pease note that you must select Excel Macro-Enables Workbook (*.xlsm), or you will lose all Macro and VBA content N) Open the file Guess Number.xlsm and press the [New Game] button to start a new game. Now you can input your answer and press [Guess] to check your luck. X4-XT-CDP-0071-A @ Peter Lo 2016 7
2. This exercise will test your knowledge learnt in this Advanced Excel course. You are required apply all your skill (such as formula, conditional formatting, data validation, pivot table and charting) to complete this exercise. A) Open the Excel file Question 6-2.xlsx, and apply the following rules: i. Calculate the Duration for each task: End Date Start Date. ii. Create a drop down list for user to select the Priority: High, Medium, and Low. iii. Create a drop down list for the Responsible based on the worksheet Staff. iv. By using VLOOKUP, obtain the Department for each Responsible v. Create a drop down list for the user to select the Status: Open, In Progress, and Closed. B) Fill the data in the column Priority, Responsible and Status, the content in the column Duration and Department should be auto determined. C) By using the conditional formatting, apply the following rules to the worksheet i. Set the row to Green color if the Status is Closed. ii. Set the row to Red color if the Status is not Closed and End Date is overdue. X4-XT-CDP-0071-A @ Peter Lo 2016 8
D) Create a Gantt chart for the project schedule as follow: E) Create a Pivot Table and Pivot Chart for the overall summary X4-XT-CDP-0071-A @ Peter Lo 2016 9