ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 683|回复: 3

[已解决] 任意(单个或多个)条件的模糊查询求教!

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-3-20 15:30 | 显示全部楼层 |阅读模式
本帖最后由 chengshanming 于 2018-3-21 08:29 编辑

我想把ar(i, 1) Like "*" & cells(i,1) & "*"   and  ar(i, 2) Like "*" & cells(i,2) & "*" and  ar(i, 3) Like "*" & cells(i,3) & "*" ……and ar(i, J) Like "*" & cells(i,J) & "*" ,有没有一种简要的表达方式?






}{1ARHG7FA9WL%QZ]C$P3{S.jpg

多条件查找.rar

270.19 KB, 下载次数: 23

TA的精华主题

TA的得分主题

发表于 2018-3-20 17:27 | 显示全部楼层
'可以是全部条件,你这7个条件当然也是可以用的

Option Explicit

Sub zz()
  Dim j, i, arr, brr, n, first, last, cond
  With Sheets("数据来源")
    arr = .Range("a2:r" & .Cells(Rows.Count, "a").End(xlUp).Row)
  End With
  ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2))
  With Sheets("查询")
    first = .[c1]: last = .[c2]: cond = .[a4:r4]
    For i = 1 To UBound(arr, 1)
      If arr(i, 1) <= last And arr(i, 1) >= first Then
        For j = 1 To UBound(cond, 2)
          If Len(cond(1, j)) > 0 Then
            If InStr(arr(i, j), cond(1, j)) = 0 Then Exit For
          End If
        Next
        If j = UBound(cond, 2) + 1 Then
          n = n + 1
          For j = 1 To UBound(cond, 2): brr(n, j) = arr(i, j): Next
        End If
      End If
    Next
    .[a6].Resize(.Cells(Rows.Count, "a").End(xlUp).Row, UBound(brr, 2)).ClearContents
    If n > 0 Then .[a6].Resize(n, UBound(brr, 2)) = brr
  End With
End Sub

评分

2

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-3-20 23:36 | 显示全部楼层
本帖最后由 chengshanming 于 2018-3-21 08:23 编辑
一把小刀闯天下 发表于 2018-3-20 17:27
'可以是全部条件,你这7个条件当然也是可以用的
已OK!是帖子审核速度太慢,没看到以为回复没成功,所以重复回复了!
Option Explicit
  1. Sub zz()
  2.   Dim j, i, arr, brr, n, first, last, cond
  3.   Dim sh As Worksheet
  4.   [a6].Resize(Cells(Rows.Count, "a").End(xlUp).Row, UBound(UsedRange.Value, 2)).ClearContents
  5.   For Each sh In ThisWorkbook.Sheets
  6.   With sh
  7.     arr = .Range("a2:r" & .Cells(Rows.Count, "a").End(xlUp).Row)
  8.   End With
  9.   ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2))
  10.   With Sheets("查询")
  11.     first = .[c1]: last = .[c2]: cond = .[a4:r4]
  12.     For i = 1 To UBound(arr, 1)
  13.       If arr(i, 1) <= last And arr(i, 1) >= first Then
  14.         For j = 1 To UBound(cond, 2)
  15.           If Len(cond(1, j)) > 0 Then
  16.             If InStr(arr(i, j), cond(1, j)) = 0 Then Exit For
  17.           End If
  18.         Next
  19.         If j = UBound(cond, 2) + 1 Then
  20.           n = n + 1
  21.           For j = 1 To UBound(cond, 2): brr(n, j) = arr(i, j): Next
  22.         End If
  23.       End If
  24.     Next
  25.     If n > 0 Then .Range("a" & UsedRange.Rows.Count + 1).Resize(n, UBound(brr, 2)) = brr
  26.   End With
  27.   Next
  28. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-3-20 23:38 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 chengshanming 于 2018-3-21 08:24 编辑

一把小刀闯天下 发表于 2018-3-20 17:27
'可以是全部条件,你这7个条件当然也是可以用的

Option Explicit
小刀闯天下大师好!你的这中方法的代码可以应用于一个工作薄多张格式相同的工作表的这中情况吗?我试了一下,好像只能取到一张表的明细。


  1. Sub zz()
  2.   Dim j, i, arr, brr, n, first, last, cond
  3.   Dim sh As Worksheet
  4.   [a6].Resize(Cells(Rows.Count, "a").End(xlUp).Row, UBound(UsedRange.Value, 2)).ClearContents
  5.   For Each sh In ThisWorkbook.Sheets
  6.   With sh
  7.     arr = .Range("a2:r" & .Cells(Rows.Count, "a").End(xlUp).Row)
  8.   End With
  9.   ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2))
  10.   With Sheets("查询")
  11.     first = .[c1]: last = .[c2]: cond = .[a4:r4]
  12.     For i = 1 To UBound(arr, 1)
  13.       If arr(i, 1) <= last And arr(i, 1) >= first Then
  14.         For j = 1 To UBound(cond, 2)
  15.           If Len(cond(1, j)) > 0 Then
  16.             If InStr(arr(i, j), cond(1, j)) = 0 Then Exit For
  17.           End If
  18.         Next
  19.         If j = UBound(cond, 2) + 1 Then
  20.           n = n + 1
  21.           For j = 1 To UBound(cond, 2): brr(n, j) = arr(i, j): Next
  22.         End If
  23.       End If
  24.     Next
  25.     If n > 0 Then .Range("a" & UsedRange.Rows.Count + 1).Resize(n, UBound(brr, 2)) = brr
  26.   End With
  27.   Next
  28. End Sub
复制代码

小刀闯天下大师好!你的这中方法的代码可以应用于一个工作薄多张格式相同的工作表的这中情况吗?我试了一下,好像只能取到一张表的明细。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-6 05:14 , Processed in 0.035363 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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