如何在Excel VBA组合框中创建“嵌套”下拉列表?

时间:2018-06-21 08:57:55

标签: excel vba excel-vba

例如,

  1. 银行现金

    • 帐户#1
    • 帐户#2
    • 帐户编号3
  2. 工资支出

    • 常规
    • 额外
    • 代理商

我想创建一个组合框,该框允许选择主要类别(例如工资支出),同时还允许选择子类别(例如代理商)。

以下是此类下拉菜单的示例:

Example

1 个答案:

答案 0 :(得分:1)

这是一个粗略的例子:

设置两个用户表单Object[][] result = new Object[][] {}; for (int i = 0; i < customerCode.length; i++) { result = ArrayUtils.add(result, new Object[] {customerCode[i],countryCode[i]}); } return result; UserForm1

UserForm2上面有标签。

UserForm1上有一个UserForm2(您必须将其添加到工具箱中->右键单击工具箱-> TreeView-> Additional Controls...

然后在Microsoft TreeView Control, version 6.0后面添加以下代码:

UserForm1

Private Sub Label1_Click() UserForm2.Show End Sub 后面添加:

UserForm2

结果是:

enter image description here enter image description here enter image description here enter image description here

单击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 底部的灰色栏以关闭

您可以自己更多地使用它-这只是我先前评论的一个简单示例。看看将UserForm2图片添加到DropDown

的末尾