Consulting

Results 1 to 5 of 5

Thread: Difficulties with a userform

  1. #1
    VBAX Regular
    Joined
    Nov 2010
    Posts
    14
    Location

    Difficulties with a userform

    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

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Run fine for me, I am using Excel 2010. What error are you getting?

  3. #3
    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
        
        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
        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

  4. #4
    VBAX Regular
    Joined
    Nov 2010
    Posts
    14
    Location
    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

    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

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [vba]
    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

    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •