×

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the code and resolve it.:

The commonly used methods are as follows:

  • Breakpoint
  • Stepping

1. Breakpoint

A selected program line at which execution will automatically stop and then you can see if the error has occurred till that line or not – if the error hasn’t occurred, then you can be sure that the remaining part of the code has the error statement. The Breakpoint is a useful technique in long procedures where it would be tedious and time-consuming to run through each and every line of code before you get to the part that you are trying to debug.

Apply Breakpoint

To add a breakpoint, click on the right-side margin of the line you want the execution to stop at or use the F9 as a shortcut key.

Remove Breakpoint

To remove a breakpoint, either click on the red dot or place the cursor on the specified breakpoint line and again press F9. Next, click on the green arrow to continue execution.

Program: In the below program, the execution will stop at 3rd line of the program before ‘Else’.

Sub BreakpointExample()
 If Worksheets("Sheet1").Cells(2, 1).Value = "India" Then
 'enabling the breakpoint and halting the process
 Worksheets("Sheet1").Cells(1, 1).Value = "Delhi"
 Else
 Worksheets("Sheet1").Cells(1, 1).Value = "Delhi"
 End If
 End Sub 

Output

Debugging in Excel VBA

2. Stepping (for Run-time error debugging)

If while running the code, excel throws an error, you can step into the code and go from one line to another by pressing F8 and see which line excel has thrown an error for. It is the easy and commonly used debugging method is to manually check every line and understand which line gave the error, we use ‘Step-Into’ from Debug menu or press F8 –

Debugging in Excel VBA

As we keep on pressing F8, VBA will keep on highlighting the lines –

Debugging in Excel VBA

As we try to move from this line to another, VBA gives the same error which we got when we ran the entire code suggesting that the error occurred because of this 3rd line which is right – as we are passing a text value to a variable which has been declared as a date variable.

Debugging in Excel VBA

This feature is even more useful when the code is complex or has a lot of lines – this example had just 4 lines.


Related Topics

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

Excel Objects in VBA

What are Excel Objects? The Excel objects belong to the entities that make up an Excel Workbook, Worksheets, Columns, Rows, Cell Ranges, etc. Each object in Excel has loads of Properties that are...

6 minutes read.

Miscellaneous Exercise of conditional statements and Loop

We have the already worked with syntax and examples of conditional statements and loops in the previous tutorials. In this tutorial, we will learn how to work with both together.  We will explain...

3 minutes read.

Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type. Syntax CByte (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Excel VBA Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

Excel VBA CDec Function

VBA CDec Function: The CDec function in VBA converts an expression into a Decimal data type. Syntax CDec (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Excel VBA Str Function

VBA Str Function: The Str function in VBA converts the given number into a string representation of that number. Syntax Str (Number) Parameter Number (required) – This parameter represents the numeric value that you want...

1 minute read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

Excel VBA IsError Function

VBA IsError Function: The IsError function in VBA returns a Boolean value showing whether the specified Expression represents an error or not. Syntax IsError (Expression) Parameter Expression (required)- This parameter represents the variant that you...

1 minute read.

Declaring a Variable in VBA

Declaring a Variable A variable is broadly described as a storage location combined with a name and representing a specific value. Declaring a variable is instructing the computer to reserve space...

2 minutes read.

Excel VBA IsNumeric Function

VBA IsNumeric Function: The IsNumeric function in VBA returns a Boolean value showing whether the specified Expression contains a numeric value or not. Syntax IsNumeric (Expression) Parameter Expression (required)- This parameter represents the variant that...

1 minute read.

Basics of Userform

A User Form is a built-in customized dialog box that fetches data from the user through a user-friendly dialog box or window that makes up part of an application's user interface. It...

9 minutes read.

Excel VBA IsEmpty Function

VBA IsEmpty Function: The IsEmpty function in VBA returns a Boolean value showing whether the specified Expression is Empty (variant has not been declared) or not. Syntax IsEmpty (Expression) Parameter Expression (required)- This parameter...

1 minute read.

VBA Charts

What is a Chart? A chart is used to visually show numbers or data in a spreadsheet (or spread over multiple spreadsheets) so that the end-user can look at the chart...

3 minutes read.

Excel VBA IsMissing Function

VBA IsMissing Function: The IsMissing function in VBA checks if any parameter to a procedure is missing or not. It returns a Boolean value True if the specified parameter has not been...

1 minute read.

If ElseIf ElseIf Statement or Nested If statement in VBA

VBA Excel: If … ElseIf … ElseIf Statement or Nested If statement This function enables you to check multiple conditions and, based on that, then run one of the statement blocks present. If...

2 minutes read.

Excel VBA DatePart Function

The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time. Syntax DatePart (Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required) – This parameter represents a string specifying the interval to be used. It can...

2 minutes read.