View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Getting input from combobox to update sheet


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

--
Regards,

OssieMac