【问题标题】:Excel VBA Listbox to Filter excel vbaExcel VBA列表框过滤excel vba
【发布时间】:2019-07-26 10:59:00
【问题描述】:

我有一个 listbox1,它在从第一个列表框中选择一个项目时填充第二个 listbox2。我正在尝试为 listbox2 提供一个代码,以根据选择过滤我的 excel 电子表格中的数据。但是,我是 vba 的新手,我正在苦苦挣扎,因为:

  1. 我的 listbox2 代码没有过滤数据和
  2. 截至目前,一列的代码是静态的。

为了让我的代码正常工作,它需要动态确定要过滤的列。我在下面添加了我的代码:

  • A) 初始化
  • B) 列表框1 点击
  • C)Listbox2 点击

C 部分是我的代码失败的地方


A 部分

用户表单初始化:

Public Sub UserForm_Initialize()

With ListBox1
    .AddItem "State"
    .AddItem "County"
    .AddItem "City"
End With

ListBox1.Font.Size = 12
ListBox1.Font.Name = "Arial"

End Sub

B部分

列表框 1 点击

Private Sub ListBox1_Click()

Dim x As Integer

x = ListBox1.ListIndex

Select Case x

    Case Is = 0

        'code to add a unique list to user form when selecting budim yer

        'With ListBox2
        '
        '    .AddItem "Michael"
        '    .AddItem "Kassie"
        'End With


        Dim myCollection As Collection, cell As Range

        On Error Resume Next

        Set myCollection = New Collection

        With ListBox2
            .Clear

            For Each cell In Range("O11:O6000")

                If Len(cell) <> 0 Then
                    Err.Clear
                    myCollection.Add cell.Value, cell.Value
                    If Err.Number = 0 Then .AddItem cell.Value
                End If

            Next cell

        End With

    Case Is = 1

        Dim my2Collection As Collection, cell_2 As Range
        On Error Resume Next
        Set my2Collection = New Collection

        With ListBox2
            .Clear

            For Each cell_2 In Range("D11:D6000")

                If Len(cell_2) <> 0 Then
                    Err.Clear
                    my2Collection.Add cell_2.Value, cell_2.Value
                    If Err.Number = 0 Then .AddItem cell_2.Value
                End If

            Next cell_2

        End With

    Case Is = 2

        Dim my3Collection As Collection, cell_3 As Range

        On Error Resume Next

        Set my3Collection = New Collection

        With ListBox2
            .Clear

            For Each cell_3 In Range("F11:F6000")

                If Len(cell_3) <> 0 Then
                    Err.Clear
                    my3Collection.Add cell_3.Value, cell_3.Value
                    If Err.Number = 0 Then .AddItem cell_3.Value
                End If

            Next cell_3

        End With
        End With              ' << Edit:  double End With ??? :-;

End Select

ListBox2.Font.Size = 12
ListBox2.Font.Name = "Arial"

End Sub

C 部分

Listbox 2 Click 这是我的代码失败的地方

Private Sub ListBox2_Click()

Dim str As String
str = ListBox2.Text
Worksheets("Report").Range("A10:AE6000").AutoFilter Field:=2, criteria:="str"

End Sub

【问题讨论】:

    标签: excel vba filter listbox userform


    【解决方案1】:

    由于str 已经是字符串类型的变量,所以不需要在此语句中将其放在引号中:

    Worksheets("Report").Range("A10:AE6000").AutoFilter Field:=2, criteria:="str"

    将其更改为变量表示:

    Worksheets("Report").Range("A10:AE6000").AutoFilter Field:=2, criteria:=str

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多