How do i retrieve the values from a combo box control using VBA?

keithaul

New Member
Joined
Mar 27, 2015
Messages
12
I have a user form where the user must type in a new value into the combobox control on the form. I want to be able to validate the entry to make sure it doesn't already exist in the list. There is a add button that adds the new value to the list

So I'm trying to add code behind the 'add' button to compare the entry made by the user to the values already in the combo box list. However I'm unsure how to extract the values in the list from the combo box and then compare those values to the value the user entered in the combo box. The following code is started but I don't know the syntax to get the values from the list to compare:

For Each cCatg In ws.Range("CategoryList")
With Me.cboCatgList

If Trim(Me.cboCatgList.Value) = *** what do I put here **** Then
Me.cboCatgList.SetFocus
MsgBox "This value already exists"
Exit Sub
End If
End With
Next cCatg
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi keithaul,

Here's one possibility:

Code:
On Error Resume Next
    'The following message will only appear if the Me.CatgList.Value is matched in the first column of the 'CategoryList' named range.
    'Change to suit if necessary.
    MsgBox Evaluate("VLOOKUP(""" & Me.CatgList.Value & """,CategoryList,1,FALSE)") & " has already been entered.", vbExclamation
    If Err.Number <> 0 Then
        'Code here to add record as it doesn't exist in the list
    End If
On Error GoTo 0

Regards,

Robert
 
Upvote 0
Hi Keith,

Thanks for the feedback and you're welcome ;)

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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