Presentation is loading. Please wait.

Presentation is loading. Please wait.

IE 8580 Module 4: DIY Monte Carlo Simulation

Similar presentations


Presentation on theme: "IE 8580 Module 4: DIY Monte Carlo Simulation"— Presentation transcript:

1 IE 8580 Module 4: DIY Monte Carlo Simulation
Lecture 4.3: Visual Basic for Applications 102

2 More detailed steps Add a module Start the sub Project Explorer
Highlight the Excel file Insert > Module Start the sub In Code window, type Sub LoopIt <Enter> () and End Sub are added automatically IE 8580,

3 Write the Following Program
Notice the blue for the reserved words IE 8580,

4 Create a button in Excel
Go back to Excel Developer Tab Click Insert Click the button symbol Draw button Assign Macro Dialog Box opens Select the macro we created, click OK Click the button while still selected to change text Unselect the button Click it to run To reselect the button, right click the button then left click IE 8580,

5 Declaring Variables Declare all variables Use Dim keyword Basic Types
Whole Numbers: Integer (-32,768 to 32,768), Long (more than Integer) Real Numbers: Single, Double String, Boolean, Currency, Variant Objects If you try to assign a value out of the allowable range, you will get an OVERFLOW error IE 8580,

6 Syntax for Declaring Variables
Basic syntax Dim varname As type Multiple variables on a line Dim i As Integer, j As Integer, k As Integer Variables get values using the equal sign assignment i = 1 IE 8580,

7 The ampersand (&) concatenates the information
Message Boxes Most basic syntax MsgBox “stuff for the box” MsgBox “the value of the variable is ” & varname The ampersand (&) concatenates the information Be careful with spaces!!! IE 8580,

8 If condition then do_something Else do_something_else End if
Basic If Syntax If condition then do_something If condition then do_something Else do_something_else End if IE 8580,

9 If condition then do_something Else do_something_else End if
Basic If Syntax If condition then do_something If condition then do_something Else do_something_else End if Statements can be multiple lines Indentation helps readability VBA does some indention for you I love tabs IE 8580,

10 If condition then do_something Else do_something_else End if
Basic If Syntax If condition then do_something If condition then do_something Else do_something_else End if The “else” clause is optional IE 8580,

11 If condition then do_something Else do_something_else End if
Basic If Syntax If condition then do_something If condition then do_something Else do_something_else End if The “else” clause is optional IE 8580,

12 If-then-elseif Constructions
If condition1 then do_something1 Elseif condition2 do_something2 Elseif … Else do_default End if IE 8580,

13 For repeating a statement or series of statement over and over again
Looping For repeating a statement or series of statement over and over again Definite (pre-determined) number of repetitions: use a for loop Indefinite number of repetitions: Repeat some code while some condition holds: use one of the four do (or while) loop structures IE 8580,

14 For counter = first To last [Step increment] statements Next [counter]
For Loops first, last and increment are often integers (positive or negative, increment is not 0) Using the counter in the Next statement Helps readability Required for nested for loops For counter = first To last [Step increment] statements Next [counter] IE 8580,

15 Check If You Are Following Me!
Write a sub that requests a positive integer between 10 and 20 (assume it is correctly entered) Display each odd number from one to entered integer in ONE messagebox (build the message in a string then display it) IE 8580,

16 Answer for the Check IE 8580,


Download ppt "IE 8580 Module 4: DIY Monte Carlo Simulation"

Similar presentations


Ads by Google