ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Texbox values based on Combobox Value(more detail) (https://www.excelbanter.com/excel-programming/282099-re-texbox-values-based-combobox-value-more-detail.html)

Todd Huttenstine[_2_]

Texbox values based on Combobox Value(more detail)
 
Hey Tom,

I finally got it to work. Thank you for your help. I had
to remove the Option Explicit part. That was the reason I
was receiving the error. Below is what I removed from the
top of the entire module:

*******
Option Explicit
Dim cLastRow As Long
Dim i As Long
********

Instead of using the Option Explicit, I just copied the 2
lines of code to each of the 3 OptionButton code (which it
pertains to). Do you know why when I removed the Option
Explicit, it worked?

Below is the code you wrtoe that I am using.
"Here is the second piece of code, copied out of a working
module:"

Private Sub Combobox1_Click()

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.Text = 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


End Sub


Thanx again,

Todd

Tom Ogilvy

Texbox values based on Combobox Value(more detail)
 
Option Explicit requires all variables to be declared - always a good
practice. If you want it in your code you need to declare variables such as
this:

Private Sub Combobox1_Click()
dim rng as Range
dim cell as Range
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.Text = 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
End Sub

--
Regards,
Tom Ogilvy


Todd Huttenstine wrote in message
...
Hey Tom,

I finally got it to work. Thank you for your help. I had
to remove the Option Explicit part. That was the reason I
was receiving the error. Below is what I removed from the
top of the entire module:

*******
Option Explicit
Dim cLastRow As Long
Dim i As Long
********

Instead of using the Option Explicit, I just copied the 2
lines of code to each of the 3 OptionButton code (which it
pertains to). Do you know why when I removed the Option
Explicit, it worked?

Below is the code you wrtoe that I am using.
"Here is the second piece of code, copied out of a working
module:"

Private Sub Combobox1_Click()

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.Text = 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


End Sub


Thanx again,

Todd





All times are GMT +1. The time now is 03:55 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com