Types and Loops
Short List of VBA Types Data Type Range Byte 0 to 255 Boolean True or False Integer -32678 to 32678 Long(integer) -2147,483,648 to 2147,483,648 Single(floating) 1.4 E-45 to 3.4 E 38 digits for Positive Values Double (floating) 4.9 E -324 to 1.79 E 208 for positive values String (Variable Length) 0 to approximately 2 billion String (Fixed Length) 0 to approximately 65,400 Variant (with Numbers) Any Numeric value upto length of double Variant (with Characters) Same as String variable length
Option Explicit If you don’t declare the data type for a variable that you use in a VBA routine, VBA uses the default data type “VARIANT”. DATA stored as a variant acts like a CHAMELON, it changes type depending on what you do with it.
Public and Private Function Public (Optional) indicates that the function procedure is accessible to all other procedures in all other modules in all active excel VBA Projects Private: Indicates that the function procedure is accessible only to other procedures in the same module
Variables Public Variables To make a variable available to all procedures in all VBA modules in a project Public SomeRate as long (declared outside of function) Private Variables Local Variable is a variable declared within a procedure. Local variables can be used only in the procedure in which they are declared. When the procedure ends, the variables no longer exists. Ex: Dim
Loops For Loop : used mainly for loops where the number of times the action is repeated is known in advance Function ForDemol(N As Integer) As Long Dim i As Integer Dim j As Long If N <= 1 Then ForDemol = 1 Else j = 1 For i = 1 To N Step 1 j = j * i Next i ForDemol = j End If End Function
Loops A top-checking loop: The loop condition is checked before anything else gets done. The something to be done can be left undone if the condition is not fulfilled on entry to the loop. A bottom-checking loop: The loop condition is checked after the something to be done is done. The something to be done will always be done at least once.
LOOPS A top-checking loop: Do while <condition> Your Statements Do until <condition> Loop A Bottom-checking loop: Do Your Statements Loop while <condition> Do Loop Until <condition>
Top Checking Loops Top Checking Loops: Do While - Loop Function DoWhileDemo(N As Integer) As Integer Dim i, j As Integer If N < 2 Then DoWhileDemo = 1 Else i = 1 j = 1 Do While i <= N j = j * i i = i + 1 Loop DoWhileDemo = j End If End Function
Top Checking Loops Top Checking Loops: Do Until - Loop Function DoUntilDemo(N As Integer) As Integer Dim i, j As Integer If N < 2 Then DoUntilDemo = 1 Else i = 1 j = 1 Do Until i > N j = j * i i = i + 1 Loop DoUntilDemo = j End If End Function
Bottom Checking Loops Do Loop While < Condition> Function DoLoopWhileDemo(n As Integer) Dim i As Integer Dim j As Long If n < 2 Then DoLoopWhileDemo = 1 Else i = 1 j = 1 Do j = j * i i = i + 1 Loop While i <= n DoLoopWhileDemo = j End If End Function
Bottom Checking Loops Function DoLoopUntilDemo(n As Integer) Dim i As Integer Dim j As Long If n < 2 Then DoLoopUntilDemo = 1 Else i = 1 j = 1 Do j = j * i i = i + 1 Loop Until i > n DoLoopUntilDemo = j End If End Function
Can you write a Function to Compute Implied Volatility Start with a high value Start with a low value Let high =1 Let low =0 We have stock price, strike price, interest rate, time to expiry and Traded_Call_price (Let us call it Target ) Objective to compute Implied Volatility (sigma).
Plug in the average value of (Implied) volatility (sigma) (high +low)/2, and check whether the call price is greater or less than target ( traded option price) if greater then reduce the high to the average value(average of high and low). If less than increase the low to the average value (average of high and low)
We have to check this condition as long as say the difference between high and low is small. We need to repeat the process or put it in a loop. Put the above condition in a loop such that you come out as soon as the difference between high and low is small We will repeatedly test the above condition as long as (High –low) is let say as small as .0001.