PDA

View Full Version : Difficulties with a userform



Ako_____
11-02-2012, 02:17 PM
Hi,

I am still a rookie at VBA and I could really use a little help from some of you big brainers!

I am currently making a userform with some textboxes and comboboxes. What I want to do is to have an initial value in ComboBox1 and then let the value in TextBox1 depend on it.

Every time I try this I am getting an error unless I erase the line saying "If ComboBox1.Value = "S235" Then TextBox1.Value = 235 / 2".

Does anyone know how to solve this problem?

I would really appreciate some help :)

Private Sub UserForm_initialize()

With ComboBox1
.AddItem "S235"
.AddItem "S275"
.AddItem "S355"
End With
ComboBox1.ListIndex = 0
ComboBox1.Style = fmStyleDropDownList

End Sub

Private Sub ComboBox1_Change()

If ComboBox1.Value = "S235" Then TextBox1.Value = 235 / 2
If ComboBox1.Value = "S275" Then TextBox1.Value = 275 / 2
If ComboBox1.Value = "S355" Then TextBox1.Value = 355 / 2

End Sub

JKwan
11-02-2012, 06:29 PM
Run fine for me, I am using Excel 2010. What error are you getting?

Trebor76
11-02-2012, 06:39 PM
Hi there,

Change the combobox control to a listbox (ListBox1 in my example) and then try this:


Option Explicit
Private Sub UserForm_initialize()
'Written by Trebor76
'Visit my website www.excelguru.net.au (http://www.excelguru.net.au)

Application.EnableEvents = False

With ListBox1
.AddItem "S235"
.AddItem "S275"
.AddItem "S355"
End With

ListBox1.ListIndex = 0

Application.EnableEvents = True

TextBox1.Value = 0

End Sub
Private Sub ListBox1_Click()
'Written by Trebor76
'Visit my website www.excelguru.net.au (http://www.excelguru.net.au)
Dim intListBoxItem As Integer

With ListBox1
For intListBoxItem = 0 To .ListCount - 1
If .Selected(intListBoxItem) = True Then
TextBox1.Value = Val(Mid(.List(intListBoxItem), 2, 3)) / 2
Exit For
End If
Next intListBoxItem
End With
End Sub

Regards,

Robert

Ako_____
11-03-2012, 09:58 AM
Hi,

Thank you very much for your replies!

When I wrote last time I had simplified my code a little before I posted it. In fact I want to divide 235, 275 or 355 with a value in a textbox as shown below.

When I got your replies I tried to do as you suggested but it still would not work.
After that I had to rethink my code and it turned out that the problem was that I was dividing with a value in a textbox. After some trial and error it I found out that the solution was just to make it a dropbuttonclick.

Thanks again for your replies! Sometimes all it takes is to have someone else looking :thumb

Private Sub ComboBox1_DropButtonClick()

If ComboBox1.Value = "S235" Then TextBox1.Value = 235 / TextBox2.Value

If ComboBox1.Value = "S275" Then TextBox1.Value = 275 / TextBox2.Value

If ComboBox1.Value = "S355" Then TextBox1.Value = 355 / TextBox2.Value

End Sub

snb
11-03-2012, 02:27 PM
Private Sub UserForm_initialize()
ComboBox1.list=split("S235|S275|S355")
End Sub

Private Sub ComboBox1_Change()
if combobox1.listindex>-1 then TextBox1.Value = Val(mid(combobox1.value,2)) / Textbox2.value
End Sub