Question: VBA (Visual Basic for Applications) is a programming language integrated into Excel based on Visual Basic 6. It primarily follows the procedural (ie. Grouping code

VBA (Visual Basic for Applications) is a programming language integrated into Excel based on Visual Basic 6. It primarily follows the procedural (ie. Grouping code into subroutines/functions and calling those) and event-driven programming paradigms (responding to important events).

We will focus on some of the basics of the procedural programming features of VBA in this lab. Create a new macro-enabled Workbook, open the VBA editor, and Insert a code Module into the workbook.

Note the following operators available in VBA:

Arithmetic

+ (addition), - (subtraction), * (multiplication), / (division), (, ) (brackets), ^ (exponent), Mod (modulus), \ (division)

String

& (concatenation)

Assignment

=

Recall the demonstration in class where a function that accepted values from three cells was shown. In this lab, you write some short functions based on this example.

The general structure of a function is as follows:

Function () As

= result

End Function

We havent done much of the possibilities for yet, but we do know how to write an expression and return its result.

Some other samples are found below:

Function CircleArea(radius As Double) As Double

CircleArea = ?

End Function

Function ConeVolume(radius As Double, height As Double) As Double

ConeVolume = ?

End Function

In VBA, the following Data Types for parameters and function result data types are valid, after the As keyword:

Double (64-bit floating point number ex. 545.333221)

Integer (16-bit whole number ex. 123 )

Long (32-bit whole number ex. 123123123)

Boolean (TRUE or FALSE)

String (characters ex. Hello World)

Date (we will look at Dates later)

Using the function skeletons above:

  1. Create a function CalcArea that calculates the area of a circle given a radius in cell A1; the result should go in cell B2. (Hint: you need the formula; Google it)
  2. Create a function ConeVol that calculates the volume of a cone given the height and radius in cell A2 and B2; the result should go in cell C (Hint: you need the formula; Google it)
  3. Write a function named SumTwo that adds two numbers from cell A3 and B3; the result should go in cell C
  4. Write a function named PyramidVol that finds the volume of a pyramid given the width in cell A4, the height in cell B4, and the length in cell C (Hint: you need the formula; Google it)
  5. Create a function that adds the text ing to the end of strings found in A5 and B Return the concatenated result in C5. (Hint: you could separate the two strings using an & & between the values from the parameters).
  6. Create a function isEven that takes an Integer from cell A6 and returns True in B6 if the value is odd otherwise it returns False in B

Hints: This one is a little tougher, because we have to make use of a conditional IF statement in the function and use the Mod operator (modulus, remainder of division). The code is provided below:

If Mod 2 = 0 Then

isEven = True

Else

isEven = False

End If

  1. Copy the code below for a function DoesNothing().

Function DoesNothing(message As String) As String

Call ShowMessage(message)

DoesNothing = The sub did all the work

End Function

Write a Sub (subroutine) ShowMessage that displays a parameter message in a MessageBox, but starts with THIS IS AN IMPORTANT MESSAGE: . Use the following code:

Sub ShowMessage(message As String)

MsgBox message

End Sub

Subroutines are, like, functions, another way to group code logically together. However, they do not return anything.

Call DoesNothing(Hello!) in Cell B7.

Call DoesNothing(Whats up?) in Cell B7.

Please show codes about above 7 subquestions!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!