Composite Primary kay in Excel

bghosh

New Member
Joined
Feb 24, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello Team,

I need to develop a custom primary key in Excel, i.e. based on multiple columns data, it should be a unique combination and none of the cells data can be NULL. For example, data in Column A, B, C, D form an unique pair. So I need to restrict a user to enter duplicate entry for these columns. Individual columns can have duplicate values but combination of Cell A, Cell B, Cell C & Cell D should be unique. How do we achieve this in Excel.

1582551377429.png


We need to generate a custom validation and if user violates the rule, an error message should be thrown. Please help.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi there and welcome to the forum. You can get what you want with a helper column (E) which has all 4 columns concatenated, and a formula in column F to look for duplicates. Try this:
Book2
ABCDEF
1PLOAN11SENSUBRIQUENEGATIFPLOAN11SENSUBRIQUENEGATIF
2PLOAN11SENSUBRIQUEPOSITIFPLOAN11SENSUBRIQUEPOSITIF 
3PLOAN11SENSUBRIQUEPOSITIFPLOAN11SENSUBRIQUEPOSITIFduplicate
Sheet3
Cell Formulas
RangeFormula
E1:E3E1=CONCATENATE(A1,B1,C1,D1)
F2:F3F2=IF(ISNA(VLOOKUP(E2,E$1:E1,1,FALSE)),"","duplicate")
PS Please have a look at XL2BB - it will enable you to post sample ranges directly into your question, as I have done here.
 
Upvote 0
Hello John,

Thanks for replying. There are some limitations in using this solution. I don't have the liberty of using extra columns. Actually, I have 1st row as column names of table. So I cannot add extra columns in the sheet. I am reading the 1st row as column names for generating the Insert script of the table, So no extra columns allowed. Please help me with a solution which can be applied on existing columns.

Thanks & Regards
Biswajeet Ghosh
 
Upvote 0
Hello Biswajeet ,

In that case, the only way would be to have a VBA script which looks for a duplicate. Here is one:
VBA Code:
Option Explicit

Function IsDuplicate(A As String, B As String, C As String, D As String) As Long
    Dim LookFor As String
    Dim OneRow As Long
    Dim OneValue As String
    Dim LastRow As Long
    LookFor = A + B + C + D
    LastRow = Range("A65536").End(xlUp).Row

    Dim AllRows As New Scripting.Dictionary
    For OneRow = 2 To LastRow
        OneValue = Cells(OneRow, 1) & Cells(OneRow, 2) & Cells(OneRow, 3) & Cells(OneRow, 4)
        If AllRows.exists(OneValue) Then
            MsgBox "Duplicate found in existing data on row " & OneRow
        Else
            AllRows(OneValue) = OneRow 'Record the row number
        End If
    Next
    If AllRows.exists(LookFor) Then
        IsDuplicate = AllRows(LookFor)
    Else
        IsDuplicate = False
    End If

End Function

Sub tst()
    Dim fred As Long
    fred = IsDuplicate("PLOAN1", "1", "SENSUBRIQUE", "POSITIF")
    If fred Then
        MsgBox "Duplicate found in row " & fred
    Else
        MsgBox "Unique"
    End If
End Sub
You can use the function in vba as shown in the tst subroutine, or as a formula.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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