MAcro to add new data and check for duplicates

mike_ate_a_pie

Board Regular
Joined
Sep 25, 2009
Messages
69
Hi all,

I am trying to find a macro that will enable data to be enetered to a list. The list will then be sorted smallest to biggest and only unique entries will be allowed to be added.

For example:

<table style="border-collapse: collapse;" width="274" border="0" cellpadding="0" cellspacing="0" height="197"><col style="width: 56pt;" width="74"> <col style="width: 86pt;" width="115"> <tbody><tr style="height: 15.75pt;" height="21"> <td style="height: 15.75pt; width: 56pt;" width="74" height="21">enter code</td> <td style="width: 86pt;" width="115">enter description</td> </tr> <tr style="height: 15.75pt;" height="21"> <td class="xl64" style="height: 15.75pt;" height="21">103008</td> <td class="xl64" style="border-left: medium none;">ryan</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">
</td> <td>
</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">Code</td> <td class="xl63">Description</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">102001</td> <td class="xl63">mike</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">102006</td> <td class="xl63">john</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">102009</td> <td class="xl63">sam</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">103001</td> <td class="xl63">pete</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">104002</td> <td class="xl63">gary</td> </tr> </tbody></table>
If i entered 103008 and ryan this would be added to the list below in code order (below pete). However if i tried to add a code already in the list, eg - 102006, john, it would create an error box.

Does anybody know if this is possible or a simple way to do this?

Thanks in advance

Mike :)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I havent written any code as yet. I am only a begineer at VBA, i can record the macro to enter and sort the data into the list but the duplication is causing me problems and this probably needs to be written.

Therfore I think its probably best to start from scratch.

Cheers
 
Upvote 0
You could write a macro that scans the list for a match, or alternatively, you could just do a formula next to the input range that will tell you if there is a match. Then make the "add data" macro exit with an error message if the formula shows the data is already in the list. ie.

Formula:
=IF(ISERROR(MATCH([INPUT CELL],[RANGE TO COMPARE TO],0)),"","Already in list")

Macro:
If Range("[CELL W/ ABOVE FORMULA]") = "Already in List" then
MsgBox "This customer cannot be added because the record already exists",vbOKOnly,"Error"
Exit Sub
Else
'Run your code to insert and sort
End If

One benefit of this method is that it provides instant feedback rather than waiting until the macro is run.
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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