View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Texbox values based on Combobox Value(more detail)

Note that setting ListIndex to 0 in your option button code, that the user
won't be able to select the first item in the list.


if combobox1.Listindex < -1 then
With worksheets("Sheet2")
for each cell in .Range("A2:C52")
if cell.Value = Combobox1.Value then
Textbox1.Value = .Cells(cell.row,1).Value
Textbox2.Value = .Cells(cell.row,2).Value
Textbox3.Value = .Cells(cell.row,3).Value
exit for
end if
Next
End With
end if

or
If combobox1.Listindex < -1 then
With Worksheets("sheet2")
if OptionButton1.Value then
set rng = .Range("A2:A52")
elseif OptionButton2.Value then
set rng = .Range("B2:B52")
else
set rng = .Range("C2:C52")
End if
For each cell in rng
if cell.Value = combobox1.Value then
Textbox1.Value = .Cells(cell.row,1).Value
Textbox2.Value = .Cells(cell.row,2).Value
Textbox3.Value = .Cells(cell.row,3).Value
exit for
end if
Next
End with
End If


--
Regards,
Tom Ogilvy


Todd Huttenstine wrote in message
...
Im sorry but I dont have any code for this so far, however
I do have code at the bottom of this post for when I click
the 3 Option Buttons and ComboBox1 auto-populates with
data from the corresponding columns on Sheet2 Range A2:C52.


I have a userform that contains TextBox1, TextBox2,
TextBox3, OptionButton1, OptionButton2, OptionButton3, and
ComboBox1.

I have a table of data on Sheet2 Range A2:C52.

When I select an option button, it auto-populates
ComboBox1 with the corresponding data in the columns in
Range A2:C52.
OptionButton1 is corresponding with data in columnA,
OptionButton2 is corresponding with data in columnB, and
OptionButton3 is corresponding with data in columnC.

Now, I need a code that will match the value in ComboBox1
with a value in Range A2:C52 on Sheet2. If a match is NOT
found, then do nothing(this will never happen because
anything that is in ComboBox1 will be found in the
range). If there IS a match with the value in ComboBox1
(which there always will be), then I need for the code to
put the corresponding data in the corresponding
TextBoxes. The way it will do this is as follows:

If the match is in a cell in Column A, then I need it to
put the data in that cell in Column A into TextBox1, and
then the corresponding data in the other 2 cells in the
columns into the corresponding 2 Textboxes.

If the match is in a cell in Column B, then I need it to
put the data in that cell in Column B into TextBox2 and
then the corresponding data in the other 2 cells in the
columns into the corresponding 2 Textboxes.

If the match is in a cell in Column C, then I need it to
put the data in that cell in Column C into TextBox3 and
then the corresponding data in the other 2 cells in the
columns into the corresponding 2 Textboxes.

Here is some info that may help...
Below is the code I am using for when I click the 3 Option
Buttons and ComboBox1 auto-populates with data from the
corresponding columns on Sheet2 Range A2:C52...

Private Sub OptionButton1_Click()
cLastRow = Worksheets(2).Cells(Rows.Count, "A").End
(xlUp).Row
With ComboBox1
.Clear
For i = 3 To cLastRow + 1
If Cells(i, "A") < "" Then
.AddItem Worksheets(2).Cells(i -
1, "A").Value
End If
Next i
.ListIndex = 0
End With
End Sub

Private Sub OptionButton2_Click()
cLastRow = Worksheets(2).Cells(Rows.Count, "B").End
(xlUp).Row
With ComboBox1
.Clear
For i = 3 To cLastRow + 1
If Cells(i, "B") < "" Then
.AddItem Worksheets(2).Cells(i -
1, "B").Value
End If
Next i
.ListIndex = 0
End With
End Sub

Private Sub OptionButton3_Click()
cLastRow = Worksheets(2).Cells(Rows.Count, "C").End
(xlUp).Row
With ComboBox1
.Clear
For i = 3 To cLastRow + 1
If Cells(i, "C") < "" Then
.AddItem Worksheets(2).Cells(i -
1, "C").Value
End If
Next i
.ListIndex = 0
End With
End Sub

Thank you

Todd Huttenstine