使用命令按钮

时间:2017-07-24 09:58:07

标签: excel vba excel-vba

我开发了一个userform,有两个Listbox多选项。

我已经使用" locations"填充了listbox1。例如:德国,美国,英国;从表单列表。

我已经填充了listbox2,其中包含"位置"例如:德国,美国,英国来自sheet list_Man。

我的目标是过滤我的工作表中的行"结果",具体取决于位置选择。该位置可在我的工作表的第L和M栏中找到"结果"

我有一个功能Do过滤器。

Sub DoFilter()
Dim Ws As Worksheet
Dim strCriteria() As String
Dim arrIdx As Integer
Dim xRow As Integer
arrIdx = 0
For xRow = 2 To Last(1, List.Cells)
If List.Cells(xRow, 2) = True Then
ReDim Preserve strCriteria(0 To arrIdx)
strCriteria(arrIdx) = List.Cells(xRow, 3)
arrIdx = arrIdx + 1
End If
Next xRow
Set Ws = ThisWorkbook.Sheets("Result")
If arrIdx = 0 Then
Ws.UsedRange.AutoFilter
Else
Ws.Range("A:R").AutoFilter Field:=12, Criteria1:=Array(strCriteria), Operator:=xlFilterValues
End If
End Sub

![this is the user form that have been designed. The Listbox1 has the locations from the list sheet and the listbox2 has the locations from the List_Man sheet. The filter option, according to the selection in the listbox, filters the column L and M of the sheet "Result". exit buton, unloads and returns to the original sheet clearing the filter option.] 1

现在用户形式的工作原理是,如果我同时选择了两个列表框,那么我会选择过滤选项。

我希望在命令按钮中有一个代码,以这种方式

If I select "USA" in listbox1, then I would like to see only the filtered rows in my sheet "Result" for the listbox1 [![如果我在listbox1中选择了多个选项,"美国和英国"在listbox2中,我选择"德国",然后我需要在结果表中为这些选项"过滤行。

反之亦然的情况应该是可能的。如果我在listbox2中只选择location2,我应该能够在表格中看到过滤的行"结果"。 如果我在listbox2中选择多个选项,并在listbox1中选择一个选项,那么我应该能够相应地看到结果。

我想在命令按钮中输入代码" Filter"对于这种情况。

任何领导都会有所帮助。

这是我在列表框中使用的代码     私有子ListBox2_Change()     Dim listboxCounter As Integer     对于listboxCounter = 0到ListBox2.ListCount - 1     如果ListBox2.Selected(listboxCounter)= True则        List_Man.Cells(listboxCounter + 2,2)= True     其他       List_Man.Cells(listboxCounter + 2,2)= False     万一     下一个     结束子 这是我的用户形式的代码

Private Sub UserForm_activate()
Dim xRow As Integer
Dim yows As Integer

For xRow = 2 To Last(1, List.Range("A:A"))
    With ListBox1
        .AddItem List.Cells(xRow, 3).Value
        If List.Cells(xRow, 2) = True Then
            .Selected(xRow - 2) = True
        Else
            .Selected(xRow - 2) = False
        End If
    End With
Next xRow
ListBox1.Height = (xRow - 1) * 15

For yrow = 2 To Last(1, List_Man.Range("A:A"))
With ListBox2
.AddItem List_Man.Cells(yrow, 3).Value
If List_Man.Cells(yrow, 2) = True Then
.Selected(yrow - 2) = True
Else
.Selected(yrow - 2) = False
End If
End With
Next yrow
ListBox2.Height = (xRow - 1) * 15
End Sub

0 个答案:

没有答案
相关问题