VBA preventing adding a name to a list if the name is already in the list

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I use the following code to enter a name selected from a listbox to the end of a list of names named "nameList"
I please need some help to prevent the name being added if it already appears in the list and a messagebox to appear advising that the name was not added because it is already in the list, can someone please help. After the messagebox appears, the code will continue running - it must not exit the subroutine.
> Application.Goto Reference:="R1000C3"
Range(ActiveCell, ActiveCell.End(xlUp)).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Unprotect
ActiveCell.FormulaLocal = ListBox1.Value <
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Re: VBA preventuing adding a name to a list if the name is already in the list

For example:
Code:
If Application.WorksheetFunction.CountIf(Range("nameList"), ListBox1.Value) = 0 Then
    ActiveCell.FormulaLocal = ListBox1.Value
Else
    'What to do if it already Exists? I don't know
End If

Consider replacing all your code with
Code:
If Application.WorksheetFunction.CountIf(Range("nameList"), ListBox1.Value) = 0 Then
    lastc = Cells(Rows.Count, "C").End(xlUp).Row
    ActiveSheet.Unprotect
    Cells(lastc + 1, "C").FormulaLocal = ListBox1.Value
Else
    'What to do if it already Exists? I don't know
End If
Bye
 
Upvote 0
Re: VBA preventuing adding a name to a list if the name is already in the list

For example:
Code:
If Application.WorksheetFunction.CountIf(Range("nameList"), ListBox1.Value) = 0 Then
    ActiveCell.FormulaLocal = ListBox1.Value
Else
    'What to do if it already Exists? I don't know
End If

Consider replacing all your code with
Code:
If Application.WorksheetFunction.CountIf(Range("nameList"), ListBox1.Value) = 0 Then
    lastc = Cells(Rows.Count, "C").End(xlUp).Row
    ActiveSheet.Unprotect
    Cells(lastc + 1, "C").FormulaLocal = ListBox1.Value
Else
    'What to do if it already Exists? I don't know
End If
Bye

Thank you - my coding comes from a bygone era and I have replaced it with your suggestion which works perfectly. thank you.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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