IE 8580 Module 4: DIY Monte Carlo Simulation Lecture 4.3: Visual Basic for Applications 102
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, mason@clemson.edu
Write the Following Program Notice the blue for the reserved words IE 8580, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
If-then-elseif Constructions If condition1 then do_something1 Elseif condition2 do_something2 Elseif … Else do_default End if IE 8580, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
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, mason@clemson.edu
Answer for the Check IE 8580, mason@clemson.edu