Excel

Returns All Matching Ranges

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

sebastienm

Description:

This FindAll function takes the same parameters as the Find method of the range object and returns all the matching cells in a single range result. This code is mostly useful for coders and not Excel end-users. 

Discussion:

This is a general-purpose search function. You can use it to, for example, search all cells containing a specific substring for later clearing their contents in one single line of code. You can also use it to search all cells in column A for values 'HI' in uppercase only for later copy their entire row at the end of the current data in another sheet in just 1 line of code. It needs to be called from a wrapping sub which passes all paramaters for a specific search. The FindAll function is very fast because it does not use a vba-for-loop. It is even more noticeable when working with large ranges. It also enables you to work on the whole result range directly and not on each cell of that range individually. The search value is passed as variant, therefore you can use clng(), cstr(), cdate(), datevalue() to explicitely search for a data type, which can be very convenient or even mandatory with dates. 

Code:

instructions for use

			

'WRAPPING SUB EXAMPLE 1 'Clear all contents of cells containing a specific substring. The sub bellow uses FindAll: Public Sub BlankCellsContainingString() Dim RgToSearch As Range 'Range to search Dim CharToFind As String 'String to find Dim rg As Range '------ CHANGE HERE ------ Set RgToSearch = ActiveSheet.Range("A:A") 'Search in A:A CharToFind = ">" 'search for the '>' character '------------------------- Set rg = FindAll(CharToFind, RgToSearch, xlValues, xlPart) If Not rg Is Nothing Then rg.ClearContents 'clear result range End If End Sub 'WRAPPING SUB - EXAMPLE 2 'Search cells in column A of value 'HI' in uppercase only. Copy their entire row at the end 'of the current data in sheet 2. Sub CopyCellsContainingString() Dim RgToSearch As Range Dim CharToFind As String Dim RgDestination As Range Dim rg As Range '------ change here ------ Set RgToSearch = ActiveSheet.Range("A:A") 'search in A:A CharToFind = "HI" 'Search for HI Set RgDestination = ActiveWorkbook.Worksheets(2).Range("A65536") _ .End(xlUp).Offset(1, 0).EntireRow '------------------------- Set rg = FindAll(CharToFind, RgToSearch, xlValues, xlWhole, True, False) If Not rg Is Nothing Then rg.EntireRow.Copy RgDestination 'copy resulting rows in RgDestination End If End Sub '--------------------------------------------------------------------------------------------------------------------- 'FUNCTION FINDALL : Find all macthing cells and return them into one single range. ' What: what to search for ' Where: range to search ' LookIn: look into xlComments, xlFormulas, or xlValues (default) ' LookAt: look at xlPart (default) or xlWhole ' MatchCase: True or False (default) ' MatchByte: True or False (default) ' Assumes: neither What or Where is Nothing. Public Function FindAll( _ What As Variant, _ Where As Range, _ Optional LookIn As XlFindLookIn = xlValues, _ Optional LookAt As XlLookAt = xlPart, _ Optional MatchCase As Boolean = False, _ Optional MatchByte As Boolean = False) As Range ' 2002 - Sebastien Mistouflet Dim ResultRg As Range Dim rg As Range Dim firstAddress As String With Where Set rg = .Find(What, LookIn:=LookIn, LookAt:=LookAt, _ MatchCase:=MatchCase, MatchByte:=MatchByte) If Not rg Is Nothing Then Set ResultRg = rg firstAddress = rg.Address Do Set ResultRg = Application.Union(ResultRg, rg) Set rg = .FindNext(rg) Loop While Not rg Is Nothing And rg.Address <> firstAddress End If End With Set FindAll = ResultRg 'Range to return End Function '-------------------------------------------------------------------------------------------------------------

How to use:

  1. Make sure the book you want to contain the code is open
  2. Go to the VBA Editor: menu Tools > Macros > Visual Basic (or ALT+F11)
  3. In the VB Editor, add a new general Module: menu Insert > Module
  4. Paste the Code section from this page to the module
  5. Close the VB Editor: menu File > Close or ALT+Q
 

Test the code:

  1. Paste the code in a module (see HowToUse section above)
  2. In excel, make sure you have data in column A of sheet1 that will match partially the '>' character, and fully the word 'HI' uppercase.
  3. Run the macros CopyCellsContainingString or BlankCellsContainingString: menu Tools > Macro > Macros or ALT+F8
 

Sample File:

No Attachment 

Approved by Anne Troy


This entry has been viewed 241 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express