如何使用用户输入在 VBA 中创建 filter/drop 向下菜单
How to Create a filter/drop down menu in VBA with user input
目前我的代码提示用户输入以逗号分隔的标题列表。但是,有没有一种方法可以更改代码,以便用户可以从下拉菜单中搜索所需的标题,单击标题旁边的复选框,并能够继续搜索所需的标题,直到完成。并将这些字符串存储在一个数组中。
Dim arWords() As String
myt = InputBox("Enter User Input")
arWords() = Split(myt, ",")
For X = 0 To UBound(arWords)
myf = myf & arWords(X) & vbNewLine
Next X
我稍后在代码中有一个数组 (arrH),其中存储了所有标题,所以我认为 id 必须使用它作为源,如果我需要的话?。或者我相信。我还在学习。
arrH = Split(arrTxt(HeaderRow), vbTab)
谢谢!
创建用户窗体。
“用户可以从下拉菜单中搜索所需的标题,单击标题旁边的复选框,并能够继续搜索所需的标题,直到完成”。
带有组合框和复选框的用户窗体。在 CheckBox_Change 事件中,让脚本将 ComboBox 的值存储到 public module-level 数组并迭代数组索引。
有一个隐藏用户表单的“完成”按钮。
Userform 代码模块如下所示:
Public SelectedTitles As Variant, ArrCount As Long, EnableEvents As Boolean
Private Sub CheckBox1_Change()
'User has indicated they want to add the currently selected item to the list
If Not EnableEvents Then Exit Sub
If ArrCount = 0 Then 'First item, create the array
SelectedTitles = Array("")
Else
ReDim Preserve SelectedTitles(UBound(SelectedTitles) + 1) 'Next items, add one more space in the array
End If
'Add the selected title to the array
SelectedTitles(ArrCount) = ComboBox1.Value
'Increment the counter
ArrCount = ArrCount + 1
'Reset the checkbox and the combobox
EnableEvents = False
CheckBox1.Value = False
ComboBox1.Value = ""
EnableEvents = True
End Sub
Private Sub CommandButton1_Click()
'Done Button
Me.Hide
End Sub
Private Sub UserForm_Initialize()
EnableEvents = True
End Sub
这是一个帮助向 ComboBox 列表添加项目的子程序:
Sub ComboBox_AddFromArray(ByRef ComboBox As Object, ListArray As Variant)
ComboBox.Clear
If IsArray(ListArray) Then
Dim i As Long
For i = LBound(ListArray) To UBound(ListArray)
ComboBox.AddItem ListArray(i), ComboBox.ListCount
Next i
Else
ComboBox.AddItem ListArray, 0
End If
End Sub
此函数将在 Load UserForm1
之后和 UserForm1.Show
之前。你会输入像 ComboBox_AddFromArray UserForm1.ComboBox1, arrH
.
这样的参数
将所有这些放在一起的方法是拥有一个控制功能,该功能执行所有用户窗体流程,然后 returns 您需要什么,即用户选择的标题数组。
这就是它的样子
Function UserInputForm(ByRef ArrayOfAllTitles As Variant) As Variant
Load UserForm1
ComboBox_AddFromArray UserForm1.ComboBox1, ArrayOfAllTitles
UserForm1.Show
UserInputForm = UserForm1.SelectedTitles
Unload UserForm1
End Function
最后,一个如何将该功能包含到您的主子中的示例:
Dim SelectedTitles As Variant
SelectedTitles = UserInputForm(arrH)
'SelectedTitles is now an array of Variant/String values
Dim i As Long
For i = LBound(SelectedTitles) To UBound(SelectedTitles)
Debug.Print SelectedTitles(i)
'Access individual members of the array using SelectedTitles(i)
Next i
目前我的代码提示用户输入以逗号分隔的标题列表。但是,有没有一种方法可以更改代码,以便用户可以从下拉菜单中搜索所需的标题,单击标题旁边的复选框,并能够继续搜索所需的标题,直到完成。并将这些字符串存储在一个数组中。
Dim arWords() As String
myt = InputBox("Enter User Input")
arWords() = Split(myt, ",")
For X = 0 To UBound(arWords)
myf = myf & arWords(X) & vbNewLine
Next X
我稍后在代码中有一个数组 (arrH),其中存储了所有标题,所以我认为 id 必须使用它作为源,如果我需要的话?。或者我相信。我还在学习。
arrH = Split(arrTxt(HeaderRow), vbTab)
谢谢!
创建用户窗体。
“用户可以从下拉菜单中搜索所需的标题,单击标题旁边的复选框,并能够继续搜索所需的标题,直到完成”。
带有组合框和复选框的用户窗体。在 CheckBox_Change 事件中,让脚本将 ComboBox 的值存储到 public module-level 数组并迭代数组索引。
有一个隐藏用户表单的“完成”按钮。
Userform 代码模块如下所示:
Public SelectedTitles As Variant, ArrCount As Long, EnableEvents As Boolean
Private Sub CheckBox1_Change()
'User has indicated they want to add the currently selected item to the list
If Not EnableEvents Then Exit Sub
If ArrCount = 0 Then 'First item, create the array
SelectedTitles = Array("")
Else
ReDim Preserve SelectedTitles(UBound(SelectedTitles) + 1) 'Next items, add one more space in the array
End If
'Add the selected title to the array
SelectedTitles(ArrCount) = ComboBox1.Value
'Increment the counter
ArrCount = ArrCount + 1
'Reset the checkbox and the combobox
EnableEvents = False
CheckBox1.Value = False
ComboBox1.Value = ""
EnableEvents = True
End Sub
Private Sub CommandButton1_Click()
'Done Button
Me.Hide
End Sub
Private Sub UserForm_Initialize()
EnableEvents = True
End Sub
这是一个帮助向 ComboBox 列表添加项目的子程序:
Sub ComboBox_AddFromArray(ByRef ComboBox As Object, ListArray As Variant)
ComboBox.Clear
If IsArray(ListArray) Then
Dim i As Long
For i = LBound(ListArray) To UBound(ListArray)
ComboBox.AddItem ListArray(i), ComboBox.ListCount
Next i
Else
ComboBox.AddItem ListArray, 0
End If
End Sub
此函数将在 Load UserForm1
之后和 UserForm1.Show
之前。你会输入像 ComboBox_AddFromArray UserForm1.ComboBox1, arrH
.
将所有这些放在一起的方法是拥有一个控制功能,该功能执行所有用户窗体流程,然后 returns 您需要什么,即用户选择的标题数组。
这就是它的样子
Function UserInputForm(ByRef ArrayOfAllTitles As Variant) As Variant
Load UserForm1
ComboBox_AddFromArray UserForm1.ComboBox1, ArrayOfAllTitles
UserForm1.Show
UserInputForm = UserForm1.SelectedTitles
Unload UserForm1
End Function
最后,一个如何将该功能包含到您的主子中的示例:
Dim SelectedTitles As Variant
SelectedTitles = UserInputForm(arrH)
'SelectedTitles is now an array of Variant/String values
Dim i As Long
For i = LBound(SelectedTitles) To UBound(SelectedTitles)
Debug.Print SelectedTitles(i)
'Access individual members of the array using SelectedTitles(i)
Next i