1

I have a combobox in Excel which is populated with values from a DB. There are 2 columns with the 2nd column bound and linked to a cell.

I want users to be able to input their own value.

If a user selects an item from the list, everything works great. I get the value in the linked cell.

However, if the user inputs their own data i get a #N/A value in the linked cell.

Is there any way where I can retrieve the value inputted by the user?

Thanks in advance

7
  • There will be a _change event on the combo box. Or you can monitor the keypress event for an enter.
    – MatthewD
    Aug 26, 2015 at 14:54
  • Can you explain why the ComboBox has 2 columns?
    – DragonSamu
    Aug 26, 2015 at 15:16
  • @MatthewD I can use the_change or _keypress event, but I dont know how to get the actual value of what the user has inputted. The combobox.value just returns the latest selected item from the list. @ Dragon Samu its just for practical purposes that I have 2 coloumns. The 1st column holds a text and the 2nd column holds a DB id (hidden from the user). However, that should not change my problem of getting the value of the user input. Aug 26, 2015 at 17:22
  • 2
    Look at the ComboBox2.Text vs the ComboBox2.Value There is a difference. Not sure if it will return what you are looking for but take a look. stackoverflow.com/questions/2844193/…
    – MatthewD
    Aug 26, 2015 at 17:26
  • I just tested both .value and .text at a different computer, and both worked perfectly. Could that have something to do with the versions of excel? Anyway, thanks a lot for the feedback. It was driving me crazy :) Aug 26, 2015 at 17:38

1 Answer 1

0

Strange that the text or value. You will have to access them after update. But you could watch what is being entered and keep track of it that way.

Declare a global variable to hold what is entered.

Private strComboEntry As String

Then watch what is being keyed in.

Private Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim strKey As String
    strKey = Chr(KeyAscii)
    strComboEntry = strComboEntry & strKey
End Sub

We will have to handle the backspace and delete too.

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 8 Then
        strComboEntry = Left(strComboEntry, Len(strComboEntry) - 1)
        Exit Sub
    End If

End Sub

EDIT: OP says this comment got him the answer.
Look at the ComboBox2.Text vs the ComboBox2.Value There is a difference. Not sure if it will return what you are looking for but take a look. Distinction between using .text and .value in VBA Access

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.