Visual Basic for Applications

advertisement
SIG4040 Applied Computing in Petroleum
VBA Exercises
page 1 of 3
Visual Basic for Applications - Exercises to deliver
Part 1. Practice UDF and Subroutine syntax
The pseudo-stationary productivity index for horizontal wells completed over less than the
whole length of the reservoir can be estimated with the following equation:
JH 
4hLw
D 2h  h 

Fc 
ln 
2   2rw 
Where:
Lw= lenght of the completed wellbore
D= drainage distance, perpendicular to the wellbore
Fc= Function of the completion geometry, given by:
Fc 
Lw Lw L 16

L
L D2


 2n  1 L
n 1 m 1
1
2
  m D L
2
D
sin( mLw / L)
mLw / L
Exercise A
Class exercise #4 showed how to create a VB function to calculate the Geometry function Fc
having as input data L/Lw and L/D. Edit that VB function so it will have as input data L, Lw
and D.
Hint: You will have to declare three variables instead of two. Modify the calculations using L,
Lw and D.
Exercise B
Create a Subroutine that calculates the pseudo-stationary productivity index, JH.

Ask the user for the input data (λ, h, Lw, L, D, rw) (Hint: use the InputBox command)

To calculate Fc, use the function from Exercise A.

Display the result on the screen. (Hint: use the MsgBox command)

Create a command button on a Excel sheet to run the Subroutine from the sheet.
Solution:
Exercise A
Function CorFac(L, Lw, D)
M = 1000
n = 1000
i=1
SIG4040 Applied Computing in Petroleum
VBA Exercises
page 2 of 3
F2 = 0
Do Until i = n
j=1
F1 = 0
Do Until j = M
F = (Sin(j * 3.1416 * (Lw/L)) / (j * 3.1416 * (Lw/L))) / (((2 * i - 1) ^ 2) * (L/D) + ((j ^
2) * (1 / (L/D))))
F1 = F1 + F
j=j+1
Loop
F2 = F2 + F1
i=i+1
Loop
CorFac = (Lw/L) + (Lw/L * (L/D) * (16 / (3.1416 ^ 2)) * F2)
End Function
Exercise B
Sub HorizProdIndex()
lambda = InputBox("Enter the lambda factor")
h = InputBox("Enter h")
L = InputBox("Enter L")
Lw = InputBox("Enter Lw")
D = InputBox("Enter D")
rw= InputBox("Enter rw")
FC = CorFac(L, Lw, D)
JH=(4*lambda*h*Lw)/((FC*D/2)+(2*h/3.1416)* Log(h /(2*3.1416*rw)) / Log(10))
MsgBox "The Horizontal well Productivity Index , JH is," & JH
End Sub
SIG4040 Applied Computing in Petroleum
VBA Exercises
page 3 of 3
Part 2. (Practice the Do loop syntax)
Values such as the sin (0.54) or exp (0.98) are easily obtained using a calculator or Excel. To
practice the Do loop in Visual Basic, you can create a UDF (User Defined Function) to
calculate the function sin(x) given x. (Remember that you can’t use “sin” as the name of your
function, since it is already used in Visual Basic as a predefined function.
Use the Maclaurin series with an upper limit of k =20 (or you can end the loop using a
precision variable, as calculators or Excel do).
(1) k x 2 k 1
x3 x5 x7
sin( x)  
 x

 
3! 5! 7!
K 0 (2k  1)!

Solution
Function sine(x)
k = 20
j=0
Sum = 0
Do Until j = k
F = (((-1) ^ (j)) * (x ^ ((2 * j) + 1))) / WorksheetFunction.Fact((2 * j) + 1)
Sum = Sum + F
j=j+1
Loop
sine = Sum
End Function
Download