Getting input from combobox to update sheet

J

John G.

In a userform I'm have a Combobox (CB) that holds names. When the routine is
first run list for the CB is empty. The user then inputs a name and this is
to be stored for subsequent uses of the workbook. Any other time the form is
opened, the ListIndex = 0 will show the user's name for confirmation.

The following is another option that I'm contemplating but I need to get the
single entry done first: "If another user needs to, they can input their
name and it is then added to the top of the list, so that the next time the
form is opened this new entry will be the default name shown on the form and
the previous entries would show in the dropdown as secondary names."

It is my understanding that manual input into a combobox can be stored back
to a sheet, preferably the list for the combobox.

Can't seem to find a way to start this that works. I tried using CB_Exit to
allow for input but it doesn't copy the input back to the named range on the
sheet. Also, I don't seem to getting the new entry into the value property
after it's typed in.

--------------------------------------------------
' CBName is the combobox and Cardholder is the named range

Private Sub CBName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim CHName As String
CHName = Sheets("Carddata").Range("Cardholder").Cells(1, 1).Value
If CHName = "" Then
CHName = Me.CBName.Value
End If
End Sub
----------------------------------------------------
 
J

Jacob Skaria

Use the Before Update event of the control to store data; like the below. Try
and feedback..

Private Sub CBName_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If WorksheetFunction.CountIf(Sheets("Carddata"). _
Range("A:A"), ComboBox1.Text) = 0 Then
lngRow = Sheets("Carddata").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Carddata").Range("A" & lngRow) = CBName.Text
End If
End Sub

If this post helps click Yes
 
J

Jacob Skaria

Private Sub CBName_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If WorksheetFunction.CountIf(Sheets("Carddata"). _
Range("A:A"), CBName.Text) = 0 Then
lngRow = Sheets("Carddata").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Carddata").Range("A" & lngRow) = CBName.Text
End If
End Sub


If this post helps click Yes
 
O

OssieMac

Hi John,

With the following code you need to initially name one cell only as
Cardholder. The code assumes this cell to be Cell A1 on worksheet Carddata.

As the names are added in the combo box, if not already existing, they are
added to the top of the list and the named range for the combo is redefined
with the expanded range.

The RowSource property for the combo needs to be set to Cardholder. (Can't
do this until after cell is named Cardholder.)

The MatchRequired property for the combo needs to be false.

Do a Find and Replace on the following code to change the sub name from
ComboBox1 to match the name of your combo box.

Private Sub ComboBox1_AfterUpdate()
Dim comboEntry As Variant
Dim rngTofind As Range
Dim lngRows As Long

'Save the Combo box entry to a variable
comboEntry = ComboBox1.Value

'Test if combo box entry exists in Rowsource
With Sheets("Carddata")
Set rngTofind = .Range("Cardholder") _
.Find(What:=comboEntry, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
End With

'If entry not found then add it to named range
If rngTofind Is Nothing Then
lngRows = ComboBox1.ListCount + 1

With Sheets("Carddata")
'Move existing Cardholder range down 1 cell
'to allow new entry to be at top of range.
.Range("Cardholder").Cut _
Destination:=.Range("A2")

'Redefine Cardholder range to include
'additional cell for new combo box entry.
.Range(.Cells(1, 1), _
.Cells(lngRows, 1)) _
.Name = "Cardholder"

'Populate the first cell in the named range
'with the new combo box entry.
.Cells(1, 1) = comboEntry
End With

'Update the Rowsource for the combo box
ComboBox1.RowSource = ("Carddata!Cardholder")
End If

End Sub
 
J

John G.

Thanks Jacob and James for the suggestions and advice...I'll let you when I
get it working.
 
J

John G.

OssieMac...This worked with just one small change. Kept getting a "NULL"
error when I got to CBName.Value. Changed it to CBName.Text and it worked
great including the inserting of new names.

Thanks
 
O

OssieMac

Hi again John,

I am assuming that it is this line giving you the problem.
comboEntry = ComboBox1.Value
I tested the code in xl2007 and xl2002 and no problems. However, the code
also works with comboEntry = ComboBox1.Text.

Having said that, I have found another problem. If you define the name for
the first cell and leave the first cell empty then the empty cell gets pushed
down to allow the next new entry and it is continually pushed down and this
gives you a blank line at the bottom of the RowSource. The following modified
code tests for a blank first cell and if blank it does not push the list
down. (I have changed Value to Text).

Don't forget to use Find/Replace for ComboBox1.

Private Sub ComboBox1_AfterUpdate()
Dim comboEntry As Variant
Dim rngTofind As Range
Dim lngRows As Long

'Save the Combo box entry to a variable
comboEntry = ComboBox1.Text

'Test if combo box entry exists in Rowsource
With Sheets("Carddata")
Set rngTofind = .Range("Cardholder") _
.Find(What:=comboEntry, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
End With

'If entry not found then add it to named range
If rngTofind Is Nothing Then
lngRows = ComboBox1.ListCount + 1

With Sheets("Carddata")
'Move existing Cardholder range down 1 cell
'to allow new entry to be at top of range.
'(Move only if 1st cell in not blank.)
If .Range("Cardholder").Cells(1, 1) <> "" Then
.Range("Cardholder").Cut _
Destination:=.Range("A2")

'Redefine Cardholder range to include
'additional cell for new combo box entry.
.Range(.Cells(1, 1), _
.Cells(lngRows, 1)) _
.Name = "Cardholder"
End If

'Populate the first cell in the named range
'with the new combo box entry.
.Cells(1, 1) = comboEntry
End With

'Update the Rowsource for the combo box
ComboBox1.RowSource = ("Carddata!Cardholder")
End If

End Sub
 
J

John G.

Thx OssieMac...I noticed that when I was ran it. Really appreciate the
update. JG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top