Macro challenge: verify on change that value is unique !!

Nyborg

New Member
Joined
Feb 12, 2010
Messages
12
I need to setup a macro to run on cell change (e.g. by using "Private Sub Worksheet_Change(ByVal Target As Range)").

Case: technician will use bar scanner to scan several batches. Some times there are two batches with the same master ID and in these cases I need the code to find the next vacant column to the right for text input

The macro needs to check that the value entered in column A is unique for that column.
  • If match found then EXCEL VBA must jump the cursor to the row of the found match and find the next vacant column to the right.
  • If unique, then jump to the following column in the same row.

Any suggestions are welcome.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi
Note that this code treats the entries as numbers, tell me if they are strings instead:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, fad$, i%, j%, r%


If Not Intersect(Target, Range("a:a")) Is Nothing Then
    i = 0
    With ActiveSheet.Range("a:a")
        Set c = .Find(Target.Value, LookIn:=xlValues)
        fad = c.Address
        Do
            Set c = .FindNext(c)
            If c.Value = Target.Value And c.Address <> fad Then i = 1
            r = c.Row
        Loop While Not c Is Nothing And c.Address <> fad And i = 0
    End With
    
    Select Case i
        Case Is = 0         ' unique
            Cells(Target.Row, 2).Activate
        Case Is = 1         ' match found
            j = 0
            Do
                j = j + 1
            Loop While Cells(r, 1).Offset(, j).Value <> ""
            Cells(r, j + 1).Activate    ' next vacant to the right
    End Select
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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