Checking for Duplicate in VBA code

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I am trying to check if the value in a combobox exists already in a column of a worksheet (Column I or 9). If the value does not exist I want to add it to the bottom of the list and filter the column A to Z, if it does I want it to do nothing.

My code seemingly is parsing the column correctly, but it is not identifying if the entry is a duplicate or not and adds it to the list anyways. Hope someone can point out what is missing.

VBA Code:
    Dim lastRow As Long
    Dim mtchIndex As String
    Dim iCntr As Long
  
    lastRow = Fields.Range("I65000").End(xlUp).Row
  
    For iCntr = 1 To lastRow
    If Cells(iCntr, 9) <> OrderEntry.UPCBox.Value Then
        mtchIndex = "New"
    Else
        mtchIndex = "Duplicate"
    End If
    Next

Even if I use existing values the mtchindex always resolves as "new".
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try replacing your:
VBA Code:
    For iCntr = 1 To lastRow
    If Cells(iCntr, 9) <> OrderEntry.UPCBox.Value Then
        mtchIndex = "New"
    Else
        mtchIndex = "Duplicate"
    End If
    Next

With the following:
VBA Code:
MatchCheckResult = Application.WorksheetFunction.CountIf(Range("I1:I" & lastRow), OrderEntry.UPCBox.Value)
'
If MatchCheckResult > 0 Then
    mtchIndex = "Duplicate"     'Match Found
Else
    mtchIndex = "New"           'No Match
End If
 
Upvote 0
Solution
Try replacing your:
VBA Code:
    For iCntr = 1 To lastRow
    If Cells(iCntr, 9) <> OrderEntry.UPCBox.Value Then
        mtchIndex = "New"
    Else
        mtchIndex = "Duplicate"
    End If
    Next

With the following:
VBA Code:
MatchCheckResult = Application.WorksheetFunction.CountIf(Range("I1:I" & lastRow), OrderEntry.UPCBox.Value)
'
If MatchCheckResult > 0 Then
    mtchIndex = "Duplicate"     'Match Found
Else
    mtchIndex = "New"           'No Match
End If
Awesome. Thanks a ton! Works like a charm.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top