Preventing duplicates

Gregm66

Board Regular
Joined
Jan 23, 2016
Messages
170
Hi everyone,
I am using a userform to add data to my worksheet, my userform uses a combobox to to show the list that exsists but also allows for items to be added to my list. My problem is that everytime i select or add an item it continues to create duplicates please code below that i am using, is there a way to prevent the duplicates happening on the worksheet via the code and also display a message if an item is typed in that already exsists in t
VBA Code:
Private Sub CmdAdd_Click()

Dim rw As Long

If CmbList = "" Then
    MsgBox "Please Add a Description!"
    CmbList.SetFocus
    Exit Sub
  End If

With Sheets("Club Ledger")
    Application.ScreenUpdating = False
  
rw = .Range("B" & .Rows.Count).End(xlUp).Row + 1

.Range("B" & rw).Value = CmbList.Value
.Range("C" & rw).Value = txtCredit.Value
.Range("D" & rw).Value = txtDebit.Value

    With Sheets("PROPS")
    
rw = .Range("F" & .Rows.Count).End(xlUp).Row + 1

.Range("F" & rw).Value = CmbList.Value

    Application.ScreenUpdating = True

    End With
End With
'================================
  'Clear the text boxes
  '================================
  CmbList.Value = ""
  txtCredit.Value = ""
  txtDebit.Value = ""
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
OK try this one:
VBA Code:
Private Sub CmdAdd_Click()


Dim rw As Long


If CmbList = "" Then

    MsgBox "Please Add a Description!"

    CmbList.SetFocus

    Exit Sub

End If

Application.ScreenUpdating = False

With Sheets("Club Ledger")

    rw = .Range("B" & .Rows.Count).End(xlUp).Row + 1
    
    .Range("B" & rw).Value = CmbList.Value
    
    .Range("C" & rw).Value = txtCredit.Value
    
    .Range("D" & rw).Value = txtDebit.Value


End With

    If IsNumeric(Application.Match(CmbList.Value, CmbList.List, 0)) Then
       
       MsgBox CmbList.Value & " already exist"

    Else
        With Sheets("PROPS")
    
            rw = .Range("F" & .Rows.Count).End(xlUp).Row + 1
            .Range("F" & rw).Value = CmbList.Value
            
            
        End With

    End If

Application.ScreenUpdating = True

'================================

  'Clear the text boxes

  '================================

  CmbList.Value = ""

  txtCredit.Value = ""

  txtDebit.Value = ""

End Sub
 
Upvote 0
when i type a new entry into the combobox i get an error, "Invalid Property Value"
so i take it that it wont allow me to add a new item
 
Upvote 0
All good Akuini,
It was a setting in propertys i just fixed it, now it is working 100% Correctly
Thankyou very much for all of your help, Problem solved....

Cheers Thankyou
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
One thing though:
I think you need to repopulate the combobox list after inserting a new value in colomn F.
Maybe something like this:
Just add the blue line.

Rich (BB code):
        With Sheets("PROPS")


            rw = .Range("F" & .Rows.Count).End(xlUp).Row + 1

            .Range("F" & rw).Value = CmbList.Value

            Me.CmbList.List = Worksheets("PROPS").Range("f2:f" & rw + 1).Value


        End With
 
Upvote 0

Forum statistics

Threads
1,215,558
Messages
6,125,511
Members
449,236
Latest member
Afua

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