Add Name if it is not in the list

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guy,
Active sheet Column A can select name from a dropdown list in worksheets("Data") Column L and can input name. I want to input a name and if it is not found in the dropdown list, then add this (new)name to the last empty cell in Column L.
My code does not work but shows no error. Appreciate any help to make it works.

VBA Code:
Sub AddName2()


Dim srchRng As Range, oRng As Range
Dim M As Range, N As Range
Dim slastRow As Long, olastRow As Long
olastRow = Range("A3").End(xlDown).Row
Set oRng = Range("A3:A" & olastRow)
Set srchRng = Worksheets("Data").Range("L2:L" & slastRow)

On Error GoTo ErrHandler:

If Not Intersect(Target, Range("A3:A" & olastRow)) Is Nothing Then
   If Not IsNumeric(Target.Value) Then
      Application.EnableEvents = False
    
      
      For Each N In oRng
      Set M = srchRng.Find(Target.Value, , xlValues, xlWhole)
           If M = N Then
           Else
           Target.Value = Worksheets("Data").Range("L2:L" & slastRow + 1).Value
           End If
       Next N
      
ErrHandler:
Application.EnableEvents = True

End If
    
    Application.EnableEvents = True
    
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
if you want Normal Macro Try this:
VBA Code:
Sub AdName2()
Dim srchRng As Range, oRng As Range
Dim M As Long, N As Range
Dim slastRow As Long, olastRow As Long
olastRow = Range("A3").End(xlDown).Row
slastRow = Worksheets("Data").Range("L2").End(xlDown).Row
Set oRng = Range("A3:A" & olastRow)
Set srchRng = Range("L2:L" & slastRow)
On Error Resume Next
    For Each N In oRng
        If Not IsNumeric(N.Value) Then
            M = srchRng.Find(N.Value).Row
                If M = 0 Then
                Worksheets("Data").Range("L" & slastRow + 1).Value = N.Value
                slastRow = slastRow + 1
                Else
                M = 0
                End If
        End If
    Next N

End Sub

and if Worksheet Change event. Right click on sheet name and paste this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim srchRng As Range, oRng As Range
Dim M As Long, N As Range
Dim slastRow As Long, olastRow As Long
olastRow = Range("A3").End(xlDown).Row
slastRow = Worksheets("Data").Range("L2").End(xlDown).Row
Set oRng = Range("A3:A" & olastRow)
Set srchRng = Range("L2:L" & slastRow)
Application.EnableEvents = False
On Error Resume Next
    If Not IsNumeric(Target.Value) Then
        M = srchRng.Find(Target.Value).Row
        If M = 0 Then Worksheets("Data").Range("L" & slastRow + 1).Value = Target.Value
    End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Hi maabadi,
The code addes all names in the column to the list. I only want the new name to copy. Or it can be the active cell's value which is new when the moment I input it, the code will find it is already existed or not.
 
Upvote 0
Then Use worksheet change event macro.
Right-click on Sheet name. Select view Code and Paste it at window appears.
 
Upvote 0
Hi maabadi,
The worksheet change event mode adds old and new names to the list.
 
Upvote 0
Hi maabadi,
The worksheet change event mode adds old and new names to the list.
Set srchRng = Range("L2:L" & slastRow) S/B Set srchRng = Worksheets("Data").Range("L2:L" & slastRow)
 
Upvote 0
I want to input a name and if it is not found in the dropdown list, then add this (new)name to the last empty cell in Column L.

The worksheet change event mode adds old and new names to the list.
You told you want to add new name after last row of old names. then macro added new Names after previous names at the Column L and don't Delete OLD Names

How about This:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim srchRng As Range, oRng As Range
Dim M As Long, N As Range
Dim slastRow As Long, olastRow As Long
olastRow = Range("A3").End(xlDown).Row
slastRow = Worksheets("Data").Range("L2").End(xlDown).Row
Set oRng = Range("A3:A" & olastRow)
Set srchRng = Worksheets("Data").Range("L2:L" & slastRow)
Application.EnableEvents = False
On Error Resume Next
    If Not IsNumeric(Target.Value) Then
        M = srchRng.Find(Target.Value).Row
        If M = 0 Then Worksheets("Data").Range("L" & slastRow + 1).Value = Target.Value
    End If
Application.EnableEvents = True
End Sub
 
Upvote 0
Hi maabadi,
With few adjustments, these codes work. Thank you.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim srchRng As Range
Dim M As Long
Dim slastRow As Long, lastRow as Long
slastRow = Worksheets("Data").Range("L2").End(xlDown).Row
Set srchRng = Worksheets("Data").Range("L2:L" & slastRow)
lastRow = Range("A3").End(xlDown).Row

If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Range("A3:A" & lastRow)) Is Nothing Then
        If Not IsNumeric(Target.Value) Then
Application.EnableEvents = False
On Error Resume Next
    If Not IsNumeric(Target.Value) Then
        M = srchRng.Find(Target.Value).Row
        'M = srchRng.Find(Target.Value, , xlValues, xlWhole).Row
        'Debug.Print M
        'Debug.Print Target.Value
        If M = 0 Then Worksheets("Data").Range("L" & slastRow + 1).Value = Target.Value
    End If
Application.EnableEvents = True

End If

End sub



Sub addName24()

Dim srchRng As Range
Dim M As Long
Dim slastRow As Long

slastRow = Worksheets("Data").Range("L2").End(xlDown).Row
Set srchRng = Worksheets("Data").Range("L2:L" & slastRow)

Application.EnableEvents = False
On Error Resume Next
    If Not IsNumeric(ActiveCell.Value) Then
        M = srchRng.Find(ActiveCell.Value).Row
        'M = srchRng.Find(Target.Value, , xlValues, xlWhole).Row
        Debug.Print M
        Debug.Print ActiveCell.Value
        If M = 0 Then Worksheets("Data").Range("L" & slastRow + 1).Value = ActiveCell.Value
    End If
Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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