0% found this document useful (0 votes)
38 views

Lab12 - Functions - All

The document provides instructions for a lab assignment on VBA functions and procedures. Students are asked to: 1. Create a "main" sub procedure and leave it empty. 2. Write a "findTaxRate" function that searches a tax table and returns the tax rate. 3. Write a "computeDailySales" sub to calculate sales for a given date using loops and the "findTaxRate" function. 4. Write a function to classify tax rates as low, medium, high or very high using a switch statement. 5. Write a "transposeProductPrice" sub to transpose values between cells using nested for loops.

Uploaded by

Eman Zahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab12 - Functions - All

The document provides instructions for a lab assignment on VBA functions and procedures. Students are asked to: 1. Create a "main" sub procedure and leave it empty. 2. Write a "findTaxRate" function that searches a tax table and returns the tax rate. 3. Write a "computeDailySales" sub to calculate sales for a given date using loops and the "findTaxRate" function. 4. Write a function to classify tax rates as low, medium, high or very high using a switch statement. 5. Write a "transposeProductPrice" sub to transpose values between cells using nested for loops.

Uploaded by

Eman Zahra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DISC 112: Lab 12

(10:00 pm - 12:50 pm)

Date: Monday December 11, 2023

Follow the instructions in the given order to complete the assignment:

1. Download the excel workbook and save it in D Drive. Rename it after Assignment
number and your roll number (e.g. L10-2026-11-001). Save it as macro enabled
workbook! (.xlsm).
2. Create a new worksheet and call it “Group Information” and enter your
information in it similar to the example below:

# Name Roll Number

1 <Your Name> <Your ID>


<Your Partners’
2 <Your Partners’ ID>
Name>
<Your Partners’
3 <Your Partners’ ID>
Name>

The idea of making this group work is that you can learn from each other and teach each
other as you go along with your work. Please help your group partners to understand the
concept/logic/syntax and then be each other’s support. Where you need additional help, you
can always reach out to instructor and TAs too.
Functions and more Total Marks = 30

A manager at CleanHome, a cleaning supplies store brand, is working on sales data for one
of their branches and needs your help with VBA.
1. Write a sub procedure called “main” and leave it empty for now. [1]
a. You can use this sub procedure to call and use the functions and sub
procedures you will be creating below.

2. Write a function called “findTaxRate” [1], that will do the following:


a. Takes one argument [1]
b. Searches the tax-rate table based on the argument and returns the tax rate.
i. Use a do-while loop to traverse the tax-rate table to find the
appropriate rate. [2]
ii. Breaks from the loop when it finds the desired value [2]
iii. Returns the tax rate [1]

3. Write a sub procedure called “computeDailySales” [1], that calculates the sale for a
given date. For this:
a. Asks the user the date for which they want the sale. [1]
b. Uses a for-loop to find the column for that date and breaks the loop when
you do. [2+1]
c. Once done, use a do-until loop (this is not to be nested in the previous one,
comes after) to calculate the sale for that day. [3]
i. When calculating the sales for each item, use the ‘findTaxRate’
function created above to get the tax rate for the item. [2]
ii. Daily sale for each item = price * quantity * (1 – (tax rate/100))
d. Display the total sales for the day in the corresponding cell in row 13 and
make it bold. [1+1]
e. Insert a button with appropriate name and assign it to the macro. [1]

4. Write a function to find out if the tax rate is low, medium, high or very high. To this:
a. The function will take one argument of the item name[1]
b. Get the tax rate value using the findTaxRate function[1]
c. Use a switch statement (select case, see in key below) to define different
cases based on the following: [2]
i. Less than 5 is low
ii. Less than 10 is medium
iii. Less than 15 is high
iv. Above that is very high
d. Use the function directly in excel worksheet in column c and display the
answers. [1]

5. Write a sub procedure called “transposeProductPrice” by


a. Using two for-loops, place values as shown in picture below from F16:P17.
[3]
b. Insert a button and label it ‘Transpose Product-Price info’ and assign it to the
macro. [1]
Syntax key
Function function_name (argument list) As datatype
Statements to execute
Function_name = value
End Function

Switch Statement (Example / Possible Uses)


Select Case varName
Case 1 (will execute if varName = 1)
Statement to execute
Statement to execute
Case 2, 3, 7 (will execute if varName = 2 or 3 or 7)
Statement to execute
Statement to execute
Case 4 To 6 (will execute if varName = 4 or 5 or 6)
Statement to execute
Statement to execute
Case Is > 8 (will execute if varName > 8)
Statement to execute
Statement to execute
Case Else (will execute if no case is true)
Statement to execute
Statement to execute
End Select

Exit statements examples:


Exit for
Exit do
Exit sub

For-Next loop – performs a specified number of iterations.

For counter = start To end


Statement to execute
Statement to execute
Next
Do-While loop – performs iterations only while a conditional test made on each iteration
evaluates as True.

Do While test-expression
Statement to execute
Statement to execute
updater (updates test value to false to stop looping)
Loop

Do-Until loop – performs iterations only while a conditional test made on each
iteration evaluates as False.

Do Until test-expression
Statement to execute
Statement to execute
Updater (updates test value to true to stop looping)
Loop

For-Each loop – performs iterations to traverse each element of an array.


For Each item In collection
Statement to execute
Statement to execute
Statement to execute
Next

You might also like