扩展的Find方法
我们能够使用Find方法查找单元格区域的数据,但是没有一个方法能够返回一个Range对象,该对象引用了含有所查找数据的所有单元格,下面提供了一个FindAll函数来实现此功能。此外,Find方法的另一个不足之处是不支持通配符字符串,下面也提供了一个WildCardMatchCells函数,返回一个Range对象,引用了与所提供的通配符字符串相匹配的单元格。通配符字符串可以是有效使用在Like运算符中的任何字符串。
FindAll函数
这个程序在参数SearchRange所代表的区域中查找所有含有参数FindWhat代表的值的单元格,SearchRange参数必须是一个单独的单元格区域对象,FindWhat参数是想要查找的值,其它参数是可选的且与Find方法的参数意思相同。
FindAll函数的代码如下:
Option Compare Text Function FindAll(SearchRange As Range, FindWhat As Variant, _ Optional LookIn As XlFindLookIn = xlValues, Optional LookAt As XlLookAt = xlWhole, _ Optional SearchOrder As XlSearchOrder = xlByRows, _ Optional MatchCase As Boolean = False) As Range '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 返回SearchRange区域中含有FindWhat所代表的值的所有单元格组成的Range对象 ' 其参数与Find方法的参数相同 ' 如果没有找到单元格,将返回Nothing. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim FoundCell As Range Dim FoundCells As Range Dim LastCell As Range Dim FirstAddr As String With SearchRange Set LastCell = .Cells(.Cells.Count) End With Set FoundCell = SearchRange.Find(what:=FindWhat, after:=LastCell, _ LookIn:=LookIn, LookAt:=LookAt, SearchOrder:=SearchOrder, MatchCase:=MatchCase) If Not FoundCell Is Nothing Then Set FoundCells = FoundCell FirstAddr = FoundCell.Address Do Set FoundCells = Application.Union(FoundCells, FoundCell) Set FoundCell = SearchRange.FindNext(after:=FoundCell) Loop Until (FoundCell Is Nothing) Or (FoundCell.Address = FirstAddr) End If If FoundCells Is Nothing Then Set FindAll = Nothing Else Set FindAll = FoundCells End If End Function
使用示例:
Sub TestFindAll() Dim SearchRange As Range Dim FoundCells As Range Dim FoundCell As Range Dim FindWhat As Variant Dim MatchCase As Boolean Dim LookIn As XlFindLookIn Dim LookAt As XlLookAt Dim SearchOrder As XlSearchOrder Set SearchRange = ThisWorkbook.Worksheets(1).Range("A1:L20") FindWhat = "A" '要查找的文本,可根据实际情况自定 LookIn = xlValues LookAt = xlPart SearchOrder = xlByRows MatchCase = False Set FoundCells = FindAll(SearchRange:=SearchRange, FindWhat:=FindWhat, _ LookIn:=LookIn, LookAt:=LookAt, SearchOrder:=SearchOrder, MatchCase:=MatchCase) If FoundCells Is Nothing Then Debug.Print "没有找到!" Else For Each FoundCell In FoundCells.Cells Debug.Print FoundCell.Address, FoundCell.Text Next FoundCell End If End Sub
上面的代码中,列出了查找区域中含有所要查找的数据的所有单元格的地址以及相应文本。不仅可以找出所有含有所查找数据的单元格地址,而且也可以对这些单元格进行一系列操作,如格式化、更改数据等。
WildCardMatchCells函数
这个程序查找参数SearchRange所代表的区域中所有单元格,使用Like运算符将它们的值与参数CompareLikeString所代表的值比较。参数SearchRange必须是一个单独的区域,参数CompareLikeString是想要比较的文本的格式。该函数使用单元格的Text属性而不是Value属性。可选参数SearchOrder和MatchCase与Find方法中的参数意义相同。
该函数返回一个Range对象,该对象包含对与参数CompareLikeString相匹配的所有单元格的引用。如果没有匹配的单元格,则返回Nothing。
因为Find方法不支持通配符,程序将循环所有的单元格,因此对于包含大量数据的区域,执行时间可能是一个问题。并且,如果参数MatchCase为False或忽略该参数,文本在程序中必须被转换成大写,以便于查找时不区分大小写(即“A”=“a”),因此,此时程序运行将更慢。
WildCardMatchCells函数的代码如下:
Function WildCardMatchCells(SearchRange As Range, CompareLikeString As String, _ Optional SearchOrder As XlSearchOrder = xlByRows, _ Optional MatchCase As Boolean = False) As Range '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 本程序返回文本值与通配符字符串相匹配的单元格引用 ' 返回SearchRange区域中所有相匹配的单元格 ' 匹配的条件是参数CompareLikeString ' 使用了VBA中的LIKE运算符 ' 如果没有相匹配的单元格或指定了一个无效的参数,则返回Nothing. ' ' 参数SearchOrder指定查找的方向;逐行还是逐列(SearchOrder:=xlByRows或SearchOrder:=xlByColumns ' 参数MatchCase指定是否区分大小写(MatchCase:=True, "A" <> "a")或(MatchCase:=False,"A" = "a"). ' ' 不需要在模块顶指定"Option Compare Text",如果指定的话,将不会正确执行大小写比较 ' ' 执行单元格中的Text属性比较,而不是Value属性比较 ' 因此,仅比较显示在屏幕中的文本,而不是隐藏在单元格中具体的值 ' ' 如果参数SearchRange是nothing或多个区域,则返回Nothing. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim FoundCells As Range Dim FirstCell As Range Dim LastCell As Range Dim RowNdx As Long Dim ColNdx As Long Dim StartRow As Long Dim EndRow As Long Dim StartCol As Long Dim EndCol As Long Dim WS As Worksheet Dim Rng As Range ' 确保参数SearchRange不是Nothing且是一个单独的区域 If SearchRange Is Nothing Then Exit Function End If If SearchRange.Areas.Count > 1 Then Exit Function End If With SearchRange Set WS = .Worksheet Set FirstCell = .Cells(1) Set LastCell = .Cells(.Cells.Count) End With StartRow = FirstCell.Row StartCol = FirstCell.Column EndRow = LastCell.Row EndCol = LastCell.Column If SearchOrder = xlByRows Then With WS For RowNdx = StartRow To EndRow For ColNdx = StartCol To EndCol Set Rng = .Cells(RowNdx, ColNdx) If MatchCase = False Then ''''''''''''''''''''''''''''''''''' '如果参数MatchCase是False,则将字符串转换成大写 '执行忽略大小写的比较 '因此,MatchCase:=False比MatchCase:=True更慢 ''''''''''''''''''''''''''''''''''' If UCase(Rng.Text) Like UCase(CompareLikeString) Then If FoundCells Is Nothing Then Set FoundCells = Rng Else Set FoundCells = Application.Union(FoundCells, Rng) End If End If Else '''''''''''''''''''''''''''''''''''''''''''''''' ' MatchCase为真,不需要再进行大小写转换,因此更快些 ' 这也是不需要在模块中指定"Option Compare Text"的原因 '''''''''''''''''''''''''''''''''''''''''''''''' If Rng.Text Like CompareLikeString Then If FoundCells Is Nothing Then Set FoundCells = Rng Else Set FoundCells = Application.Union(FoundCells, Rng) End If End If End If Next ColNdx Next RowNdx End With Else With WS For ColNdx = StartCol To EndCol For RowNdx = StartRow To EndRow Set Rng = .Cells(RowNdx, ColNdx) If MatchCase = False Then If UCase(Rng.Text) Like UCase(CompareLikeString) Then If FoundCells Is Nothing Then Set FoundCells = Rng Else Set FoundCells = Application.Union(FoundCells, Rng) End If End If Else If Rng.Text Like CompareLikeString Then If FoundCells Is Nothing Then Set FoundCells = Rng Else Set FoundCells = Application.Union(FoundCells, Rng) End If End If End If Next RowNdx Next ColNdx End With End If If FoundCells Is Nothing Then Set WildCardMatchCells = Nothing Else Set WildCardMatchCells = FoundCells End If End Function
使用示例:
Sub TestWildCardMatchCells() Dim SearchRange As Range Dim FoundCells As Range Dim FoundCell As Range Dim CompareLikeString As String Dim SearchOrder As XlSearchOrder Dim MatchCase As Boolean Set SearchRange = Range("A1:IV65000") CompareLikeString = "A?C*" SearchOrder = xlByRows MatchCase = True Set FoundCells = WildCardMatchCells(SearchRange:=SearchRange, CompareLikeString:=CompareLikeString, _ SearchOrder:=SearchOrder, MatchCase:=MatchCase) If FoundCells Is Nothing Then Debug.Print "没有找到!" Else For Each FoundCell In FoundCells Debug.Print FoundCell.Address, FoundCell.Text Next FoundCell End If End Sub
这样,在找到所需单元格后,就可以对这些单元格进行操作了。

