Excel VBA列表框填充过滤数据

时间:2018-07-07 18:14:39

标签: excel vba

我有一个工作表“ Database UI”,在D列上有adres

我有一张“ Database Klant”工作表,上面所有的客户都在其中

我尝试在userform1上以相同的过滤条件过滤并显示数据库UI中的所有行

问题是listbox1保持为空,没有错误显示 我在代码中错了什么

    Private Sub userform_Initialize()
    Dim RowMax As Integer
    Dim wsh As Worksheet
    Dim countExit As Integer
    Dim CellCombo2 As String
    Dim i As Integer

    Set wsh = ThisWorkbook.Sheets("Database IU")
    RowMax = wsh.Cells(Rows.Count, "D").End(xlUp).Row
    'find last row of sheet in column D

    ListBox1.Clear
    'clear all value of comboBox1

    With ListBox1
        For i = 1 To RowMax
            If wsh.Cells(i, "D").Value = str3 Then
            'Just show value of mapping with column D
        .ColumnWidths = "50;50;50;50;50;50;50;50;50;50"
        .AddItem wsh.Cells(i, "E").Value
        .List(.ListCount - 1, 1) = wsh.Cells(i, "F").Value
        .List(.ListCount - 1, 2) = wsh.Cells(i, "G").Value
        .List(.ListCount - 1, 3) = wsh.Cells(i, "H").Value
        .List(.ListCount - 1, 4) = wsh.Cells(i, "I").Value
        .List(.ListCount - 1, 5) = wsh.Cells(i, "J").Value
        .List(.ListCount - 1, 6) = wsh.Cells(i, "K").Value
        .List(.ListCount - 1, 7) = wsh.Cells(i, "L").Value
        .List(.ListCount - 1, 8) = wsh.Cells(i, "M").Value
        .List(.ListCount - 1, 9) = wsh.Cells(i, "N").Value
        .List(.ListCount - 1, 10) = wsh.Cells(i, "O").Value

        Else
        End If
        Next i
    End With
End Sub

enter image description here 预先感谢

1 个答案:

答案 0 :(得分:0)

经过一些令人费解的尝试,然后复制并粘贴了几段代码,在这里可以正常工作了,现在对我来说是有效的代码

此代码在模块中

Public str2$

此代码

Private Sub userform_activate()
Klant.Caption = str2
End Sub

和这个

Private Sub UserForm_Initialize()
Blad8.Activate
Dim RowMax As Integer
Dim wsh As Worksheet
Dim countExit As Integer
Dim CellCombo2 As String
Dim i As Integer

Set wsh = ThisWorkbook.Sheets("Database IU")
RowMax = wsh.cells(Rows.Count, "C").End(xlUp).Row
'find last row of sheet in column A

Filters.Clear
'clear all value of comboBox2

With Filters
    For i = 1 To RowMax
        If wsh.cells(i, "C").Value = str2 Then
        'Just show value of mapping with column A
        .AddItem wsh.cells(i, "N").Value
        Else
        End If
    Next i
End With
End Sub

提前感谢您的时间和帮助