test-question-2020-vba-test
test-question-2020-vba-test
Quantitative Business Analysis with Visual Basic for Applications (City University of
Hong Kong)
Downloaded by gu gu ([email protected])
lOMoARcPSD|18632687
The worksheet “OrderList” contains the purchased records of the customers. Note the following:
¥ The position of “OrderList” in Transactions.xlsx is unknown to you at design time. It is not
necessary the active worksheet when Transactions.xlsx is opened.
¥ You may use the object name Worksheets("OrderList") in your program code.
¥ The column titles “Customer Identification”, “ Product Code”, and “Amount Purchased” are
always in cells A1, B1, and C1, respectively. You may make use of this information in your
program code.
¥ the worksheet will contain at least one row of records, and the first record will always start in
Row 2 of the worksheet. The records are not arranged in any order, and the same customer
identification number may appear more than once in the records. There is no empty row
between any two records. The exact number of records and the content of each record are
unknown to you at design time.
Downloaded by gu gu ([email protected])
lOMoARcPSD|18632687
Tasks:
Write VBA code in nnnnnnnn.xlsm that does the following tasks sequentially:
1. Programmatically set to display the page “Open File” of the MultiPage control mpgReport
before frmPurchases is drawn to the screen at run time.
2. When the CommandButton btnOpenFile is clicked on, a system open file dialog box will appear
(the code is already provided in the related sub). Provide code for the following activities:
a. If the user clicks the “x” button or the “Cancel” button on the displayed open file dialog box:
i. Immediately display the message “Must select a workbook!” in a system message dialog
box.
ii. Then do nothing else except stopping the running sub, but do not end the application.
b. If the user selects a workbook from the displayed open file dialog box:
i. Immediately open the selected workbook. (You may assume the user will choose to open
Transactions.xlsx. No checking is required.)
ii. Then assign the newly opened workbook to an appropriate object variable so that other
subs can access the workbook within the same code window via the object variable.
c. Populate the Listbox control lstRecords on the page “Records” of mpgReport by the product
codes in the column “Product Code” of worksheet “OrderList” in Transactions.xlsx. Each
product code listed in lstRecords must be unique. No blank rows will be contained in
lstRecords when this is done. You may include the object name Worksheets("OrderList") in
your code.
d. Open a new workbook. Then assign the newly opened workbook to an appropriate object
variable so that other subs can access it within the same code window via the object
variable.
e. Activate the workbook nnnnnnnn.xlsm. You are not allowed to activate any other workbooks
from this task onward.
f. Show page “Records” of mpgReport.
3. User may select one or more items in lstRecords. Whenever the user has selected an item in
lstRecords:
a. Determine the number of items currently selected.
b. If the current number of selected items is four:
i. Immediately display this message “Not allowed to select more than three items. Select
again.” in a system message dialogue box.
ii. Deselect the last selected item in lstRecords.
Downloaded by gu gu ([email protected])
lOMoARcPSD|18632687
- - The End - -
Downloaded by gu gu ([email protected])