|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
在百度中了个“如何用vba做出类似百度搜索那样的逐步提示模糊查询下拉列表框?”,把里面的代码复制增加到我的工具中也没反应,我对于这些只是简单会做些表格,所以求楼主大大在你的SOSO工具里做个这种插件,万分感谢!!!
附代码如下:
'定义一个公共的数组变量,用于存放所有列表框项目
Public arr
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim oSP As Shape
'所有列表项数组
arr = Array("张飞", "关羽", "刘备", "赵云", "诸葛亮", "水星", "张苞", "关平", "孙权", "孙坚", "孙策")
'只选中一个单元格时触发
If Target.CountLarge = 1 Then
'定义触发的单元格行列条件
If Target.Column = 1 And Target.Row > 1 Then
'满足条件先显示文本框,隐藏列表框
With Me
Set oSP = .Shapes("TextBox1")
With oSP
.Visible = msoCTrue
.Left = Target.Offset(0, 1).Left
.Top = Target.Top
.Height = Target.Height * 1.5
.Width = Target.Width
End With
Set oSP = .Shapes("ListBox1")
oSP.Visible = msoFalse
End With
Else
'不满足条件就不显示文本框和列表框
With Me
Set oSP = .Shapes("TextBox1")
oSP.Visible = msoFalse
Set oSP = .Shapes("ListBox1")
oSP.Visible = msoFalse
End With
End If
Else
'不满足条件就不显示文本框和列表框
With Me
Set oSP = .Shapes("TextBox1")
oSP.Visible = msoFalse
Set oSP = .Shapes("ListBox1")
oSP.Visible = msoFalse
End With
End If
End Sub
Private Sub TextBox1_Change()
'读取筛选后的列表框项目数组
Dim sText As String
sText = TextBox1.Text
arrList = VBA.Filter(arr, sText)
With ListBox1
.Clear
.List = arrList
End With
'显示列表框
With Me
Set oSP = .Shapes("ListBox1")
With oSP
.Visible = msoCTrue
.Left = TextBox1.Left
.Top = TextBox1.Top + TextBox1.Height
.Height = TextBox1.Height * 5
.Width = TextBox1.Width
End With
End With
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'双击列表框中的列表项将内容填入当前活动单元格中,同时隐藏列表框
Dim oRng As Range
Set oRng = Excel.ActiveCell
oRng.Value = ListBox1.Value
ListBox1.Visible = False
'清空文本框内容
TextBox1.Text = ""
End Sub |
|