HOME

Monday, January 30, 2012

Set Primary Key in Excel


press alt+f11

Private Sub worksheet_change(ByVal target As Range)
Dim targ As Range, cel As Range, rg As Range
Set targ = [c1:c15]
'change to the cell range being watched for duplicates
Set targ = Intersect(targ, target)
If targ Is Nothing Then Exit Sub
For Each cel In targ.Cells
If cel <> "" Then Set rg = Columns(cel.Column).Find(cel, LookIn:=xlValues, after:=cel)
If rg.Address <> cel.Address Then
Application.EnableEvents = False
cel = ""
cel.Select
Application.EnableEvents = True
MsgBox "duplicate entry! " & cel.Value
End If
Next
End Sub

and den save as Macro enabled work sheet
and den enable developer tab
and den in developer tab enable the macro security

No comments:

Post a Comment