How to Use VBA COUNTIFS to Find Duplicate Values in Two Columns
Question details
The user needs a VBA macro to search two separate columns for a specific combination of values and display a warning if the combination appears multiple times.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Validating data entry or cleaning datasets by identifying duplicate multi-column combinations using VBA.
- Observed behavior
- The user wants an efficient way to detect row combinations matching two parameters without relying on slow iteration loops.
Ensure your Excel workbook is saved in a macro-enabled format (.xlsm) and that you have enabled Developer tools in the ribbon to access the Visual Basic Editor.
Use Application.WorksheetFunction.CountIfs
Leverage Excel's built-in COUNTIFS worksheet function inside VBA to efficiently count matching rows across multiple columns.
Using Application.WorksheetFunction.CountIfs is the most efficient way to check for multi-column duplicates. By passing the specific columns and criteria to the function, VBA can process the data natively at high speeds.
It is critical to explicitly qualify your ranges with the worksheet object (e.g., ws.Range) to avoid errors caused by the code accidentally referencing the active sheet instead of the target sheet.
Press Alt + F11 to open the Visual Basic for Applications Editor, then insert a new Module or locate the function where you want to add the duplicate check.
Declare and set your worksheet to ensure your columns are properly qualified. For example: Set ws = ThisWorkbook.Sheets("Sheet1").
Use the Application.WorksheetFunction.CountIfs method. Pass the first column and parameter, followed by the second column and parameter: If Application.WorksheetFunction.CountIfs(ws.Range("B:B"), parametername, ws.Range("C:C"), routingname) > 1 Then
Add a MsgBox inside the If statement to alert the user when a duplicate is found: MsgBox "That combination appears multiple times."

Run Advanced Macros Smoothly with WPS Spreadsheet
WPS Spreadsheet fully supports VBA macros, allowing you to use WorksheetFunction.CountIfs and execute multi-column duplicate checks effortlessly. Experience a highly compatible and lightweight environment for all your automation tasks.
- 1. Enable Developer Tools: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled to access macro features.
- 2. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click on 'Visual Basic' to launch the integrated macro editor.
- 3. Paste and Run: Paste your COUNTIFS macro code into a new module, save your workbook as a macro-enabled file, and execute the code to instantly flag duplicates.

Frequently Asked Questions
Why does my Application.WorksheetFunction.CountIfs throw an error in VBA?
This typically occurs if the variables passed as criteria are empty or if the ranges are not explicitly qualified (using ws.Range instead of just Range). Ensure your worksheet object is correctly set and your variables contain valid data.
Can I use COUNTIFS in VBA to check more than two columns?
Yes, you can add up to 127 range and criteria pairs to the CountIfs function. Just separate each pair with a comma within the function parameters.
How do I highlight the duplicate rows instead of showing a message box?
Instead of triggering a MsgBox, you can modify the macro to apply formatting. For instance, you can use a command like ws.Range("B" & rowIndex).Interior.Color = vbRed inside your logic when the COUNTIFS condition is met.




