|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
其实用函数 Search 也可以实现模糊查询
Private Sub TextBox1_Change()
Dim rw As String: rw = Sheet1.Range("A65536").End(xlUp).Row
Dim total As String: total = 0
Dim I&, SJ$
If TextBox1.Value = "" Then ' 如果文本框内容为空,导入所有数据
ListView1.ListItems.Clear ' 清除所有内容
For I = 2 To rw
Set ITM = ListView1.ListItems.Add()
ITM.Text = Sheet1.Cells(I, 1)
ITM.SubItems(1) = Sheet1.Cells(I, 2)
ITM.SubItems(2) = Sheet1.Cells(I, 3)
ITM.SubItems(3) = Format(Sheet1.Cells(I, 4), "#,##0")
total = total + Sheet1.Cells(I, 4).Value
Next I
Label2.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
Label3.Caption = "总计: " & Format(total, "#,##0")
TextBox1.SetFocus
Exit Sub
Else
ListView1.ListItems.Clear ' 清除所有内容
For I = 2 To rw
SJ = Sheet1.Range("A" & I) & Sheet1.Range("B" & I) & Sheet1.Range("C" & I)
On Error Resume Next
If Application.WorksheetFunction.Search(Me.TextBox1, SJ) = 0 Then
Else
Set ITM = ListView1.ListItems.Add()
ITM.Text = Sheet1.Cells(I, 1)
ITM.SubItems(1) = Sheet1.Cells(I, 2)
ITM.SubItems(2) = Sheet1.Cells(I, 3)
ITM.SubItems(3) = Format(Sheet1.Cells(I, 4), "#,##0")
total = total + Sheet1.Cells(I, 4).Value
End If
Next
Label2.Caption = "共找到 " & ListView1.ListItems.Count & " 条记录"
Label3.Caption = "总计: " & Format(total, "#,##0")
TextBox1.SetFocus
End If
End Sub |
|