Excel交流网
  • 设为首页|收藏本站|手机版
  • Excel-教程-技巧-培训视频

  • 网站首页
  • Excel教程
  • 新闻动态
  • Excel资源
  • 关于我们

Excel教程

Excel操作
Excel函数
Excel图表
Excel VBA
Excel 行业应用

联系方式

Excel中交流网 联系方式

QQ:18449932 


网  址:www.excel-cn.com  

当前位置:网站首页 > Excel教程 > Excel VBA
Excel VBA

Excel VBA实现渐进式模糊搜索

Excel在录入时可以匹配现有的内容,但有时还是满足不了我们的要求,以下是用Excel VBA实现的渐进式模糊搜索


作者:Excel小子-Office中国


实现的效果:


   渐进式模糊搜索.jpg



Excel 模糊 渐进式搜索操作动画教程


1.gif


Excel VBA实现渐进式模糊搜索的主要代码:


先放置 一个 TextBox1 文本框 及列表框 ListBox1


然后在工作表代码中


Dim d

Dim arr, brr(0)

Dim ar




Private Sub ListBox1_Click()


End Sub


Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

ActiveCell = Me.ListBox1.Value

Me.ListBox1.Visible = False

Me.TextBox1.Visible = False

ActiveCell.Select

End Sub


Private Sub ListBox1_GotFocus()


End Sub

 

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If KeyCode = 13 Then

ActiveCell = ListBox1.Value

Me.ListBox1.Visible = False

Me.TextBox1.Visible = False

ActiveCell.Select

End If

End Sub


Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If WorksheetFunction.CountA(ActiveSheet.UsedRange) > 0 Then

    If WorksheetFunction.CountA(ActiveSheet.UsedRange) = 1 Then

        brr(0) = ActiveSheet.UsedRange

        arr = brr

    Else

        arr = ActiveSheet.UsedRange

    End If

    Dim ct

   Set d = CreateObject("scripting.dictionary")

    If KeyCode = vbKeyDown Then

    'Stop

            ct = ListBox1.ListIndex + 1

        If ct < ListBox1.ListCount Then ListBox1.ListIndex = ct Else ListBox1.ListIndex = 0

    ElseIf KeyCode = vbKeyUp Then

        ct = ListBox1.ListIndex - 1

        If ct > -1 Then ListBox1.ListIndex = ct Else ListBox1.ListIndex = ListBox1.ListCount - 1

        End If

    If KeyCode <> 37 And KeyCode <> 39 And KeyCode <> 13 Then

        For Each ar In arr

            If Len(ar) > 0 Then

            If InStr(ar, TextBox1.Value) = 1 Then

                d(ar) = ""

            End If

            End If

        Next ar

    End If

    If d.Count > 0 And Len(Me.TextBox1.Value) > 0 Then

        With Me.ListBox1

        .Visible = True

        .Left = ActiveCell.Left + ActiveCell.Width

        .Top = ActiveCell.Top

        .Height = ActiveCell.Height * 5

        .Width = ActiveCell.Width * 2

        .List = d.keys

        End With

    Else

        Me.ListBox1.Visible = False

    End If

    End If

End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error Resume Next

If Target.Count = 1 Then

    Me.ListBox1.Visible = False

    With Me.TextBox1

        .Value = ""

        .Visible = True

        .Activate

        .Left = Target.Left

        .Top = Target.Top

        .Width = Target.Width

        .Height = Target.Height


    End With


End If

End Sub


点击次数:  更新时间:2016-12-04 23:08:20  【打印此页】  【关闭】
上一条:Excel VBA 拆分工作簿  下一条:Excel函数-工资求和统计
本站动态|在线留言|在线反馈|友情链接|会员中心|站内搜索|网站地图|网站管理

中山市天鸣科技发展有限公司 版权所有 1999-2024 粤ICP备10043721号

QQ:18449932

免费Excel教程、Excel技巧、Excel培训、Excel函数公式、Excel图表、Excel VBA

Excel教程|Excel技巧|Excel培训|Excel函数公式|Excel图表|VBA

Powered by MetInfo 5.3.12 ©2008-2026  www.metinfo.cn