excel vba列表框填充过滤后的数据
我有一张“数据库用户界面”的表格,D列有地址 我有一张“Klant数据库”的表格,上面有所有客户的地址 我尝试在userform1上筛选并显示数据库UI中具有相同地址的所有行 问题是listbox1保持为空,不显示错误 我在代码中错了什么excel vba列表框填充过滤后的数据,excel,vba,Excel,Vba,我有一张“数据库用户界面”的表格,D列有地址 我有一张“Klant数据库”的表格,上面有所有客户的地址 我尝试在userform1上筛选并显示数据库UI中具有相同地址的所有行 问题是listbox1保持为空,不显示错误 我在代码中错了什么 Private Sub userform_Initialize() Dim RowMax As Integer Dim wsh As Worksheet Dim countExit As Integer Dim Cell
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
提前感谢经过一些困惑和尝试,并复制和粘贴了一些代码片段,它工作得很好,这里是我现在的工作代码 此代码位于模块中
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
提前感谢您的时间和帮助在获得RowMax后,添加一行sez MsgBox(“Count=“&Rows.Count&”,Max=“&RowMax”),以查看这些值是什么。也可以添加Me.Adres.CaptionCount=1048576 max=5 Me.Adres.caption=null(将此编辑为str3)str3=Kelvinring 52编辑列表框1后仍然为空。现在,我们可以看到“For i=1 to RowMax”只查看前5行。这就是你想要的吗?RowMax在D列中查找,但下面的注释是sez A列。哪一个是正确的,代码或注释?之前的代码“对不起”尚未编辑注释:)前5行是正确的,因为现在没有更多记录测试数据是5行,但代码读取前5行非常好,但它没有在“列表框”中显示该行的值,然后单击“确定”,然后进行比较——D列与str3列——str3列在其他地方设置为Kelvinring 52。所以D列中的值不等于。其中一个空格中可能有前导空格或尾随空格,或者空格的数量可能不同,或者大写字母可能不同。再次检查D列和str3列中的内容。在debug.print的“then”后面添加一行,以显示D列和E列