单击命令按钮时列表框缩小

时间:2016-12-28 16:23:30

标签: vba excel-vba listbox excel

我有一个问题,每次我点击命令按钮,我在userform上的列表框将缩小。我的命令按钮假设根据插入的相应年份过滤数据。列表框返回所有数据过滤器,但每次单击命令按钮时它都会继续缩小。

这是我的命令按钮代码;

   Private Sub CommandButton5_Click()

Dim a, o() As Variant, n As Long
Dim i As Long, c As Long

Me.ListBox1.RowSource = vbNullString
Me.ListBox1.Clear
Me.ListBox1.ColumnCount = 27
Me.ListBox1.ColumnWidths = "30 pt;65 pt;65 pt;65 pt;65 pt;130 pt;130 pt;49.95 pt;30 pt;45 pt;35 pt;65 pt;65 pt;65 pt;80 pt;65 pt;49.95 pt;30 pt;30 pt;30 pt;30 pt;100 pt;59 pt;65 pt;65 pt;30 pt;65 pt"


a = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).Resize(, 27).Value2
ReDim o(1 To 27, 1 To UBound(a, 1))

For i = 1 To UBound(a, 1)
If UCase(a(i, 11)) Like UCase(Me.TextBox1.Value) & "*" Then
    n = n + 1
    For c = 1 To UBound(a, 2)
        o(c, n) = a(i, c)
    Next
End If
Next

 If n Then
    ReDim Preserve o(1 To 27, 1 To n)
    Me.ListBox1.List = Application.Transpose(o)
 End If

End Sub

0 个答案:

没有答案