1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

帖子
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 210|回复: 11

改成模糊查询

[复制链接]

TA的精华主题

TA的得分主题

发表于 2025-1-27 15:40 | 显示全部楼层 |阅读模式
老师!有代码《系查询。如何修改为模糊查询
Option Explicit
Sub 查询()
    Dim ar, br, i&, j&, r&, N&, dic As Object, strInput$, iPosCol&, wks As Worksheet
    Sheets("查询").Select
    Application.ScreenUpdating = False
    strInput = InputBox("请输入关键字:", "名称")
    If strInput = "" Then Exit Sub
    Set dic = CreateObject("Scripting.Dictionary")
   
    With [a4].CurrentRegion
        .Offset(1).Clear
        ar = .Resize(12 ^ 3)
        For i = 1 To UBound(ar, 2): dic(ar(1, i)) = i: Next
        r = 1
    End With
    For Each wks In Sheets
        If wks.Name <> ActiveSheet.Name Then
            With wks
                br = .[a1].CurrentRegion
                For i = 2 To UBound(br)
                    N = 0
                    For j = 1 To UBound(br, 2)
                        If br(i, j) = strInput Then
                             N = i: Exit For
                        End If
                    Next j
                    If N <> 0 Then
                        r = r + 1
                        For j = 1 To UBound(br, 2)
                            If dic.exists(br(1, j)) Then
                                iPosCol = dic(br(1, j))
                                ar(r, iPosCol) = br(i, j)
                            End If
                        Next j
                        ar(r, UBound(ar, 2)) = .Name
                    End If
                Next i
            End With
        End If
    Next wks
   
    With [a4].Resize(r, UBound(ar, 2))
        .Columns(1).NumberFormatLocal = "0000"
        .Columns(2).NumberFormatLocal = "yyyy-mm-dd"
        .Columns(5).NumberFormatLocal = "@"
        .Columns("F:h").NumberFormatLocal = "0.00"
        .Value = ar
        .HorizontalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous
        .Font.Size = 10
    End With
    Set dic = Nothing
    Application.ScreenUpdating = True
    Beep
End Sub
请老师指教。谢谢


TA的精华主题

TA的得分主题

发表于 2025-1-27 15:57 | 显示全部楼层
    If instr(br(i, j), strInput) Then
       N = i: Exit For
   End If

TA的精华主题

TA的得分主题

发表于 2025-1-27 16:30 | 显示全部楼层
源代码是gwjkkkkk老师写的吧,让他来吧。

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-1-27 16:30 | 显示全部楼层
shiruiqiang 发表于 2025-1-27 15:57
If instr(br(i, j), strInput) Then
       N = i: Exit For
   End If

老师!是加这里吗
strInput = InputBox("&#199;&#235;ê&#228;è&#235;1&#216;&#188;ü×&#214;£o", "&#195;&#251;3&#198;")
    ‘’'If strInput = "" Then Exit Sub
    If InStr(br(i, j), strInput) Then
       N = i: Exit For
    End If
    Set dic = CreateObject("Scripting.Dictionary")

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-1-27 16:31 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
shiruiqiang 发表于 2025-1-27 15:57
If instr(br(i, j), strInput) Then
       N = i: Exit For
   End If

老师!运行不行也

TA的精华主题

TA的得分主题

发表于 2025-1-27 16:44 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Option Explicit
Sub 查询()
    Dim ar, br, i&, j&, r&, N&, dic As Object, strInput$, iPosCol&, wks As Worksheet
    Sheets("查询").Select
    Application.ScreenUpdating = False
    strInput = InputBox("请输入关键字:", "名称")
    If strInput = "" Then Exit Sub
    Set dic = CreateObject("Scripting.Dictionary")
   
    With [a4].CurrentRegion
        .Offset(1).Clear
        ar = .Resize(12 ^ 3)
        For i = 1 To UBound(ar, 2): dic(ar(1, i)) = i: Next
        r = 1
    End With
    For Each wks In Sheets
        If wks.Name <> ActiveSheet.Name Then
            With wks
                br = .[a1].CurrentRegion
                For i = 2 To UBound(br)
                    N = 0
                    For j = 1 To UBound(br, 2)
                        If Instr(br(i, j), strInput) Then
                             N = i: Exit For
                        End If
                    Next j
                    If N <> 0 Then
                        r = r + 1
                        For j = 1 To UBound(br, 2)
                            If dic.exists(br(1, j)) Then
                                iPosCol = dic(br(1, j))
                                ar(r, iPosCol) = br(i, j)
                            End If
                        Next j
                        ar(r, UBound(ar, 2)) = .Name
                    End If
                Next i
            End With
        End If
    Next wks
   
    With [a4].Resize(r, UBound(ar, 2))
        .Columns(1).NumberFormatLocal = "0000"
        .Columns(2).NumberFormatLocal = "yyyy-mm-dd"
        .Columns(5).NumberFormatLocal = "@"
        .Columns("F:h").NumberFormatLocal = "0.00"
        .Value = ar
        .HorizontalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous
        .Font.Size = 10
    End With
    Set dic = Nothing
    Application.ScreenUpdating = True
    Beep
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2025-1-27 16:47 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-1-27 17:02 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
ykcbf1100 发表于 2025-1-27 16:30
源代码是gwjkkkkk老师写的吧,让他来吧。

老师!不知道也!是谁写的,没有查到

TA的精华主题

TA的得分主题

发表于 2025-1-27 17:08 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

TA的精华主题

TA的得分主题

发表于 2025-1-27 19:04 | 显示全部楼层
ykcbf1100 发表于 2025-1-27 16:30
源代码是gwjkkkkk老师写的吧,让他来吧。

看来每个人写代码的风格也比较固定,好像是我写的,但还是建议楼主上传附件便于其他老师发挥自己的水平。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-3-8 03:24 , Processed in 0.032719 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表