View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Greg Wilson[_4_] Greg Wilson[_4_] is offline
external usenet poster
 
Posts: 218
Default Question about ComboBox/Userform/TextBox etc.

I answered only because you seemed to be getting ignored.
I'm definately no expert. It did work as you requested in
my trials. To run the demo:

1) Delete existing ControlSource and Value settings for
the combo box if they are set.
2) The code assumes only 5 text boxes. These are named
TextBox1 through TextBox5. The combo box name is assumed
to be ComboBox1. Change the code or the control names to
suit.
3) It is assumed that the table of names starts in Cell
A5. Change to the code to suit.
4) Paste the below code to the UF code module.
5) Correct for word wrap.

'Module level declarations
Dim Rng As Range, Rw As Long, Found As Boolean

Private Sub UserForm_Activate()
Rw = Range("A65536").End(xlUp).Row
Set Rng = ActiveSheet.Range(Cells(5, 1), Cells(Rw, 1))
ComboBox1.RowSource = Rng.Address
End Sub

Private Sub ComboBox1_Change()
Dim i As Long, ii As Long
Found = False
For i = 0 To ComboBox1.ListCount - 1
If ComboBox1.List(i) = ComboBox1.Text Then
Found = True
Exit For
End If
Next
If Found Then
For ii = 1 To 5
Controls("TextBox" & ii).Text = Cells(Rng(i +
1).Row, ii + 1)
Next
End If
End Sub

Private Sub ComboBox1_Exit(ByVal Cancel As
MSForms.ReturnBoolean)
Dim i As Long, ii As Long
If Not Found Then
Cells(Rw + 1, 1).Value = ComboBox1.Value
For i = 1 To 5
Cells(Rw + 1, i + 1) = Controls("TextBox" &
i).Text
Next
End If
Rw = Range("A65536").End(xlUp).Row
Set Rng = ActiveSheet.Range(Cells(1, 1), Cells(Rw, 1))
ComboBox1.RowSource = Rng.Address
End Sub

Re your other post "Timing problem can someone . . . "

I think the answer is simply to use the statement
Application.EnableEvents = False before transfering the
data. Then set it to True immediately after. This should
stop the workbook from responding to the cell changes
resulting from the data transfer. Therefore, the UF won't
be repeatedly called. This is application level code so I
assume it will work. I havn't bothered to test it so I
answered it here to avoid nixing your chance of getting a
better answer.

Regards,
Greg