Exam 3-VBA
Supplemental Instruction
IowaStateUniversity / Leader: / Becca
Course: / ChE 160
Instructor: / Heinen/Haman
Date: / 03/29/2016
Resources
- VBA chapters on Blackboard. HINT: Download for future reference!
- Youtube channels such as WiseOwlTutorials
- ByRef/ByVal explanation:
Study strategies
- Really understand the basics and go from there
- Look at the SI worksheets posted
- Redraw graphs and figures
- Rewrite notes
- Reword ideas and principles
- Write out steps to problem solving
- Talk about notes and concepts with a friend
- Make your own example problems
- Write a reference sheet with all study information to review right before
- Make your 3”x5” note card
Notes from Becca
- Read through Exam 3 information on Blackboard
- You should be able to finish the entire exam in 50 min
- Strategize—start with the easy problems first to knock them out or hard problems first
- If you don’t know, skip it and come back
- Show your work! Partial credit is better than no credit
PRACTICE EXAM (Note: adapted from Dr. Heinen’s practice exam format, and this practice exam is longer than what the actual exam would be!)
State the value of the variable that results from each of the VBA commands. (Do you understand the logic of loops? How can you organize the values after each loop interation?)
T = 8T = 128
Do
If (T <= 87) Then
T = T*2
ElseIf (T= 87) Then
T = T^0.5
Else Exit Do
End If
Loop
z(1,1) = 20 z = [20, 10, 5, 2.5]
For count = 2 To 4
z(1,count) = z(1,count-1) / 2
Next count
Sub SI_Is_Awesome()
Dim a As Integer, b AsSingle, c As Single
a = 5
b = 12.5
c = difference(a,b)
End Sub
Function difference(ByVal a As Integer, ByRef b As Single) As Singlea = 5
a = 25b = 25
b = b*2c = 0
difference = a-b
End Function
Short Answer
Write the command that requires all variables to be declared by type.
Option Explicit
What are the four data types addressed in ChE 160 VBA Unit?
Single, Double, Integer, String
Write a loop that calculates the factorial of whole, even numbers from 2 to 10, and then displays the results with a message box. Which loop would be best for this type of problem? How do you know?
Sub QuestionFactorial()
Dim FactorialAs Single, count as Integer
Factorial = 1 ‘initialize your sum
For count = 2 To 10 Step 2
Factorial = Factorial*count
Next Count
MsgBox(“The factorial of whole, even numbers from 2 to 10 is “ & Factorial)
End Sub
Reynolds Number
The Reynolds number is a dimensionless quantity to predict fluid flow patterns such as laminar and turbulent. (You will learn all about this in ChE 356!)
wherev = superficial velocity [m/s]
(rho) = density [kg/m3]
L = characteristic length [m]
(mu) = dynamic viscosity of fluid [kg/ms]
In a pipe, laminar flow occurs when Re < 2300; transient flow occurs when 2300 < Re < 4000; turbulent flow when Re > 4000.
Write a function that calculates the Reynolds number for given velocity, density, length, and viscosity values. Don’t forget about ByRef and ByVal. What is the difference between them?
Function Reynolds(ByVal v As Single, ByVal rho As Single, ByVal L As Single, ByVal mu As Single) As Single
Re = v*rho*L/mu
End Function
In a sub-procedure, use input boxes to ask the user for superficial velocity, density, characteristic length, and dynamic viscosity. Calculate the Reynolds number using the function you just wrote. Use a message box to display whether the flow is laminar, transient, or turbulent based on the Reynolds number. Do you need a loop? If so, which loop would be best?
Sub PipeFlow()
Dim v As Single, rho As Single, L As Single, mu As Single
v = InputBox(“superficial velocity [m/s]”)
rho = InputBox(“flulid density [kg/m^3]”)
L = InputBox(“Characteristic length [m]”)
mu = InputBox(“dynamic viscosity of fluid [kg/ms]”)
ReNum = Reynolds(v, rho, L, mu)
If ReNum < 2300 Then
MsgBox(“This flow is laminar.”)
ElseIf Re > 4000 Then
MsgBox(“This flow is turbulent.”)
Else
MsgBox(“This flow is transient.”)
End If
End Sub
The temperature data shown below is contained in the file X:\Desktop\Tdata.txt, and is from your work in ChE 325 lab with shell-and-tube heat exchangers. Input the data into a VBA matrix, and then export the data to a new file, X:\Desktop\HeatExchT.txt, that can be read by VBA. Which form of outputting information can be read by VBA?
T_cold,in [C] / T_cold,out [C]11.2 / 35.3
11.1 / 32.8
10.9 / 31.7
10.8 / 28.7
10.7 / 26.3
10.7 / 26.1
Sub Data_Problem()
DimMatrix(1 To 6, 1 To 2) As Single, Count As Integer, Count1 As Integer
Open(“X:\Desktop\Tdata.txt”) For Input As 1
For Count = 1 To 6
Input #1, Matrix(Count,1), Matrix(Count,2)
Next Count
Close #1
Open (“X:\Desktop\HeatExchT.txt”) For Output As 2
For Count1 = 1 To 6
Write #2, Matrix(Count,1), Matrix(Count,2)
Next Count
Close #2
End Sub
A 7.1 m^3 cylindrical tank rated to a pressure of 15.7*10^5 Pa contains 1500 moles of gas. Use a do loop to calculate the maximum temperature that the gas in the tank can reach before exceeding the pressure rating. Begin at 273 K and work in 5 K increments. R = 8.314 Pa m^3/mol K. Use a message box to display “The maximum temperature before exceeding the pressure rating is ______K.”).What should be inside the do loop? What do you have to initialize before?
Sub PressureTank()
Dim V As Single, n As Single, T As Integer, R As Single, P As Single
V = 7.1
n = 1500
T = 273
R = 8.314
Do
P = n*R*T/V
If P > 15.7*10^5 Then Exit Do
T = T + 5
Loop
T = T -5 ‘Have to undo what the do loop added on to
MsgBox(“The maximum temperature is “ & T & “K.”)
End Sub
After all of that, what should you put on your 3”x5” note card to bring into the exam?
What should you spend more time studying?
Are there any study strategies that have worked well for you?