VBA Exercises - University of New Haven

advertisement
EAS112 VBA Exercises
Created by Jeff Young, EAS and Mechanical Engineering Peer Tutor
Using Excels VBA, create a function to output the name of the day of the week corresponding to a
specific date.
1. Startup Excel, make sure that the Developer Tab, Macros and VBA are enabled (File->Options)
a. If the Developer Tab is not enabled, using office 2010, go to File->Options->Customize
Ribbon->On right side check the box next to Developer
2. Create a table as is shown below with one dates filled in for one week.
Date
Day of the week
3. Open up VBA and begin with the line: Function DayName(DayDate As Date)
a. DayName is the function that will output the name of the day
b. DayDate is the argument or input for the function to be created and is the date that will
be inputted into the Date column
4. A new variable will have to be called to output a number which will then be converted into a day
name. The new variable will be called DayofWeek and will be an integer since there are 7 days in
a week.
a. The next two lines of code will look like this
Dim DayofWeek As Integer
DayofWeek = Weekday (DayDate, vbSunday)
i. In the line above, an equation has been created to input Excels Weekday()
function, which will output a number 1 through 7 corresponding to the day or
the week relative to Sunday, hence the vbSunday.
1. If you would like the week to start on a Monday instead of Sunday, you
would use the following: DayofWeek = Weekday (DayDate, vbMonday)
5. In order to get Excel to output a text day name, you will have to tell it what each number
outputted by the DayofWeek function corresponds to. For example, if DayofWeek gives a
number 1, the day is Sunday because you have designated Sunday as the first day of the week.
An If statement will be used as is shown below.
If DayofWeek = 1 Then
DayName = "Sunday"
a. This will be repeated through DayofWeek =7, followed by End If
1
This handout is provided by the CLR.
6. The VBA code should look something like this
Function DayName(DayDate As Date)
Dim DayofWeek As Integer
DayofWeek = Weekday(DayDate, vbSunday)
If DayofWeek = 1 Then
DayName = "Sunday"
ElseIf DayofWeek = 2 Then
DayName = "Monday"
ElseIf DayofWeek = 3 Then
DayName = "Tuesday"
ElseIf DayofWeek = 4 Then
DayName = "Wednesday"
ElseIf DayofWeek = 5 Then
DayName = "Thursday"
ElseIf DayofWeek = 6 Then
DayName = "Friday"
ElseIf DayofWeek = 7 Then
DayName = "Saturday"
End If
End Function
7. Try your function for the current date and verify that the day of the week is correct.
2
This handout is provided by the CLR.
EAS112 VBA Exercises
Created by Jeff Young, EAS and Mechanical Engineering Peer Tutor
Interpolation Function
1. Startup Excel, make sure that the Developer Tab, Macros and VBA are enabled (File->Options)
a. If the Developer Tab is not enabled, using office 2010, go to File->Options->Customize
Ribbon->On right side check the box next to Developer
2. Create a table as is shown below
Temp (K) Press (KPa)
1
20
100
2
30
x
3
40
200
a. In this example, we are only given the pressure values corresponding to temperatures of
20 and 40 Kelvin, and we wish to find the pressure at 30 Kelvin. From simple math we
can see that, since 30 is halfway between 20 and 40, the pressure at 30 should be
halfway between 100 and 200 KPa, which is 150 KPa.
3. A function will now be created to output the pressure at state 2 such given the temperature and
pressure inputs shown above. The interpolation equation is shown below.
𝑦2 − 𝑦0 𝑦1 − 𝑦0
=
𝑥2 − 𝑥0 𝑥1 − 𝑥0
0, 1 and 2 represent the three states shown above 1, 2 and 3. The pressure at state 2
needs to be found, which would be y1. Solving for this value, the resulting equation is:
𝑦1 = 𝑦0 +
𝑦1 − 𝑦0
(𝑥2 − 𝑥0)
𝑥1 − 𝑥0
This will now be plugged into VBA so that the value can be calculated using a function.
4. Open up VBA, create a function called Press2 which depends on the variables y0, y1, x0,
x1 and x2. Because this is a dependent function, all the dependent variables mentioned
must be defines. The first line of code will look like this:
Function Press2(y0 As Double, y2 As Double, x0 As Double, x2 As Double, x1 As Double)
5. Press2 is the variable of interest, so we can create an equation now where Press2
replaces y1 in the interpolation. In VBA, the equation looks like this:
Press2 = y0 + (((y1 - y0) / (x1 - x0)) * (x2 - x0))
6. Now the unknown pressure at state two can be found by calling the Press2 function in
excel. Open the spreadsheet and type in =Press2( select the values for the following
variables from the table in the order shown; y0, y2, x0, x2, x1). Press Enter and the value
produced should be 150 as was predicted.
3
This handout is provided by the CLR.
Download