ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

一个FindAll函数,谁有时间看看写得怎么样?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-3-20 13:35 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:自定义函数开发
本帖最后由 liucqa 于 2013-9-23 10:12 编辑

  1. Function FindAll(SearchRange As Range, _

  2.                 FindWhat As Variant, _

  3.                 Optional LookIn As XlFindLookIn = xlValues, _

  4.                 Optional LookAt As XlLookAt = xlWhole, _

  5.                 Optional SearchOrder As XlSearchOrder = xlByRows, _

  6.                 Optional MatchCase As Boolean = False, _

  7.                 Optional BeginsWith As String = vbNullString, _

  8.                 Optional EndsWith As String = vbNullString, _

  9.                 Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range

  10.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  11.     ' FindAll

  12.     ' This searches the range specified by SearchRange and returns a Range object

  13.     ' that contains all the cells in which FindWhat was found. The search parameters to

  14.     ' this function have the same meaning and effect as they do with the

  15.     ' Range.Find method. If the value was not found, the function return Nothing. If

  16.     ' BeginsWith is not an empty string, only those cells that begin with BeginWith

  17.     ' are included in the result. If EndsWith is not an empty string, only those cells

  18.     ' that end with EndsWith are included in the result. Note that if a cell contains

  19.     ' a single word that matches either BeginsWith or EndsWith, it is included in the

  20.     ' result.  If BeginsWith or EndsWith is not an empty string, the LookAt parameter

  21.     ' is automatically changed to xlPart. The tests for BeginsWith and EndsWith may be

  22.     ' case-sensitive by setting BeginEndCompare to vbBinaryCompare. For case-insensitive

  23.     ' comparisons, set BeginEndCompare to vbTextCompare. If this parameter is omitted,

  24.     ' it defaults to vbTextCompare.

  25.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



  26.     Dim FoundCell As Range

  27.     Dim FirstFound As Range

  28.     Dim LastCell As Range

  29.     Dim ResultRange As Range

  30.     Dim XLookAt As XlLookAt

  31.     Dim Include As Boolean

  32.     Dim CompMode As VbCompareMethod

  33.     Dim Area As Range

  34.     Dim MaxRow As Long

  35.     Dim MaxCol As Long

  36.    

  37.     CompMode = BeginEndCompare

  38.     If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then

  39.         XLookAt = xlPart

  40.     Else

  41.         XLookAt = LookAt

  42.     End If



  43.     ' this loop in Areas is to find the last cell

  44.     ' of all the areas. That is, the cell whose row

  45.     ' and column are greater than or equal to any cell

  46.     ' in any Area.

  47.     For Each Area In SearchRange.Areas

  48.         With Area

  49.             If .Cells(.Cells.Count).Row > MaxRow Then

  50.                 MaxRow = .Cells(.Cells.Count).Row

  51.             End If

  52.             If .Cells(.Cells.Count).Column > MaxCol Then

  53.                 MaxCol = .Cells(.Cells.Count).Column

  54.             End If

  55.         End With

  56.     Next Area

  57.     Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)

  58.    

  59.     Set FoundCell = SearchRange.Find(what:=FindWhat, _

  60.         after:=LastCell, _

  61.         LookIn:=LookIn, _

  62.         LookAt:=XLookAt, _

  63.         SearchOrder:=SearchOrder, _

  64.         MatchCase:=MatchCase)



  65.     If Not FoundCell Is Nothing Then

  66.         Set FirstFound = FoundCell

  67.         Set ResultRange = FoundCell

  68.         Set FoundCell = SearchRange.FindNext(after:=FoundCell)

  69.         Do Until False ' Loop forever. We'll "Exit Do" when necessary.

  70.             If (FoundCell Is Nothing) Then

  71.                 Exit Do

  72.             End If

  73.             If (FoundCell.Address = FirstFound.Address) Then

  74.                 Exit Do

  75.             End If

  76.             Include = False

  77.                

  78.             If BeginsWith = vbNullString Then

  79.                 If EndsWith = vbNullString Then

  80.                     Include = True

  81.                 Else

  82.                     If Len(FoundCell.Text) < Len(EndsWith) Then

  83.                         Include = False

  84.                     Else

  85.                         If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, CompMode) = 0 Then

  86.                             Include = True

  87.                         Else

  88.                             Include = False

  89.                         End If

  90.                     End If

  91.                 End If

  92.             End If

  93.             If EndsWith = vbNullString Then

  94.                 If BeginsWith = vbNullString Then

  95.                     Include = True

  96.                 Else

  97.                     If StrComp(Left(FoundCell.Text, Len(BeginsWith)), BeginsWith, CompMode) = 0 Then

  98.                         Include = True

  99.                     Else

  100.                         Include = False

  101.                     End If

  102.                 End If

  103.             Else

  104.                 If Len(FoundCell.Text) < Len(EndsWith) Then

  105.                     Include = False

  106.                 Else

  107.                     If StrComp(Right(FoundCell.Text, Len(EndsWith)), EndsWith, CompMode) = 0 Then

  108.                         Include = True

  109.                     Else

  110.                         Include = False

  111.                     End If

  112.                 End If

  113.             End If

  114.             

  115.             If Include = True Then

  116.                 Set ResultRange = Application.Union(ResultRange, FoundCell)

  117.             End If

  118.             Set FoundCell = SearchRange.FindNext(after:=FoundCell)

  119.         Loop

  120.     End If

  121.         

  122.     Set FindAll = ResultRange

  123. End Function
复制代码


原帖
http://club.excelhome.net/forum. ... =843516&pid=5762275

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-27 21:57 , Processed in 0.023711 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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