How to create “nested” dropdowns in Excel VBA combo boxes?

前端 未结 1 1528
無奈伤痛
無奈伤痛 2021-01-28 16:24

For example,

  1. Cash in Bank

    • Account# 1
    • Account# 2
    • Account# 3
  2. Payroll Expense

    • Regular
相关标签:
1条回答
  • 2021-01-28 17:19

    Here's a rough example:

    Set up two userforms UserForm1 and UserForm2.

    UserForm1 has a label on it.

    UserForm2 has a TreeView on it (You'll have to add this to your toolbox -> Right Click on the Tool Box -> Additional Controls... -> Microsoft TreeView Control, version 6.0)

    Then behind UserForm1 add the following code:

    Private Sub Label1_Click()
        UserForm2.Show
    End Sub
    

    Behind UserForm2 add:

    Option Explicit
    #If VBA7 Then
        Private Declare PtrSafe Function FindWindow Lib "User32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
    
        Private Declare PtrSafe Function GetWindowLong Lib "User32" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
    
        Private Declare PtrSafe Function SetWindowLong Lib "User32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long
    
        Private Declare PtrSafe Function DrawMenuBar Lib "User32" ( _
        ByVal hwnd As Long) As Long
    #Else
        Private Declare Function FindWindow Lib "User32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
    
        Private Declare Function GetWindowLong Lib "User32" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
    
        Private Declare Function SetWindowLong Lib "User32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long
    
        Private Declare Function DrawMenuBar Lib "User32" ( _
        ByVal hwnd As Long) As Long
    #End If
    
    Sub RemoveTitleBar(frm As Object)
        Dim lStyle          As Long
        Dim hMenu           As Long
        Dim mhWndForm       As Long
    
        If Val(Application.Version) < 9 Then
            mhWndForm = FindWindow("ThunderXFrame", frm.Caption) 'for Office 97 version
        Else
            mhWndForm = FindWindow("ThunderDFrame", frm.Caption) 'for office 2000 or above
        End If
        lStyle = GetWindowLong(mhWndForm, -16)
        lStyle = lStyle And Not &HC00000
        SetWindowLong mhWndForm, -16, lStyle
        DrawMenuBar mhWndForm
    End Sub
    
    Private Sub TreeView1_Click()
        UserForm1.Label1 = TreeView1.SelectedItem
    End Sub
    
    Private Sub UserForm_Click()
        Unload Me
    End Sub
    Private Sub UserForm_Initialize()
    
        Call RemoveTitleBar(Me)
    
        With Me
            .StartUpPosition = 0
            .Top = UserForm1.Top + (UserForm1.Height - UserForm1.InsideHeight) + UserForm1.Label1.Height + UserForm1.Label1.Top
            .Left = UserForm1.Left + (UserForm1.Width - UserForm1.InsideWidth) + UserForm1.Label1.Left
        End With
    
        TreeView1.Nodes.Add Key:="Item1", Text:="Parent 1"
        TreeView1.Nodes.Add Key:="Item2", Text:="Parent 2"
        TreeView1.Nodes.Add Key:="Item3", Text:="Parent 3"
    
        TreeView1.Nodes.Add "Item1", tvwChild, "one", "Item 1, Child node 1"
        TreeView1.Nodes.Add "Item1", tvwChild, "two", "Item 1, Child node 2"
    
        TreeView1.Nodes.Add "Item2", tvwChild, "three", "Item 2, Child node 1"
        TreeView1.Nodes.Add "Item2", tvwChild, "four", "Item 2, Child node 2"
    
        TreeView1.Nodes.Add "Item3", tvwChild, "five", "Item 3, Child node 1"
        TreeView1.Nodes.Add "Item3", tvwChild, "six", "Item 3, Child node 2"
    End Sub
    

    This results in:

    Click on the grey bar at the bottom of UserForm2 to dismiss

    You can play around with this a lot more yourself - This is just a quick example of my previous comment. Have a look at adding a DropDown picture to the end of the Label

    0 讨论(0)
提交回复
热议问题