+ Reply to Thread
Results 1 to 4 of 4

Cascading Combobox Dropdowns

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2015
    Location
    Alaska
    MS-Off Ver
    365
    Posts
    1

    Cascading Combobox Dropdowns

    Hello,

    I would consider myself very new to visual basic, but after a lot of research, I have been unable to find an answer to my question. I have been able to watch some youtube videos of cascading comboboxes, but I havent been able to find one that explained how to have 4 conditional boxes. I figure that there is an and statement that needs to be thrown in there, but I am just not sure on how to do it. Any help would be appreciated, and if you would prefer a different type of example attached I can provide that.

    Thank you very much.

    My code is

    Private Sub ComboBoxConnectionBrand_LostFocus()
    
    Dim csheet As Worksheet
    Set csheet = ThisWorkbook.Sheets("connections")
    
    Me.ComboBoxSize.Clear
    Me.ComboBoxWeight.Clear
    Me.ComboBoxConnection.Clear
    
    For Each cell In ThisWorkbook.Sheets("connections").Range("brand")
        If Me.ComboBoxConnectionBrand = cell Then
            Me.ComboBoxSize.AddItem cell.Offset(0, 1)
        End If
    Next cell
    
    Me.ComboBoxSize = ""
    
    End Sub
    
    
    
    Private Sub comboboxsize_lostfocus()
    
    Dim csheet As Worksheet
    Set csheet = ThisWorkbook.Sheets("connections")
    Dim ConnBrand As String
    ConnBrand = Me.ComboBoxConnectionBrand.Value
    
    Me.ComboBoxWeight.Clear
    Me.ComboBoxConnection.Clear
    
    For Each cell In ThisWorkbook.Sheets("connections").Range("size")
    
        
        If Me.ComboBoxSize = cell Then
               Me.ComboBoxWeight.AddItem cell.Offset(0, 1)
        End If
        
    Next cell
    
    Me.ComboBoxWeight = ""
    
    End Sub
    
    Private Sub comboboxweight_lostfocus()
    
    Dim csheet As Worksheet
    Set csheet = ThisWorkbook.Sheets("connections")
    
    Me.ComboBoxConnection.Clear
    
    For Each cell In ThisWorkbook.Sheets("connections").Range("weight")
        If Me.ComboBoxWeight = cell Then
            Me.ComboBoxConnection.AddItem cell.Offset(0, 1)
        End If
    Next cell
    
    Me.ComboBoxConnection = ""
    
    
    End Sub
    Attached Files Attached Files
    Last edited by jeepbro1; 03-18-2015 at 07:01 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,926

    Re: Cascading Combobox Dropdowns

    code tags applied
    Last edited by protonLeah; 03-18-2015 at 08:30 PM.
    Ben Van Johnson

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Cascading Combobox Dropdowns

    1) To ThisWorkbook module
    Private Sub Workbook_Open()
        Run "sheet2.worksheet_activate"
    End Sub
    Sheet2 module
    Option Explicit
    
    Private dic As Object
    Private x As Variant
    
    Private Sub Worksheet_Activate()
        Dim a, i As Long, w
        Const Combo As String = "Size,Weight,Connection"
        x = Split(Combo, ",")
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        a = Sheets("Connections").Cells(1).CurrentRegion.Value
        For i = 2 To UBound(a, 1)
            If Not dic.exists(CStr(a(i, 1))) Then
                Set dic(CStr(a(i, 1))) = CreateObject("Scripting.Dictionary")
                dic(CStr(a(i, 1))).CompareMode = 1
            End If
            If Not dic(CStr(a(i, 1))).exists(CStr(a(i, 2))) Then
                Set dic(CStr(a(i, 1)))(CStr(a(i, 2))) = CreateObject("Scripting.Dictionary")
                dic(CStr(a(i, 1)))(CStr(a(i, 2))).CompareMode = 1
            End If
            If Not dic(CStr(a(i, 1)))(CStr(a(i, 2))).exists(CStr(a(i, 3))) Then
                ReDim w(1 To 2, 1 To 1)
            Else
                w = dic(CStr(a(i, 1)))(CStr(a(i, 2)))(CStr(a(i, 3)))
                ReDim Preserve w(1 To 2, 1 To UBound(w, 2) + 1)
            End If
            w(1, UBound(w, 2)) = a(i, 4)
            w(2, UBound(w, 2)) = i
            dic(CStr(a(i, 1)))(CStr(a(i, 2)))(CStr(a(i, 3))) = w
        Next
        With Me.ComboBoxConnectionBrand
            .ListFillRange = ""
            .List = dic.keys
        End With
    End Sub
    
    Private Sub ComboBoxConnectionBrand_Change()
        ClearCombo 0
        If Me.ComboBoxConnectionBrand.ListIndex > -1 Then
            Me.OLEObjects("ComboBoxsize").Object.List = dic(Me.ComboBoxConnectionBrand.Value).keys
        End If
    End Sub
    
    Private Sub ComboBoxSize_Change()
        ClearCombo 1
        If Me.ComboBoxSize.ListIndex > -1 Then
            Me.OLEObjects("ComboBoxWeight").Object.List = _
            dic(Me.ComboBoxConnectionBrand.Value)(Me.ComboBoxSize.Value).keys
        End If
    End Sub
    
    Private Sub ComboBoxWeight_Change()
        ClearCombo 2
        If Me.ComboBoxWeight.ListIndex > -1 Then
            Me.OLEObjects("ComboBoxConnection").Object.Column = _
            dic(Me.ComboBoxConnectionBrand.Value)(Me.ComboBoxSize.Value)(Me.ComboBoxWeight.Value)
        End If
    End Sub
    
    Private Sub ClearCombo(s As Long)
        Dim i As Long
        For i = s To UBound(x)
            Me.OLEObjects("Combobox" & x(i)).Object.Clear
        Next
    End Sub
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Cascading Combobox Dropdowns

    Thanks for the rep.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Cascading Dropdowns - 4 Levels
    By joannakf in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-04-2014, 09:01 AM
  2. [SOLVED] Creating two cascading dropdowns from one variable choice.
    By Emmaly in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-13-2013, 11:27 AM
  3. Multiple cascading dropdowns
    By kokorota in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-21-2013, 09:22 AM
  4. Multiple Cascading Dropdowns
    By Emmaly in forum Excel General
    Replies: 6
    Last Post: 11-03-2011, 07:25 AM
  5. Cascading Dropdowns from table
    By jrjeffcoat64 in forum Excel General
    Replies: 1
    Last Post: 09-06-2011, 02:02 PM

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