Basic duplicate needed please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I am looking for a basic duplicate code so when i leave a specific cell the code would run.

So my new code should work like this.

Worksheet is called DATABASE
Cell where i would type the customers name is always A6
Once i leave cell A6 the code should check for a duplicate in only the Column range A6 & down the page.
If no duplicate is found then allow me to continue.
If a duplicate is found then show a msgbox yes / no asking would i like to be taken to that cell where the duplicate was found.
No just closes the msgbox where yes would take me to the cell where the duplicate is.

Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

When you enter a value in Range("A6") the script will run

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  8/6/2019  11:46:11 AM  EDT
If Target.Address = Range("A6").Address Then
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim SearchString As String
Dim SearchRange As Range
SearchString = Target.Value
Set SearchRange = Range("A7:A" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then
Else
r = SearchRange.Row
ans = MsgBox("We found a duplicate. Do you want to be taken to that row", vbYesNo)
If ans = vbYes Then Application.Goto Range("A" & r)
End If
End If
End Sub
 
Upvote 0
Hi,
I allready have a Worksheet change code on my sheet already see below,

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    With Target
        If .Column = 13 Then Exit Sub
        If .Count = 1 And Not .HasFormula Then
            Application.EnableEvents = False
            .Value = UCase(.Value)
            Application.EnableEvents = True
        End If
    End With
End Sub

So if ive merged the two correctly i then have this.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)'Modified  8/6/2019  11:46:11 AM  EDT
If Target.Address = Range("A6").Address Then
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim SearchString As String
Dim SearchRange As Range
SearchString = Target.Value
Set SearchRange = Range("A7:A" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then
Else
r = SearchRange.Row
ans = MsgBox("We found a duplicate. Do you want to be taken to that row", vbYesNo)
If ans = vbYes Then Application.Goto Range("A" & r)
    With Target
        If .Column = 13 Then Exit Sub
        If .Count = 1 And Not .HasFormula Then
            Application.EnableEvents = False
            .Value = UCase(.Value)
            Application.EnableEvents = True
        End If
    End With
End If
End If
End Sub

But after typing in cell A6 and going to cell B6 i see this this message.
Variable not defined & this below in yellow.

r = SearchRange.Row
 
Upvote 0
hi,

Just looking at the existing worksheet change im not sure as i dont see what its refering to.
 
Upvote 0
I just removed my code and just used your advised code but i still get variable not defined.

This part is in yellow.

Code:
r = SearchRange.Row
 
Upvote 0
Apart from changing to uppercase i dont understand what the code refers to =13 / =1 ???

we could run with your code & uppercase and as i use it would see something out of place i think
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  8/6/2019  1:32:22 PM  EDT
If Target.Address = Range("A6").Address Then
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim SearchString As String
Dim SearchRange As Range
Dim r As Long
SearchString = Target.Value
Set SearchRange = Range("A7:A" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then
Else
r = SearchRange.Row
ans = MsgBox("We found a duplcate. Do you want to be taken to that row", vbYesNo)
If ans = vbYes Then Application.Goto Range("A" & r)
End If
End If
End Sub
 
Upvote 0
Same error message but down a line now.

Code:
r = SearchRange.Row
[COLOR=#b22222]ans[/COLOR] = MsgBox("We found a duplcate. Do you want to be taken to that row", vbYesNo)
If ans = vbYes Then Application.Goto Range("A" & r)
End If
End If
End Sub
 
Upvote 0
Well it works for me but try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  8/6/2019  1:50:22 PM  EDT
If Target.Address = Range("A6").Address Then
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim SearchString As String
Dim SearchRange As Range
Dim r As Long
Dim ans As Variant
SearchString = Target.Value
Set SearchRange = Range("A7:A" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then
Else
r = SearchRange.Row
ans = MsgBox("We found a duplcate. Do you want to be taken to that row", vbYesNo)
If ans = vbYes Then Application.Goto Range("A" & r)
End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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