VBA Debugging and Programming Practice (See...

22
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

Transcript of VBA Debugging and Programming Practice (See...

Page 1: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 2: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Excel Files

Files used in this lecture:

I debugging.xlsm

See the module ModDebug for illustrations of debugging in VBA

2

Page 3: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Outline

Debugging

Tips and VBA Practice

3

Page 4: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 5: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 6: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 7: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 8: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 9: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 10: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Runtime Errors

Why are we getting this runtime error?

Change:

Dim classSize As Integer

To:

Dim classSize As Double

Why does it work?

10

Page 11: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 12: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 13: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Logic Error

Logic error.

13

Page 14: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Logic Error

To use the Debug Tool Bar, in the VBA editior

Go to View

Select Toolbars

Select Debug

14

Page 15: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Logic Error

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

15

Page 16: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 17: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 18: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Runtime Errors

What do you do?

Anyone have any useful debugging tips?

18

Page 19: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 20: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 21: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

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

Page 22: VBA Debugging and Programming Practice (See …faculty.chicagobooth.edu/kipp.martin/root/htmls/coursework/36104/... · VBA Debugging and Programming Practice (See Chapter 5 (pages

Tips

What do you do?

Anyone have any general useful tips?

22