View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
atpgroups atpgroups is offline
external usenet poster
 
Posts: 40
Default auto add to list

On 19 May, 18:29, Gizmo wrote:

My app works fine if I "preload" the ModSN list before the user adds data.
What I am trying to do is allow NEW ModSN's to be added by the user by
typing them into the combobox.


You need to add the newly entered data into the .List property field
of the combobox. Unfortunately that will be transient, anything added
will not be there next time you open the spreadsheet. One solution is
to keep the data in a hidden sheet. In the example below I have used
Sheet1, but you almost certainly want to change that. Hidden sheets
are a good place to keep data that VBA Macros need to work with.
The secret is in the combobox.List property and the Combobox.Add
method.
I have assumed two buttons, bAdd and bDel which add and remove entries
from the list. You could, however, put the bAdd code in the
ComboBox.Keydown event to add on Enter=key press as in the earlier
example.

In frmProcessDataEntry you need the following code (again, beware of
line breaks)

Private Sub bAdd_Click()
Dim i As Integer
For i = 0 To cbModSN.ListCount - 1
If cbModSN.List(i, 0) = cbModSN.Text Then 'entry already exists
Beep
Exit Sub
End If
Next
cbModSN.AddItem cbModSN.Text
cbModSN.Text = ""
cbModSN.SetFocus ' move the focus back to the box
End Sub


Private Sub bDel_Click()
Dim i As Integer
For i = 0 To cbModSN.ListCount - 1
If cbModSN.List(i, 0) = cbModSN.Text Then 'entry found
cbModSN.RemoveItem (i)
cbModSN.Text = ""
cbModSN.SetFocus ' move the focus back to the box
Exit Sub
End If
Next
Beep ' entry not found
End Sub


Private Sub UserForm_Activate()
UserForm2.ComboBox1.List = Sheet1.Range("A1",
Sheet1.Range("A65535").End(xlUp)).Value
End Sub


Private Sub UserForm_Deactivate()
'Store the values
Sheet1.Cells.Clear
Sheet1.Range("A1").Resize(cbModSN.ListCount + 1, 1).Value =
cbModSN.List
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
'Store the values
Sheet1.Cells.Clear
Sheet1.Range("A1").Resize(cbModSN.ListCount + 1, 1).Value =
cbModSN.List
End Sub


And then in your other userform (Userform2)

Private Sub UserForm_Activate()
UserForm2.ComboBox1.List = Sheet1.Range("A1",
Sheet1.Range("A65535").End(xlUp)).Value
End Sub