首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel VBA -将值插入多列组合框

Excel VBA -将值插入多列组合框
EN

Stack Overflow用户
提问于 2015-05-26 18:01:41
回答 1查看 20.9K关注 0票数 0

我在Excel中的用户窗体上有两个组合框。当我选择第一个组合框时,它会筛选当前工作表上的行,并在combobox2中显示值。

我希望combobox2是一个多列组合框,这样我就可以将多个值从一个工作表复制并粘贴到另一个工作表中,但是我不能将多个值放入combobox2中。这是我破解出来的代码。当我更改combobox1中的值,但combobox2显示单个值时,我可以过滤结果:

代码语言:javascript
运行
复制
Private Sub ComboBox1_Change()
Me.ComboBox2.Clear

'filter the results in combo box 2 when the user selects the different         options in combo box 1
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim copyFrom As Range, aCell As Range
    Dim lRow As Long
    Dim strSearch As String

Set wb = ThisWorkbook
'~~> Set this to the worksheet where the autofilter is applied
Set ws = wb.Worksheets("RegEvents")

'~~> Filter Column on text in Combo box 1
strSearch = ComboBox1.Value

With ws
    '~~> Remove any filters
    .AutoFilterMode = False

    With .Range("RegEvents_WorksheetData")
        .AutoFilter Field:=Range("RegEvents_Action").Column, Criteria1:="="     & strSearch

        '~~> Identify the filtered range
        Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible)

        '~~> Add values from filtered search to Combobox2
        For Each aCell In copyFrom

If aCell.Value <> "" And aCell.Column = Range("RegEvents_EventID").Column Then
ComboBox2.AddItem (aCell.Value)
End If

If aCell.Value <> "" And aCell.Column = Range("RegEvents_Event").Column Then
ComboBox2.AddItem (aCell.Value)
 End If

     Next
    End With

    '~~> Remove any filters
    .AutoFilterMode = False
End With
End Sub

Private Sub UserForm_Initialize()
'Used to create an array
Dim listItems As Variant
    Me.ComboBox1.ListIndex = -1 ' -1 = no items selected

Label1.Caption = "Action"
Label2.Caption = "Event"

listItems = Range("CatogriesAction")

With ComboBox1
'Loops through the array and only adds non blank values to the combo box
For i = 1 To UBound(listItems, 1)
 If Len(Trim(listItems(i, 1))) > 0 Then
 .AddItem listItems(i, 1) ' populate the listbox
 End If
 Next i

 .ListIndex = 0 ' -1 = no items selected, set to 0 to select the first item
 End With

'Set number of columns for combobox 2
With Me.ComboBox2
 .ColumnCount = 2
 .BoundColumn = 2
 .ColumnWidths = ".5 in; .5 in"
 End With

End Sub
EN

回答 1

Stack Overflow用户

发布于 2015-05-26 19:29:53

对于多列ComboBoxes,必须使用.List属性来填充列数据。所以在你的例子中:

代码语言:javascript
运行
复制
For Each aCell In copyFrom

 If aCell.Value <> "" And aCell.Column = Range("RegEvents_EventID").Column Then
  ComboBox2.AddItem aCell.Value
  lRow = aCell.Row
 End If

 If aCell.Row = lRow And aCell.Column = Range("RegEvents_Event").Column Then
  With ComboBox2
   .List(.ListCount - 1, 1) = aCell.Value
  End With
 End If

Next

使用.TextColumn在文本字段中同时显示两列的完整示例。要求:带ComboBox1的UserForm

代码语言:javascript
运行
复制
Private Sub UserForm_Initialize()

 With Me.ComboBox1
  .ColumnCount = 3
  .BoundColumn = 2
  .TextColumn = 3
  .ColumnWidths = "1cm;1cm;0"
 End With

 RegEvents_EventID = [{1;2;3;4;5}]
 RegEvents_Event = [{"Event 1";"Event 2";"Event 3";"Event 4";"Event 5"}]

 For i = LBound(RegEvents_EventID) To UBound(RegEvents_EventID)
  With Me.ComboBox1
   .AddItem RegEvents_EventID(i, 1)
   .List(.ListCount - 1, 1) = RegEvents_Event(i, 1)
   .List(.ListCount - 1, 2) = RegEvents_EventID(i, 1) & " " & RegEvents_Event(i, 1)
  End With
 Next

End Sub
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30455382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档