Excel Macro To Compare Two Columns (4 Easy Ways)
Có thể bạn quan tâm
The sample dataset showcases Bank data: Full Name, Email_ID, and Address.
To check whether the same person has accounts in both banks:

Method 1 – Using Macro to Highlight Unique Values Comparing Two Columns

- Go to the Developer tab >> select Visual Basic You an also press ALT + F11 to open the VBA editor.

- In the Microsoft Visual Basic for Applications, select Insert >> choose Module.

- Enter the following code in the Module.

The Sub procedure Highlighting_Comparing_2Columns was declared: Twocolumns as Range type and i as Integer type variables.
The IF statement compares the values in the selected range and a FOR loop checks all the rows.
If the compared value is unique in a row, it will be highlighted it in Yellow (ColorIndex = 6).
- Save the code and go back to the worksheet.
- Select the cell range to apply the VBA. Here, B4:C10.
- Go to the View tab >> Macros >> select View Macros

A dialog box is displayed.

- In Macro name select Highlighting_Comparing_2Columns
- Select the workbook in Macros in.
- Run the selected Macro.
It will Highlight all unique values in the two columns.

- Run the Macro again to compare Addresses.

Unique values are Highlighted.

Read More: Excel formula to compare two columns and return a value
Method 2 – Using a Macro to Find Matches Comparing Two Columns
- Go to the Developer tab >> select Visual Basic You an also press ALT + F11 to open the VBA editor.

- In the Microsoft Visual Basic for Applications, select Insert >> choose Module.

- Enter the following code in the Module.

The Sub procedure Find_Matches_Comaring2Columns was declared: CompareRange, x, and y as Variant type variables.
F5:F11 is used to compare values.
The IF statement checks whether the value of x and y is equal. FOR loops check all values in Selection and CompareRange.
If the compared value is equal, it will place the values in the adjacent column.
- Save the code and go back to the worksheet.
- Select the cell range to apply the VBA. Here, C5:C11.
- Go to the View tab >> Macros >> select View Macros

A dialog box is displayed.

- In Macro name select Find_Matches_Comaring2Columns.
- Select the workbook in Macros in.
- Run the selected Macro.
All matched values of the two selected columns are displayed in a new column: Matches.

Method 3 – Using a Macro to Extract Unique Values From Two Columns
Extract unique values by comparing two columns. In this section, I’ll show you how to do that using VBA macro.
Two columns were added: Unique Name and Unique Address.
- Go to the Developer tab >> select Visual Basic You an also press ALT + F11 to open the VBA editor.

- In the Microsoft Visual Basic for Applications, select Insert >> choose Module.

- Enter the following code in the Module.

The Sub procedure Extract_Uniques_2Columns was declared: rngCell is the Range type variable.
To compare the values, an IF statement within the FOR loop was used.
The FOR loop is used twice to compare two different types of values.
- Save the code and go back to the worksheet.
- Go to the View tab >> Macros >> select View Macros

A dialog box is displayed.

- In Macro name select Extract_Uniques_2Columns.
- Select the workbook in Macros in.
- Run the selected Macro.
It will extract all unique values in the column ranges.

Method 4 – Using a Macro to Highlight Duplicate Values From Different Sheets

- Go to the Developer tab >> select Visual Basic You an also press ALT + F11 to open the VBA editor.

- In the Microsoft Visual Basic for Applications, select Insert >> choose Module.

- Enter the following code in the Module.

The Sub procedure Highlight_Comapring_2sheetsColumn was declared: Last_sheet1_Row and Last_sheet2_Row are Long type variables. i, j are two Integer type variables.
An IF statement was used to check whether the compared values are equal within nested FOR loops.
If the compared value is equal, it will highlight the values in the Email_ID column.
rgbRed was used as Font.Color.
- Save the code and go back to the worksheet.
- Go to the View tab >> Macros >> select View Macros

A dialog box is displayed.

- In Macro name select Highlight_Comparing_2sheetsColumn.
- Select the workbook in Macros in.
- Run the selected Macro.
It will highlight all duplicate values in the Email_ID column of both sheets.

The highlighted duplicate values in sheet1.

The highlighted duplicate values in sheet2.
Practice Section
Practice here.

Download To Practice
Excel Macro to Compare Two Columns.xlsm<< Go Back to Columns | Compare | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!Từ khóa » Visual Basic Excel Compare Cell Values
-
Compare 2 Cells In Excel By Using Vba - Stack Overflow
-
Vba -excel -how To Check And Compare Cell Value Against Next Cell's ...
-
Compare Ranges In Excel VBA (In Easy Steps)
-
VBA Cell Value - Get, Set, Or Change - Automate Excel
-
Comparing Two Cells With VBA | MrExcel Message Board
-
Compare Cells With This Excel VBA Function
-
How To Compare Cells With VBA - YouTube
-
How To Compare The Values Of Cells In A Range Against One ... - Quora
-
How To Compare Two Columns In Excel Using VBA - SpreadsheetWeb
-
Excel StrComp Function: Compare String Values For Matches
-
Use VBA To Compare Two Ranges In Excel And Highlight The Cells ...
-
Get Cell Value In Excel VBA - WallStreetMojo
-
By Using VBA How Do I Compare Values - Super User
-
VBA Comparison Operators: Not Equal, Less Than Or Equal To - Guru99