gpt4 book ai didi

VBA通过文本框实时过滤列表框

转载 作者:行者123 更新时间:2023-12-05 00:20:59 24 4
gpt4 key购买 nike

我想根据包含在同一用户窗体中的文本框中写入的文本来过滤根据存储在工作表中的值列表创建的列表框。

我的列表框有 4 或 5 列(取决于 OptionField 的选择),我想在所有列中搜索所写的文本。

示例:我在 TextField 中写入“aaa”,列表框应返回一个基于第 1 或 2 或 3 或 4 或 5 列包含“aaa”的所有行的列表。

在我的代码下面,用于刷新 OptionField 选择的列表(此代码不会产生任何错误,它只是为了展示我如何创建我的列表):

Sub RefreshList()

Dim selcell, firstcell As String
Dim k, i As Integer
Dim r as long
i = 0
k = 0

' reads parameters from hidden worksheet

If Me.new_schl = True Then

firstcell = Cells(3, 4).Address
selcell = firstcell

Do Until IsEmpty(Range("" & selcell & "")) And i = 2
If IsEmpty(Range("" & selcell & "")) Then i = i + 1
k = k + 1
selcell = Cells(1 + k, 7).Address(0, 0)
Loop

k = k - 1
selcell = Cells(1 + k, 7).Address(0, 0)

With Me.ListBox1

.ColumnCount = 4
.ColumnWidths = "50; 80; 160; 40"
.RowSource = ""
Set MyData = Range("" & firstcell & ":" & selcell & "")
.List = MyData.Cells.Value

For r = .ListCount - 1 To 0 Step -1
If .List(r, 3) = "" Or .List(r, 3) = "0" Then
.RemoveItem r
End If
Next r

End With

Else

firstcell = Cells(3, 11).Address
selcell = firstcell

Do Until IsEmpty(Range("" & selcell & "")) And i = 11
If IsEmpty(Range("" & selcell & "")) Then i = i + 1
k = k + 1
selcell = Cells(1 + k, 15).Address(0, 0)
Loop

k = k - 1
selcell = Cells(1 + k, 15).Address(0, 0)

With Me.ListBox1

.ColumnCount = 5
.ColumnWidths = "40; 40; 160; 40; 40"
.RowSource = ""
Set MyData = Range("" & firstcell & ":" & selcell & "")
.List = MyData.Cells.Value

For r = .ListCount - 1 To 0 Step -1
If .List(r, 3) = "" Or .List(r, 3) = "0" Then
.RemoveItem r
End If
Next r

End With

End If

End Sub

最佳答案

我终于可以拿出一些东西了!

Sub Filter_Change()

Dim i As Long
Dim Str As String

Str = Me.Filter.Text

Me.RefreshList

If Not Str = "" Then
With Me.ListBox1

For i = .ListCount - 1 To 0 Step -1
If InStr(1, LCase(.List(i, 0)), LCase(Str)) = 0 And InStr(1, LCase(.List(i, 1)), LCase(Str)) = 0 And _
InStr(1, LCase(.List(i, 2)), LCase(Str)) = 0 And InStr(1, LCase(.List(i, 3)), LCase(Str)) = 0 Then

.RemoveItem i

End If
Next i

End With
End If

End Sub

关于VBA通过文本框实时过滤列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17060306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com