PC Review


Reply
Thread Tools Rate Thread

Check for duplicate values

 
 
Freddy
Guest
Posts: n/a
 
      27th May 2009
What VB macro code can be used to check for duplicate values AFTER all
entries are made into a column instead of as they are being entered? I am
running Microsoft Visual Basic 6.5 in Office 2002. I found the code below,
written by Ardus Petus, that checks for duplicate values AS THEY ARE ENTERED:

'----------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
'Adjust next constant to your own needs
Const myColumn As String = "B"
Dim rng As Range
Dim Found As Range

Set rng = Columns(myColumn)
If Intersect(Target, rng) Is Nothing Then Exit Sub
Set Found = rng.Find(Target.Value)
If Found.Address <> Target.Address Then MsgBox ("Duplicate code")
End Sub
'-----------------------------------

 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      27th May 2009
Modified...Try the below and feedback

Sub Macro()
'Adjust next constant to your own needs
Const myColumn As String = "B"
Dim rng As Range
Dim cell As Range
Dim Found As Range

lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
For Each cell In rng
If cell.Text <> "" And IsError(cell.Value) <> True Then
Set Found = rng.Find(cell.Value)
If Not Found Is Nothing Then
If Found.Address <> cell.Address Then cell.Interior.Color = vbRed
End If
End If
Next

End Sub
--
If this post helps click Yes
---------------
Jacob Skaria


"Freddy" wrote:

> What VB macro code can be used to check for duplicate values AFTER all
> entries are made into a column instead of as they are being entered? I am
> running Microsoft Visual Basic 6.5 in Office 2002. I found the code below,
> written by Ardus Petus, that checks for duplicate values AS THEY ARE ENTERED:
>
> '----------------------------------
> Private Sub Worksheet_Change(ByVal Target As Range)
> 'Adjust next constant to your own needs
> Const myColumn As String = "B"
> Dim rng As Range
> Dim Found As Range
>
> Set rng = Columns(myColumn)
> If Intersect(Target, rng) Is Nothing Then Exit Sub
> Set Found = rng.Find(Target.Value)
> If Found.Address <> Target.Address Then MsgBox ("Duplicate code")
> End Sub
> '-----------------------------------
>

 
Reply With Quote
 
Freddy
Guest
Posts: n/a
 
      27th May 2009
It worked very well. Can it be modified to inform the user whether or not
duplicates were found and, if necessary, make corrections then rerun the
macro to check for duplicates again?

"Jacob Skaria" wrote:

> Modified...Try the below and feedback
>
> Sub Macro()
> 'Adjust next constant to your own needs
> Const myColumn As String = "B"
> Dim rng As Range
> Dim cell As Range
> Dim Found As Range
>
> lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> For Each cell In rng
> If cell.Text <> "" And IsError(cell.Value) <> True Then
> Set Found = rng.Find(cell.Value)
> If Not Found Is Nothing Then
> If Found.Address <> cell.Address Then cell.Interior.Color = vbRed
> End If
> End If
> Next
>
> End Sub
> --
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Freddy" wrote:
>
> > What VB macro code can be used to check for duplicate values AFTER all
> > entries are made into a column instead of as they are being entered? I am
> > running Microsoft Visual Basic 6.5 in Office 2002. I found the code below,
> > written by Ardus Petus, that checks for duplicate values AS THEY ARE ENTERED:
> >
> > '----------------------------------
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > 'Adjust next constant to your own needs
> > Const myColumn As String = "B"
> > Dim rng As Range
> > Dim Found As Range
> >
> > Set rng = Columns(myColumn)
> > If Intersect(Target, rng) Is Nothing Then Exit Sub
> > Set Found = rng.Find(Target.Value)
> > If Found.Address <> Target.Address Then MsgBox ("Duplicate code")
> > End Sub
> > '-----------------------------------
> >

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      27th May 2009
Untested...Try and feedback

Sub Macro()
'Adjust next constant to your own needs
Const myColumn As String = "B"
Dim rng As Range
Dim cell As Range
Dim Found As Range
Dim blnCount as Boolean

lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
rng.Interior.ColorIndex = xlNone
For Each cell In rng
If cell.Text <> "" And IsError(cell.Value) <> True Then
Set Found = rng.Find(cell.Value)
If Not Found Is Nothing Then
If Found.Address <> cell.Address Then
cell.Interior.Color = vbRed:blnFound = True
End If
End If
End If
Next

If blnFound = True then Msgbox "Duplicates Found"

End Sub
--
If this post helps click Yes
---------------
Jacob Skaria


"Freddy" wrote:

> It worked very well. Can it be modified to inform the user whether or not
> duplicates were found and, if necessary, make corrections then rerun the
> macro to check for duplicates again?
>
> "Jacob Skaria" wrote:
>
> > Modified...Try the below and feedback
> >
> > Sub Macro()
> > 'Adjust next constant to your own needs
> > Const myColumn As String = "B"
> > Dim rng As Range
> > Dim cell As Range
> > Dim Found As Range
> >
> > lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> > Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> > For Each cell In rng
> > If cell.Text <> "" And IsError(cell.Value) <> True Then
> > Set Found = rng.Find(cell.Value)
> > If Not Found Is Nothing Then
> > If Found.Address <> cell.Address Then cell.Interior.Color = vbRed
> > End If
> > End If
> > Next
> >
> > End Sub
> > --
> > If this post helps click Yes
> > ---------------
> > Jacob Skaria
> >
> >
> > "Freddy" wrote:
> >
> > > What VB macro code can be used to check for duplicate values AFTER all
> > > entries are made into a column instead of as they are being entered? I am
> > > running Microsoft Visual Basic 6.5 in Office 2002. I found the code below,
> > > written by Ardus Petus, that checks for duplicate values AS THEY ARE ENTERED:
> > >
> > > '----------------------------------
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > 'Adjust next constant to your own needs
> > > Const myColumn As String = "B"
> > > Dim rng As Range
> > > Dim Found As Range
> > >
> > > Set rng = Columns(myColumn)
> > > If Intersect(Target, rng) Is Nothing Then Exit Sub
> > > Set Found = rng.Find(Target.Value)
> > > If Found.Address <> Target.Address Then MsgBox ("Duplicate code")
> > > End Sub
> > > '-----------------------------------
> > >

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      27th May 2009
Typo...corrected..

Sub Macro()
'Adjust next constant to your own needs
Const myColumn As String = "B"
Dim rng As Range
Dim cell As Range
Dim Found As Range
Dim blnFound as Boolean

lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
rng.Interior.ColorIndex = xlNone
For Each cell In rng
If cell.Text <> "" And IsError(cell.Value) <> True Then
Set Found = rng.Find(cell.Value)
If Not Found Is Nothing Then
If Found.Address <> cell.Address Then
cell.Interior.Color = vbRed:blnFound = True
End If
End If
End If
Next

If blnFound = True then Msgbox "Duplicates Found"

End Sub

--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

> Untested...Try and feedback
>
> Sub Macro()
> 'Adjust next constant to your own needs
> Const myColumn As String = "B"
> Dim rng As Range
> Dim cell As Range
> Dim Found As Range
> Dim blnCount as Boolean
>
> lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> rng.Interior.ColorIndex = xlNone
> For Each cell In rng
> If cell.Text <> "" And IsError(cell.Value) <> True Then
> Set Found = rng.Find(cell.Value)
> If Not Found Is Nothing Then
> If Found.Address <> cell.Address Then
> cell.Interior.Color = vbRed:blnFound = True
> End If
> End If
> End If
> Next
>
> If blnFound = True then Msgbox "Duplicates Found"
>
> End Sub
> --
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Freddy" wrote:
>
> > It worked very well. Can it be modified to inform the user whether or not
> > duplicates were found and, if necessary, make corrections then rerun the
> > macro to check for duplicates again?
> >
> > "Jacob Skaria" wrote:
> >
> > > Modified...Try the below and feedback
> > >
> > > Sub Macro()
> > > 'Adjust next constant to your own needs
> > > Const myColumn As String = "B"
> > > Dim rng As Range
> > > Dim cell As Range
> > > Dim Found As Range
> > >
> > > lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> > > Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> > > For Each cell In rng
> > > If cell.Text <> "" And IsError(cell.Value) <> True Then
> > > Set Found = rng.Find(cell.Value)
> > > If Not Found Is Nothing Then
> > > If Found.Address <> cell.Address Then cell.Interior.Color = vbRed
> > > End If
> > > End If
> > > Next
> > >
> > > End Sub
> > > --
> > > If this post helps click Yes
> > > ---------------
> > > Jacob Skaria
> > >
> > >
> > > "Freddy" wrote:
> > >
> > > > What VB macro code can be used to check for duplicate values AFTER all
> > > > entries are made into a column instead of as they are being entered? I am
> > > > running Microsoft Visual Basic 6.5 in Office 2002. I found the code below,
> > > > written by Ardus Petus, that checks for duplicate values AS THEY ARE ENTERED:
> > > >
> > > > '----------------------------------
> > > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > > 'Adjust next constant to your own needs
> > > > Const myColumn As String = "B"
> > > > Dim rng As Range
> > > > Dim Found As Range
> > > >
> > > > Set rng = Columns(myColumn)
> > > > If Intersect(Target, rng) Is Nothing Then Exit Sub
> > > > Set Found = rng.Find(Target.Value)
> > > > If Found.Address <> Target.Address Then MsgBox ("Duplicate code")
> > > > End Sub
> > > > '-----------------------------------
> > > >

 
Reply With Quote
 
Freddy
Guest
Posts: n/a
 
      27th May 2009
Tested successfully. I removed "rng.Interior.ColorIndex = xlNone". Would it
be possible to increase the range from one column to multiple columns?

"Jacob Skaria" wrote:

> Typo...corrected..
>
> Sub Macro()
> 'Adjust next constant to your own needs
> Const myColumn As String = "B"
> Dim rng As Range
> Dim cell As Range
> Dim Found As Range
> Dim blnFound as Boolean
>
> lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> rng.Interior.ColorIndex = xlNone
> For Each cell In rng
> If cell.Text <> "" And IsError(cell.Value) <> True Then
> Set Found = rng.Find(cell.Value)
> If Not Found Is Nothing Then
> If Found.Address <> cell.Address Then
> cell.Interior.Color = vbRed:blnFound = True
> End If
> End If
> End If
> Next
>
> If blnFound = True then Msgbox "Duplicates Found"
>
> End Sub
>
> --
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Jacob Skaria" wrote:
>
> > Untested...Try and feedback
> >
> > Sub Macro()
> > 'Adjust next constant to your own needs
> > Const myColumn As String = "B"
> > Dim rng As Range
> > Dim cell As Range
> > Dim Found As Range
> > Dim blnCount as Boolean
> >
> > lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> > Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> > rng.Interior.ColorIndex = xlNone
> > For Each cell In rng
> > If cell.Text <> "" And IsError(cell.Value) <> True Then
> > Set Found = rng.Find(cell.Value)
> > If Not Found Is Nothing Then
> > If Found.Address <> cell.Address Then
> > cell.Interior.Color = vbRed:blnFound = True
> > End If
> > End If
> > End If
> > Next
> >
> > If blnFound = True then Msgbox "Duplicates Found"
> >
> > End Sub
> > --
> > If this post helps click Yes
> > ---------------
> > Jacob Skaria
> >
> >
> > "Freddy" wrote:
> >
> > > It worked very well. Can it be modified to inform the user whether or not
> > > duplicates were found and, if necessary, make corrections then rerun the
> > > macro to check for duplicates again?
> > >
> > > "Jacob Skaria" wrote:
> > >
> > > > Modified...Try the below and feedback
> > > >
> > > > Sub Macro()
> > > > 'Adjust next constant to your own needs
> > > > Const myColumn As String = "B"
> > > > Dim rng As Range
> > > > Dim cell As Range
> > > > Dim Found As Range
> > > >
> > > > lngLastRow = ActiveSheet.Cells(Rows.Count, myColumn).End(xlUp).Row
> > > > Set rng = Range(mycolumn & "1:" & mycolumn & lngLastRow)
> > > > For Each cell In rng
> > > > If cell.Text <> "" And IsError(cell.Value) <> True Then
> > > > Set Found = rng.Find(cell.Value)
> > > > If Not Found Is Nothing Then
> > > > If Found.Address <> cell.Address Then cell.Interior.Color = vbRed
> > > > End If
> > > > End If
> > > > Next
> > > >
> > > > End Sub
> > > > --
> > > > If this post helps click Yes
> > > > ---------------
> > > > Jacob Skaria
> > > >
> > > >
> > > > "Freddy" wrote:
> > > >
> > > > > What VB macro code can be used to check for duplicate values AFTER all
> > > > > entries are made into a column instead of as they are being entered? I am
> > > > > running Microsoft Visual Basic 6.5 in Office 2002. I found the code below,
> > > > > written by Ardus Petus, that checks for duplicate values AS THEY ARE ENTERED:
> > > > >
> > > > > '----------------------------------
> > > > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > > > 'Adjust next constant to your own needs
> > > > > Const myColumn As String = "B"
> > > > > Dim rng As Range
> > > > > Dim Found As Range
> > > > >
> > > > > Set rng = Columns(myColumn)
> > > > > If Intersect(Target, rng) Is Nothing Then Exit Sub
> > > > > Set Found = rng.Find(Target.Value)
> > > > > If Found.Address <> Target.Address Then MsgBox ("Duplicate code")
> > > > > End Sub
> > > > > '-----------------------------------
> > > > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check for duplicate values within a column Andrew Microsoft Excel Programming 1 24th Sep 2007 03:53 PM
Check for Duplicate Values on Form to Edit Them sword856@yahoo.com Microsoft Access 0 30th Jul 2007 05:48 PM
Check for duplicate values? Ed Microsoft Excel Programming 7 9th Jan 2006 04:29 PM
Check for duplicate values before update =?Utf-8?B?Qm9iIE11bGxlbg==?= Microsoft Access Form Coding 1 10th Jun 2004 02:34 AM
How to check for duplicate values in a field Surinder Kumar Arora ITS GM EAST MTNL Delhi Microsoft Access Forms 1 24th Aug 2003 01:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:52 PM.