Debugging INTRODUCTION DEBUGGER WHAT IS VBA'S DEBUGGING ENVIRONMENT?

Similar documents
A Tutorial for ECE 175

Intro to MS Visual C++ Debugging

Supplement: Visual C++ Debugging

The NetBeans Debugger: A Brief Tutorial

Assembly Programming in Atmel Studio 7 Step by Step Tutorial

Troubleshooting in Microsoft Excel 2002

Lab 8 - Vectors, and Debugging. Directions

Using the Xcode Debugger

Contents. Group 3 Excel Handouts 2010

Computer Science II Lab 3 Testing and Debugging

BasicScript 2.25 User s Guide. May 29, 1996

We are built to make mistakes, coded for error. Lewis Thomas

Outline. Debugging. In Class Exercise Solution. Review If Else If. Immediate Program Errors. Function Test Example

Introduction to IntelliJ

Integrated Software Environment. Part 2

Outline. Writing Functions and Subs. Review Immediate (1-line) Errors. Quiz Two on Thursday (2/23) Same Code Without Option Explicit

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

11Debugging and Handling. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Programming Logic - Beginning

How to Remove Duplicate Rows in Excel

Debugging in Small Basic is the process of analysing a program to detect and fix errors or improve functionality in some way.

Who Am I? Objective. Debugging Essentials

Changing the Embedded World TM. Module 3: Getting Started Debugging

Findmyshift - Getting started with Findmyshift

7 The Integrated Debugger

Parallel Debugging. ª Objective. ª Contents. ª Learn the basics of debugging parallel programs

Section 1 AVR Studio User Guide

1. Managing Information in Table

GETTING STARTED WITH ECLIPSE Caitrin Armstrong

Word 2007 Tables Part 2

Introduction Key features User levels Learning di Monitoring C H A P T E R 1: Using di Monitoring as a Normal User...

Watch the video below to learn more about number formats in Excel. *Video removed from printing pages. Why use number formats?

12/14/2016. Errors. Debugging and Error Handling. Run-Time Errors. Debugging in C# Debugging in C# (continued)

Getting Started in Assembly Programming with Keil uvision and MSP432

C Programming in Atmel Studio 7 Step by Step Tutorial

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

SAS Tricks and Techniques From the SNUG Committee. Bhupendra Pant University of Western Sydney College

Rio Hondo Prep Computer Applications Class

Code::Blocks Student Manual

NetBeans Tutorial. For Introduction to Java Programming By Y. Daniel Liang. This tutorial applies to NetBeans 6, 7, or a higher version.

Debugging Code in Access 2002

Starting Embedded C Programming CM0506 Small Embedded Systems

Using Common Features of Microsoft Office Explore Microsoft Office Start programs and switch between them. Tutorial 1

Objective: Class Activities

This lesson is part 5 of 5 in a series. You can go to Invoice, Part 1: Free Shipping if you'd like to start from the beginning.

Excel Advanced

Excel For Algebra. Conversion Notes: Excel 2007 vs Excel 2003

Debug for GDB Users. Action Description Debug GDB $debug <program> <args> >create <program> <args>

file://c:\documents and Settings\degrysep\Local Settings\Temp\~hh607E.htm

JCreator. Starting JCreator

MPLAB X Debugging Techniques

An Introduction to Tilde

Manual Calculation Definition Excel Shortcut Keyboard

Watch the video below to learn more about creating formulas in Excel. *Video removed from printing pages. Mathematical operators

THE RISK EXPLORER USER GUIDE

Chapter 12 Visual Program Debugger

HPCC - Hrothgar. Getting Started User Guide TotalView. High Performance Computing Center Texas Tech University

The CHEMCAD Interface

1. Right-click the worksheet tab you want to rename. The worksheet menu appears. 2. Select Rename.

TestPartner. Getting Started with Visual Tests

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Introduction. Rehearse and Record Slide Shows. Advanced Presentation Options. Rehearsing Slide Show Timings. Page 1

Excel 2007/2010. Don t be afraid of PivotTables. Prepared by: Tina Purtee Information Technology (818)

THE EXCEL ENVIRONMENT... 1 EDITING...

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

Use of in-built functions and writing expressions

Laboratory Assignment #4 Debugging in Eclipse CDT 1

1.2 - Introduction to the IAR Workbench IDE *

Step through Your DATA Step: Introducing the DATA Step Debugger in SAS Enterprise Guide

IFS Data Migration Excel Add-In

Debugging the Solver How to Ask for Solver Help!

Debugging and Handling Exceptions

Debugging Techniques. CEFET Engineering Week

concrete5 editing cheat sheet

STM32 Ecosystem Workshop. T.O.M.A.S Team

Microsoft Visual Basic Code Execution Has Been Interrupted Error

1 Introduction to MARS

1.1 Considering for Choosing Layout in SmartArt Graphics

Installation & Operating Instructions Macro Naming Version 1.0

ECE QNX Real-time Lab

Macros enable you to automate almost any task that you can undertake

An Introduction to Komodo

Compile your code using ncvhdl. This is the way to compile comp_const.vhd:! "#$ %" #&'

Excel Tips for Compensation Practitioners Weeks Data Validation and Protection

Chapter 11 Running the Model

SPREADSHEET (Excel 2007)

Project Debugging with MDK-ARM

Excel Tips and Tricks

If the list that you want to name will change In Excel 2007 and later, the easiest way to create.

MPLAB SIM. MPLAB IDE Software Simulation Engine Microchip Technology Incorporated MPLAB SIM Software Simulation Engine

Introduction to Excel 2013

April 4, 2001: Debugging Your C24x DSP Design Using Code Composer Studio Real-Time Monitor

Code::Blocks Student Manual

Document Collaboration

Excel 2013 Intermediate

Macros enable you to automate almost any task that you can undertake

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Learning Excel VBA. Creating User Defined Functions. ComboProjects. Prepared By Daniel Lamarche

Excel. Spreadsheet functions

version staff had them to share viewing this this user guide. >Reports, as Logging In the SQL login User Name for your district. perform the guides.

Transcription:

Debugging INTRODUCTION Logic errors are called bugs. The process of finding and correcting errors is called debugging. A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. But for a large, complex program, the most effective approach for debugging is to use a debugger utility. DEBUGGER Debugger is a program that facilitates debugging. You can use a debugger to Execute a single statement at a time. Trace into or stepping over a method. Set breakpoints. Display variables. Display call stack. Modify variables. WHAT IS VBA'S DEBUGGING ENVIRONMENT? In Excel 2016, VBA's debugging environment allows the programmer to momentarily suspend the execution of VBA code so that the following debug tasks can be done: 1. Check the value of a variable in its current state. 2. Enter VBA code in the Immediate window to view the results. 3. Execute each line of code one at a time. 4. Continue execution of the code. 5. Halt execution of the code. These are just some of the tasks that you might perform in VBA's debugging environment.

This document covers the following: How to set a breakpoint? How to clear a breakpoint? How to clear all breakpoints? Debug mode in VBA How to check values in VBA? How to use the Immediate Window? How to move through the code Step Into Step Over Continue Halt WHAT IS A BREAKPOINT? In Excel 2016, a breakpoint is a selected line of code that once reached, your program will momentarily become suspended. Once suspended, you are able to use VBA's debugging environment to view the status of your program, step through each successive line of code, continue execution of the code, or halt execution of the code.

You can create as many breakpoints in your code as you want. Breakpoints are particularly useful when you want to suspend your program where you suspect a problem/bug exists. SETTING A BREAKPOINT Next, let's look at how to set a breakpoint. First, you need to open the VBA environment. The quickest way to do this is by pressing Alt+F11 while your Excel database file is open. To set a breakpoint, find the line of code where you'd like to suspend your program. Left-click in the grey bar to the left of the code. A red dot should appear and the line of code should be highlighted in red. In this example, we've created a breakpoint at the following line of code: While LPos <= Len(pValue)

CLEARING A BREAKPOINT A breakpoint in VBA is indicated by a red dot with a line of code highlighted in red. To clear a breakpoint in Excel 2016, left-click on the red dot next to the line of code that has the breakpoint. In this example, we want to clear the breakpoint at the following line of code: While LPos <= Len(pValue)

Now, the breakpoint is cleared and the line of code should look normal again. CLEARING ALL BREAKPOINTS Because you can set as many breakpoints as you want in Excel 2016, you can save time by clearing all breakpoints in your VBA code at once. To clear all breakpoints in your program, select "Clear All Breakpoints" under the Debug menu.

This will remove all breakpoints from your VBA code, so that you don't have to individually remove each breakpoint, one by one. DEBUG MODE Now that we know how to set and clear breakpoints in Excel 2016, let's take a closer look at the debug mode in VBA. In our example, we've set our breakpoint and entered our AlphaNumeric function as a formula in a cell. This will cause the VBA code to execute.

When the breakpoint is reached, Excel will display the Microsoft Visual Basic window and highlight the line (in yellow) where the code has been suspended.

Now we are in debug mode in our Excel spreadsheet. Now we can do any of the following: 1. Check the value of a variable in its current state. 2. Enter VBA code in the Immediate window to view the results. 3. Execute each line of code one at a time. 4. Continue execution of the code. 5. Halt execution of the code. CHECKING VALUES In Excel 2016, if you want to view the value of a variable in your VBA code at the time that the program was suspended, you can move your mouse pointer over that VBA code. Bubble text will appear displaying the variable/expression name and its value in its current state.

In this example, we've moved the mouse pointer over the expression Value. The bubble text displays the expression name with a value of "123 Main St". This feature is useful if you need to quickly check a variable/expression in your code. USING THE IMMEDIATE WINDOW In Excel 2016, the Immediate window can be used to debug your program by allowing you to enter and run VBA code in the context of the suspended program. We've found the Immediate window to be the most help when we need to find out the value of a variable, expression, or object at a certain point in the program. This can be done using the print command. For example, if you wanted to check the current value of the variable called pvalue, you could use the print command as follows:

In this example, we typed print len(pvalue) in the Immediate window and pressed ENTER. print len(pvalue) The Immediate window displayed the result of 6 in the next line. The Immediate window can be used to run other kinds of VBA code, but bear in mind that the Immediate window can only be used when debugging so any code that you run is for debugging purposes only. The code entered in the Immediate window does not get saved and added to your existing VBA code. WATCH WINDOW The Microsoft Visual Basic for Applications window displays your VBA environment in Excel 2016:

Next, let's analyze the Watch window in the VBA environment. In Excel 2016, the Watch window is usually found below the Code window. It is one of the most valuable tools when debugging in the VBA environment. It lets you: Define and monitor any expression. When in debug mode, it lets you view the value of the watched expression in its current state. This will be discussed in the tutorial on Debugging VBA Code. If the Watch window is not visible when you open the Microsoft Visual Basic for Applications window, you can make it visible by selecting Watch Window under the View menu.

ADDING A WATCH EXPRESSION In Excel 2016, the Watch Window displays the value of a watched expression in its current state. This can be extremely useful when debugging VBA code. Let's explore how to add an expression to the Watch Window. To add a Watch expression, select Add Watch under the Debug menu.

When the Add Watch window appears, enter the expression to watch and click the OK button when you are done.

In this example, we've entered the following watch expression in the Expression field: Len(pValue) Next, we've selected AlphaNumeric as the Procedure and Module1 as the Module when setting up the Context for the watched expression. Finally, we've selected Watch Expression as the Watch Type but there are 3 options to choose from: Watch Type Watch Expression Break When Value Is True Break When Value Changes Description To display the value of the watched expression in its current state To stop the execution of the code when the value of the watched expression is True To stop the execution of the code when the value of the watched expression changes Now when you return to the VBA window, the Watch Window will automatically appear if it was previously hidden. Within the Watch Window, all of the watched expressions should be listed including the one that we just added.

As you can see, the expression Len(pValue) now appears in the Watch Window with a value of 6. Adding a watch is a great way to keep track of variables or expressions of interest when debugging your VBA code. CHOOSE HOW TO MOVE THROUGH YOUR CODE Your program is now suspended at the highlighted breakpoint. What's next?

Now that you've suspended execution of your program in Excel 2016 and are in debug mode, you need to choose how to proceed through your code. We'll cover the 4 most common choices. 1. Step Into 2. Step Over 3. Continue 4. Halt Let's look at each of these. STEP INTO While in debug mode, you can "Step Into" your VBA code in Excel 2016.

You can choose to "Step Into" your code in Excel 2016. What this means is that you will step through each line of code in your current procedure as well as step into the code of any procedures that are called by the current procedure. You can do this by either pressing the F8 key or selecting "Step Into" under the Debug menu.

Each time you select "Step Into", the debugger will move you to the next line of code. STEP OVER While in debug mode, you can "Step Over" your VBA code in Excel 2016.

You can choose to "Step Over in Excel 2016". What this means is that you will step through each line of code in your current procedure, but step over (ie: run as a unit) any procedures that are called by the current procedure. You can do this by either pressing the Shift+F8 key or selecting "Step Over" under the Debug menu.

Each time you select "Step Over", the debugger will move you to the next line of code in your current procedure. If your current procedure calls another procedure, the debugger will step over the called procedure. It won't drill down into the code of the called procedure. CONTINUE While in debug mode, you can "Continue" your VBA code in Excel 2016.

You can choose to "Continue" execution of your code in Excel 2016. What this means is that your suspended program will continue executing from where it left off. With this option, your program should either finish running or suspend at the next breakpoint encountered. You can do this by either pressing the F5 key or selecting "Continue" under the Run menu.

HALT While in debug mode, you can "Halt" your VBA code in Excel 2016.

You can choose to "Halt" execution of your code in Excel 2016. What this means is that your suspended program will halt execution. You will no longer be in debug mode. You can do this by selecting "Reset" under the Run menu.

You would normally select this option after you've identified the bug in your code. Once your code is halted, you can modify your VBA code to remedy the problem. SOURCE https://www.techonthenet.com/excel/macros/vba_debug2016.php