How to Compare 2 Columns Through VBA in Microsoft Excel?
Tags: tocompare2columns, vbamacros
Published: 2021-09-16 02:28:02

---
title: How to Compare 2 Columns Through VBA in Microsoft Excel?
published: true
date: 2021-09-15 09:43:46 UTC
tags: ToCompare2Columns,VBAMacros
canonical_url: https://geekexcel.com/how-to-compare-2-columns-through-vba-in-microsoft-excel/
---

In this tutorial, we will guide you on how to **compare 2 columns** through [VBA](https://geekexcel.com/how-to-use-vba-class-modules-in-excel-office-365/) in Microsoft Excel. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Compare the list of two columns

- Firstly, you need to **create the two list of data** in the Excel Spreadsheet.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-11-1024x513.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic option](https://geekexcel.com/wp-content/uploads/2020/09/1-106.png)<figcaption>Select the Visual Basic option</figcaption>

- Now, you have to  **copy and paste**  the following **code**.

```
Sub PullUniques()
    Dim rngCell As Range
    For Each rngCell In Range("A2:A40")
        If WorksheetFunction.CountIf(Range("B2:B40"), rngCell) = 0 Then
            Range("C" & Rows.Count).End(xlUp).Offset(1) = rngCell
        End If
    Next
    For Each rngCell In Range("B2:B40")
        If WorksheetFunction.CountIf(Range("A2:A40"), rngCell) = 0 Then
            Range("D" & Rows.Count).End(xlUp).Offset(1) = rngCell
        End If
    Next
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-39.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/09/Run-the-Code-20.png)<figcaption>Run the Code</figcaption>

- Finally, the **unique list from columns A & B** will be generated automatically in **columns C & D** in Microsoft Excel.

![](https://geekexcel.com/wp-content/uploads/2021/09/Output-13-1024x305.png)<figcaption>Output</figcaption>

## Conclusion

Here, we have explained the step-by-step procedure on how to **compare 2 columns** through VBA in Microsoft Excel. Make use of this. Please share your worthwhile  **feedback**  in the below  **comment**  section. To learn more, check out our website **[Geek Excel](https://geekexcel.com/)!!**

**Read Next:**

- **[Compare Two Ranges of Cells If they are Equal or Different in Excel 365!](https://geekexcel.com/compare-two-ranges-of-cells-if-they-are-equal-or-different-in-excel-365/)**
- **[Excel Formulas to Compare two Text Strings ~ Easy Tricks!!](https://geekexcel.com/excel-formulas-to-compare-two-text-strings/)**
- **[How to Compare Two Ranges using Macros (VBA) in Excel 365?](https://geekexcel.com/how-to-compare-two-ranges-using-macros-vba-in-excel-365/)**
- **[Compare Two Ranges to Select/Shade Duplicate or Unique Values in Excel 365!!](https://geekexcel.com/compare-two-ranges-to-select-shade-duplicate-or-unique-values-in-excel-365/)**
- **[Excel Formulas to Check Dates are Same Month and Year {Easy Tricks}](https://geekexcel.com/excel-formulas-to-check-dates-are-same-month-and-year-easy-tricks/)**