This preview has intentionally blurred parts. Sign up to view the full document

View Full Document

Unformatted Document Excerpt

Handling a Common Programming Mistake David Rossiter, Eddie Chan and Oz Lam COMP1022Q Introduction to Computing with Excel VBA VBA Can Automatically Make Variables • Usually, when the VBA engine is running some code and finds an unknown variable name, it will automatically create a new variable with that name, and then carry on with the code • For example, the variable MyMoney is automatically created in the following code: COMP1022Q Handling a Common Programming Mistake Page 2 MyMoney = 100000 MsgBox MyMoney The variable MyMoney is created automatically here without using a Dim statement Automatic Variable Creation – Good or Bad? • Because VBA will make variables for you, you can be lazy and not create any variables before you use them – i.e. you don’t need to use any Dim statement in the code at all! • That sounds great – but sometimes this can lead to bugs, i.e. mistakes in the code, that are difficult to find • In the following example we show a bug that means the money in a bank account is displayed wrongly COMP1022Q Handling a Common Programming Mistake Page 3 An Example of... View Full Document

End of Preview

Sign up now to access the rest of the document