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
End Function
We havent done much of the possibilities for
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:
- 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)
- 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)
- Write a function named SumTwo that adds two numbers from cell A3 and B3; the result should go in cell C
- 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)
- 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).
- 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
isEven = True
Else
isEven = False
End If
- 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
Get step-by-step solutions from verified subject matter experts
