PDA

View Full Version : How To Prevent Adding Duplicated Record In A Table Using VBA



adfo
06-08-2013, 10:58 AM
I need to prevent adding duplicated record in a table .. I attach sample file
I input date on the left.. when I click a button, the range of records are copied and added to the table .. I need to check if the record has been added before adding it .. The duplicated record is a combination of name and date .. the same account should not be entered twice in the same day .. I appreciate your help .. thanks

Admin
06-08-2013, 04:54 PM
Hi

Use this function to identify the duplicate.


Function ISDUPLICATE(ByVal ExeDate As Date, AccountName As String) As Boolean

Dim k As Variant, i As Long, d As Long

AccountName = LCase(AccountName)
d = CLng(ExeDate)
With Worksheets("EXEC Plan")
k = .Range("s6:t" & .Range("s" & .Rows.Count).End(3).row).Value2
For i = 1 To UBound(k, 1)
If k(i, 1) = d And LCase(k(i, 2)) = AccountName Then
ISDUPLICATE = True
Exit Function
End If
Next
End With

End Function

and in your sub use it like


If ISDUPLICATE(CDate(Range("b6").Value), Range("c6")) Then Exit Sub

adfo
06-08-2013, 09:32 PM
Thanks ..
This worked perfect !! .. I appreciate your help thank you very much !!