VBA Debugging and Programming Practice (See...

Preview:

Citation preview

VBA Debugging and Programming Practice(See Chapter 5 (pages 71-78) of Albright Ed 3)(See Chapter 5 (pages 78-84) of Albright Ed 4)

Kipp Martin

January 11, 2012

1

Excel Files

Files used in this lecture:

I debugging.xlsm

See the module ModDebug for illustrations of debugging in VBA

2

Outline

Debugging

Tips and VBA Practice

3

Debugging

There are three kinds of errors:

I Compile time errors (syntax problems)

I Runtime errors (logic errors that get reported at runtime)

I Logic errors (these occur at runtime but you do not get anerror message)

Unlike a true programming language such as C, C++, the compileand runtime concepts get blurred with scripting languages such asVBA.

4

Compile Time Errors

VBA is not a compiled language, but the VBA editor interpretseach line as you type.

It will pick up simple syntax errors.

If classSize > 10 MsgBox "HI"

Another simple compiling error.

x = 5 +

Each of these errors get flagged when you hit return.

5

Compile Time Errors

A more complicated compile error – you need to execute theMacro to find this one.

Sub CompileTimeError()

Dim I As Integer

Dim sum As Integer

sum = 0

For I = 1 To 3

Debug.Print I

sum = sum + I

MsgBox sum

End Sub

6

Runtime Errors

Once you get a clean compile, you may get errors when the Macroactually executes.

The error may depend on the values that the program computes.

Sub RunTimeError1()

Dim x As Double

x = InputBox("input a number")

MsgBox 1 / x

End Sub

7

Runtime Errors

A bit more obscure runtime error (see Sub RunTimeError2() ).

Range("E2", Range("E2").End(xlDown)).Name = _

"MidtermGrades"

Set midtermRange = Range("MidtermGrades")

classSize = midtermRange.Rows.Count

MsgBox classSize

8

Runtime Errors

Hint:

Step 1: Enter debug mode (click the Debug button).

Step 2: Move cursor over the variable when in debug mode. Youwill see the current value of the variable. Pretty neat!

9

Runtime Errors

Why are we getting this runtime error?

Change:

Dim classSize As Integer

To:

Dim classSize As Double

Why does it work?

10

Logic Error

These are most insidious and difficult to find.

The computer does not tell you that you have the wrong answer.

You get the wrong answer and may not know that you have thewrong answer!

Concept of unitTest.

11

Logic Error

A logic error (See Sub AverageScores()). See pages 75-76 of thetext.

With Range("A1")

Set scoreRange = Range(.Offset(0, 0), .End(xlDown))

End With

sum = 0

For Each cell In scoreRange

If IsNumeric(cell.Value) Then sum = sum + cell.Value

Next

MsgBox "Average Scores = " & sum / scoreRange.Cells.Count

12

Logic Error

Logic error.

13

Logic Error

To use the Debug Tool Bar, in the VBA editior

Go to View

Select Toolbars

Select Debug

14

Logic Error

The Debug ToolBar. Let’s set a watch on the variable.

15

Logic Error

To use a watch on a variable.

Put the cursor on the sum variable.

Select the Quick Watch button in the Debug Tool Bar.

Variables values appear in the Watch Window

Execute Step Into

16

Runtime Errors

I Set breakpoints – really neat – use this with the Localswindow to see values of the variables.

I Step over subs

I Step out subs

17

Runtime Errors

What do you do?

Anyone have any useful debugging tips?

18

Tips

I ALWAYS use Option Explicit

I Use

Application.ScreenUpdating = False

if there are frequent updates to the worksheet. See theExample Macro Sub ScreenUpdateTest()

I Use commenting to document your code.

I Use Debug.Print for getting values of variables whendebugging

19

Tips

I Indent For and If blocks to improve readability

I Once you have code that works, back it up before you moveon and alter the code. You might want to keep a folder foreach day. That way you can go back and recover old code.Hard disk space is cheap, your time is not.

I Give meaningful variable defintions

I Tip on using editor to comment out many lines

20

Tips

I Tip on using editor to indent and unindent blocks of code

I Stepping through lines of code – using F8. See page 77 ofAlbright.

I Use MsgBox rangeName.Address to see the address of yourranges – very useful.

21

Tips

What do you do?

Anyone have any general useful tips?

22

Recommended