Excel

One Line Find

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

johnske

Description:

Finds cells with chosen values. 

Discussion:

The normal Find method has too many parameters and is too complicated for a lot of people. These three functions (FindPart, FindWhole, and FindDates) each have two simple parameters "what are you looking for" as a string, and, "where do you want to look" as a range, that can be used as a single line "Find" that returns the found cell addresses as a string. The functions themselves can be kept handy and pasted into a module whenever a "Find" is required in a procedure. (Note that as the property calls are performed as a single action on the found ranges from within the procedure itself and not one at a time while in the Find functions Do Loop, this should be a little faster than a 'normal' Find used in a procedure). 

Code:

instructions for use

			

'<< Some example usages given in the attached workbook >> Option Explicit '--------------------------------------------------------------------------------------- 'FUNCTION : FindWhole 'DATE/TIME : 25 Dec 05 22:16 'PURPOSE : Function to find xlWhole strings. 'LIMITATIONS : None known '--------------------------------------------------------------------------------------- Public Function FindWhole(ByRef FindWhat As String, Optional LookInRange As Range) As String If LookInRange Is Nothing Then Set LookInRange = ActiveSheet.Cells Dim Target As Range, FirstAddress As String With LookInRange Set Target = .Find(FindWhat, LookIn:=xlValues, SearchOrder:=xlByRows, _ LookAt:=xlWhole, MatchCase:=False) If Not Target Is Nothing Then FirstAddress = Target.Address '<< bookmark the start-point of the search Do If Target Is Nothing Or Target.Address = FirstAddress Then FindWhole = FindWhole & Target.Address Else FindWhole = FindWhole & ", " & Target.Address End If Set Target = .FindNext(Target) Loop Until Target Is Nothing Or Target.Address = FirstAddress End If End With Set LookInRange = Nothing Set Target = Nothing End Function '--------------------------------------------------------------------------------------- 'FUNCTION : FindPart 'DATE/TIME : 25 Dec 05 22:17 'PURPOSE : Function to find xlPart strings. 'LIMITATIONS : None known '--------------------------------------------------------------------------------------- Public Function FindPart(ByRef WhatPart As String, Optional LookInRange As Range) As String If LookInRange Is Nothing Then Set LookInRange = ActiveSheet.Cells Dim Target As Range, FirstAddress As String With LookInRange Set Target = .Find(WhatPart, LookIn:=xlValues, SearchOrder:=xlByRows, _ LookAt:=xlPart, MatchCase:=False) If Not Target Is Nothing Then FirstAddress = Target.Address '<< bookmark the start-point of the search Do If Target Is Nothing Or Target.Address = FirstAddress Then FindPart = FindPart & Target.Address Else FindPart = FindPart & ", " & Target.Address End If Set Target = .FindNext(Target) Loop Until Target Is Nothing Or Target.Address = FirstAddress End If End With Set LookInRange = Nothing Set Target = Nothing End Function '--------------------------------------------------------------------------------------- 'FUNCTION : FindDates 'DATE/TIME : 25 Dec 05 22:20 'PURPOSE : Function to find dates. 'LIMITATIONS : None known '--------------------------------------------------------------------------------------- Public Function FindDates(ByRef FindDate As Date, Optional LookInRange As Range) As String If LookInRange Is Nothing Then Set LookInRange = ActiveSheet.Cells Dim Target As Range, FirstAddress As String With LookInRange Set Target = .Find(FindDate, LookIn:=xlFormulas, SearchOrder:=xlByRows) If Not Target Is Nothing Then FirstAddress = Target.Address '<< bookmark the start-point of the search Do If Target Is Nothing Or Target.Address = FirstAddress Then FindDates = FindDates & Target.Address Else FindDates = FindDates & ", " & Target.Address End If Set Target = .FindNext(Target) Loop Until Target Is Nothing Or Target.Address = FirstAddress End If End With Set LookInRange = Nothing Set Target = Nothing End Function

How to use:

  1. Open a workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the code into the Module
  5. Write your code
  6. Now select File/Close and return to the main application
  7. Dont forget to save your changes...
 

Test the code:

  1. Download the attachment to view some example uses...
 

Sample File:

One-Line Find Function.zip 18.09KB 

Approved by mdmackillop


This entry has been viewed 406 times.

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