+ Reply to Thread
Results 1 to 9 of 9

Change combobox list based on option button

Hybrid View

  1. #1
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,061

    Change combobox list based on option button

    I have two buttons:

    Private Sub Optionbutton1_Change()
    Call UserForm_Initialize
    End Sub
    
    Private Sub Optionbutton2_Change()
    Call UserForm_Initialize
    End Sub
    And code below:

    Private Sub UserForm_Initialize()
    Dim cPart As Range
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    Dim optbut As Integer
    
    If Optionbutton2 Then 
    
            For Each cPart In ws1.Range("List1")
          With Me.CmbSiteName
            .AddItem cPart.Value
            .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
          End With
        Next cPart
    
    
    ElseIf Optionbutton1 Then
    
        For Each cPart In ws2.Range("List2")
          With Me.CmbSiteName
            .AddItem cPart.Value
            .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
          End With
        Next cPart
        
    End If
    
    Me.CmbSiteName.SetFocus
    End Sub
    By default:
    Optionbutton1=False
    Optionbutton2=True

    and 3 steps:

    When I run User form - it's OK (looking at List 1)
    When I click Optionbutton1 - it's OK (looking at list 2)
    When I now click back to Optionbutton2 it remains at List 2.. Why doesn't switch to List1?
    Last edited by zbor; 05-09-2012 at 09:50 AM.
    Never use Merged Cells in Excel

  2. #2
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Change combobox list based on option button

    could you attach workbook with dummy data ?

    I modified your code into this, I hope it is what you need
    Private Sub UserForm_Initialize()
    Dim cPart As Range
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    Dim optbut As Integer
    
    cmbSiteName.Clear 'clear combobox before update
    
    If OptionButton2 Then
    
        'old
        'For Each cPart In ws1.Range("List1")
        '  With Me.cmbSiteName
        '    .AddItem cPart.Value
        '    .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
        '  End With
        'Next cPart
    
        'new
        With cmbSiteName
            .ColumnCount = 2
            .List = ws1.Range("list1").Value
        End With
    
    ElseIf OptionButton1 Then
    
        'old
        'For Each cPart In ws2.Range("List2")
        '  With Me.cmbSiteName
        '    .AddItem cPart.Value
        '    .List(.ListCount - 1, 1) = cPart.Offset(0, 1).Value
        '  End With
        'Next cPart
        
        'new
        With cmbSiteName
            .ColumnCount = 2
            .List = ws2.Range("list2").Value
        End With
    End If
    Last edited by MaczaQ; 02-21-2012 at 05:27 AM. Reason: add code
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,061

    Re: Change combobox list based on option button

    Thx maczaQ!

    CmbSiteName.Clear did the trick...
    I left rest of previous code because it create 2 columns in combobox.

    I can upload file when I finnish it

  4. #4
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Change combobox list based on option button

    my code creates two columns too (without for each loop)

  5. #5
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,061

    Re: Change combobox list based on option button

    hmm.. didn't work for me at the first time... I'll take a look again... Probably I need to redefine lists.

  6. #6
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Change combobox list based on option button

    of course your list1 and list2 should be range for 2 columns

  7. #7
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,061

    Re: Change combobox list based on option button

    Yea, I thought so... I'll have a look and then return you info...

  8. #8
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,061

    Re: Change combobox list based on option button

    Much easier with your approach. Everything working.. Thx.

  9. #9
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,061

    Re: Change combobox list based on option button

    Final file, as I promised... Too large for upload so I have to put it on external site: http://www.2shared.com/file/N1SpWd77/kmlkmzfiles.html

    Everything working now (as I test it but need few more days)... But you'll see codes aren't example of eleganxy (mostly by recording). So any suggestions are wellcome, but not obligatory

    And of course, free for further sharing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1