×

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: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

Excel VBA CDbl Function

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

1 minute read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Excel VBA MonthName Function

Excel VBA MonthName Function: The MonthName function in VBA returns a string with the month name for the specified month number. Syntax MonthName (Month, [Abbreviate]) Parameter Month (required) – This parameter represents an integer between...

1 minute read.

Excel VBA DateValue Function

The DateValue function in VBA returns a VBA Date from the given String representation of a date wherein the time information is ignored. It is unable to interpret dates that include the...

1 minute read.

CodeIgniter File Uploading Class

The Codeigniter provides a file Uploading library class which is used to upload any file such as images, pdf, mp3, etc. to the codeigniter’s application. It also allows to set various preferences such...

5 minutes read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 minutes read.

Excel VBA Trim Function

The Trim function in VBA removes the leading and trailing spaces from the specified string. Syntax Trim (String) Parameter String (required) – This parameter represents the string from which you want to remove the leading and...

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.

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 Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.

Excel VBA Format Function

VBA Format Function The format function in VBA applies a specified format to an expression and returns the result as a string. Syntax Format (Expression, [Format], [FirstDayOfWeek] , [FirstWeekOfYear] ) Parameter Expression (required)- This parameter represents the expression that you want to format. Format...

3 minutes read.

VBA Regex Pattern Validation

VBA Regex Pattern Validation The key purpose of using Regex in VBA was to validate the data and extract the same category of data. There are certain formats that are standard...

6 minutes read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute read.

Excel VBA: DO WHILE….Loop

DO WHILE….Loop The “Do While” Loop is the same, unlike the FOR statement, just that it will keep on looping till the specified condition is true. It is used when we want to...

3 minutes read.

VBA VBScript Regex Methods Regex

VBA VBScript Regex Methods Regex The VBA Regex supports 3 methods which are as follows: ExecuteReplaceText Execute The execute method is used to extract a match from the given based on the defined matching...

3 minutes read.