Kickbutt VBA Find Function - Excel Help & Excel Macro Help
SEARCH ENTIRE SITE LOADING..

Excel Help & Excel Best Practices Forums

 

Proudly Affiliated With: Intelligent Converters & AnalyserXL <Affiliate Program & ExcelUser Affiliate>

 

SPECIALS PAGE FOR BARGAINS | FREE EXCEL TRAINING | FREE CUSTOM FUNCTIONS ADD-IN


Go Back Excel Help & Excel Macro Help > TIP, TRICKS & CODE (NO QUESTIONS) > OPEN SOURCE: Hey! That is Cool!
HOME Register Forum Help Calendar Search For Today's Posts Mark Forums Read

Closed Thread

Kickbutt VBA Find Function

ANSWERS TO SIMILAR QUESTIONS
Find nth value Function



Javascript DHTML Drop Down Menu Powered by dhtml-menu-builder.com

Create Excel dashboards quickly with Plug-N-Play reports.


 
Thread Tools Search this Thread
Old December 9th, 2004
Aaron Blood's Avatar
Aaron Blood Aaron Blood is offline
OzMVP
 
I'm a Spammer:
MS Office Version: 2003
Op System: Windows XP
Assumed Experience: Excel Jedi
Join Date: 10th February 2003
English is 1st Language:
Location: Palm Beach, Florida
Posts: 1,580 -- Threads: 48
Cool Kickbutt VBA Find Function

A Christmas treat for my OZgrid friends.

Wraping the VBA Find/FindNext methods into a function you can use in your code to return found range objects!

This function is awesome, and the uses are many...

VB: AutoLinked keywords will cause extra spaces before keywords. Extra spacing is NOT transferred when copy/pasting, but IS if the keyword uses "quotes".
Function Find_Range(Find_Item As Variant, _ Search_Range As Range, _ Optional LookIn As Variant, _ Optional LookAt As Variant, _ Optional MatchCase As Boolean) As Range Dim c As Range If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole If IsMissing(MatchCase) Then MatchCase = False With Search_Range Set c = .Find( _ What:=Find_Item, _ LookIn:=LookIn, _ LookAt:=LookAt, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=MatchCase, _ SearchFormat:=False) If Not c Is Nothing Then Set Find_Range = c firstAddress = c.Address Do Set Find_Range = Union(Find_Range, c) Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> firstAddress End If End With End Function

This should work in 2002 and later. You may have to tweak it a bit to work with earlier versions. For instance, I don't think SearchFormat is an option for the FIND method in 2000 and earlier. You can wipe that line if needed.


Here's just a few of the potential uses for this function...

Select all cells in a range that contain 22 as part of the value:
Find_Range(22, Range("D10:G20")).Select

Clear the range if the cell contains exactly 999, but if it's a formula leave it be:
Find_Range(999, Range("D10:G20"), xlFormulas, xlWhole).ClearContents

Delete all rows that contain "X" in column A:
Find_Range("X", Columns("A"), MatchCase:=True).EntireRow.Delete

Quickly scan the whole sheet if you like!
Find_Range(1000, Cells, xlFormulas, xlWhole).EntireRow.Select


How often have you seen people asking, "How do I find all rows that match a criteria and paste the results to a new sheet?" Now you can do it in a single line of code. For those who can appreciate it, this last one is pretty amazing!

Copy all the rows that have the value 1000 in column D and paste to Sheet2:
Find_Range(1000, Columns("D"), xlFormulas, xlWhole).EntireRow.Copy Range("Sheet2!A1")
__________________
Sub All_Macros(Optional control As Variant)
Print [Post / Thread]
Old December 9th, 2004
thomach's Avatar
thomach thomach is offline
OzMVP
 
I'm a Spammer: NO
MS Office Version: 2003 English
Op System: Windows XP
Assumed Experience: Expert
Join Date: 22nd April 2003
English is 1st Language: Yes
Location: Virginia, USA
Posts: 5,033 -- Threads: 57
Re: Kickbutt VBA Find Function

Thanks, Aaron,

A really neat X-mas gift. Much appreciated.

Tom
__________________
Best Regards,
Tom
---------------------------
Please help Oz share knowledge among all users by posting your questions in a public forum rather than using a Private Message.
Print [Post / Thread]
Old December 9th, 2004
Brandtrock's Avatar
Brandtrock Brandtrock is offline
Long Term Member
 
I'm a Spammer:
MS Office Version: Excel 97, 2000, 2003
Op System: Windows XP
Assumed Experience: Expert (Know VBA Well and Use It & Excel Almost Da
Join Date: 20th May 2003
English is 1st Language: Yes
Location: Mesa, Az
Posts: 1,116 -- Threads: 34
Re: Kickbutt VBA Find Function

Better than a Taun-taun Aaron!!

Pretty cool!

Regards,
Print [Post / Thread]
Old December 9th, 2004
Aaron Blood's Avatar
Aaron Blood Aaron Blood is offline
OzMVP
 
I'm a Spammer:
MS Office Version: 2003
Op System: Windows XP
Assumed Experience: Excel Jedi
Join Date: 10th February 2003
English is 1st Language:
Location: Palm Beach, Florida
Posts: 1,580 -- Threads: 48
Re: Kickbutt VBA Find Function

And for the truly brave, you could even do a find/copy/paste-append to a different sheet as a one-liner!

Find_Range(1000, Columns("D"), xlFormulas, xlWhole).EntireRow.Copy Range("Sheet2!D65536").End(xlUp).Offset(1, 0).EntireRow


I tested this one on a 10,000 dataset and for 100 finds in the 10,000 block it was nearly instantaneous. On 65,536 records with 650 finds it took less than 5 seconds.


This next one is a tad abstract, but if you need to perform the above operation and search multiple columns the one-liner above MIGHT (yes it depends) fail if I just changed the columns reference to "A:D". In this case, a union would take care of it.

Set Found_Range = Find_Range(1000, Columns("A:D"), xlFormulas, xlWhole).EntireRow
Union(Found_Range, Found_Range).Copy Range("Sheet2!A65536").End(xlUp).Offset(1, 0).EntireRow


In this case, the union is resolving overlapping range issues created because a matching value was found in multiple columns of the same row. If there are no instances of the found item existing in multiple columns of the same row, this is not an issue. Notice for the above example this works:
Union(Found_Range, Found_Range).Copy

While this alone would fail:
Found_Range.Copy

That is, it would fail if there were multiple occurances of the value within the row.

Similarly, you can imagine how you'd have to also use the union if you were returning entire columns with this function.

Enjoy!
-AB
__________________
ADS BY OZGRID. FREE DOWNLOADS SmartVBA | SmartVB6 | CodeVBA | CodeVB6 | Code Generator Pro | TraderXL Pro Package. MORE..
Print [Post / Thread]
Old December 9th, 2004
Dave Hawley's Avatar
Dave Hawley Dave Hawley is offline
Administrator
 
I'm a Spammer: NO
MS Office Version: 2007 English
Op System: Windows Vista
Assumed Experience: Above Average
Join Date: 24th January 2003
English is 1st Language: Yes
Location: Australia
Posts: 49,226 -- Threads: 15169
Re: Kickbutt VBA Find Function

Cool indeed Aaron, thanks for sharing!
Print [Post / Thread]
Old December 10th, 2004
Intelligent Converters
FREE DOWNLOADS
File/Password Recovery
jwaldon jwaldon is offline
I agreed to these rules
 
I'm a Spammer:
MS Office Version: 2003
Op System: Windows 2000
Assumed Experience: Just Above Average (Can Record Macros &amp; Know Most of Excel)
Join Date: 10th December 2004
English is 1st Language:
Posts: 4 -- Threads: 1
Re: Kickbutt VBA Find Function

Is there anyway to use this with a number that is not fixed? Such as anything greater than 5000?

Thanks,
Josh
__________________
ADS BY OZGRID. FREE DOWNLOADS SmartVBA | SmartVB6 | CodeVBA | CodeVB6 | Code Generator Pro | TraderXL Pro Package. MORE..
Print [Post / Thread]
Old December 11th, 2004
Aaron Blood's Avatar
Aaron Blood Aaron Blood is offline
OzMVP
 
I'm a Spammer:
MS Office Version: 2003
Op System: Windows XP
Assumed Experience: Excel Jedi
Join Date: 10th February 2003
English is 1st Language:
Location: Palm Beach, Florida
Posts: 1,580 -- Threads: 48
Re: Kickbutt VBA Find Function

Not really... it works just like the find feature on the menu toolbar. There are other VBA methods you can use to test cell values.


Quote:
Originally Posted by jwaldon
Is there anyway to use this with a number that is not fixed? Such as anything greater than 5000?

Thanks,
Josh
__________________
Sub All_Macros(Optional control As Variant)
Print [Post / Thread]
Old January 8th, 2005
Ivan F Moala's Avatar
Ivan F Moala Ivan F Moala is offline
Established Member
 
I'm a Spammer: NO
MS Office Version: 2003 English
Op System: Windows XP
Assumed Experience: What's Excel?
Join Date: 27th January 2003
English is 1st Language: Yes
Location: Auckland, New Zealand
Posts: 396 -- Threads: 6
Re: Kickbutt VBA Find Function

Hi Aaron, just a couple of things;

1) FirstAddress wasn't defined.

2) I like to use functions where you are given choices rather then guess @ the expected values i.e LookIn = xlValues 'xlFormulas the use wil be expected to places these constant in.

To this end (For Excel2000+) I use Enum like so ......
With Enum writing the Code gives the user intellisence options for the Consts.

Don't take this the wrong way, it is a nice function... one that I will use

Any way for xl2000+ this is how I will use it >

VB: AutoLinked keywords will cause extra spaces before keywords. Extra spacing is NOT transferred when copy/pasting, but IS if the keyword uses "quotes".
Enum eLookin xl_Formulas = -4123 xl_Comments = -4144 xl_Values = -4163 End Enum Enum eLookat xl_Part = 2 xl_Whole = 1 End Enum Function Find_Range(Find_Item As Variant, _ Search_Range As Range, _ Optional LookIn As eLookin, _ Optional LookAt As eLookat, _ Optional MatchCase As Boolean) As Range Dim c As Range, FirstAddress As String '<< If IsMissing(LookIn) Then LookIn = xlValues 'xlFormulas If IsMissing(LookAt) Then LookAt = xlPart 'xlWhole If IsMissing(MatchCase) Then MatchCase = False With Search_Range Set c = .Find( _ What:=Find_Item, _ LookIn:=LookIn, _ LookAt:=LookAt, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=MatchCase, _ SearchFormat:=False) If Not c Is Nothing Then Set Find_Range = c FirstAddress = c.Address Do Set Find_Range = Union(Find_Range, c) Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> FirstAddress End If End With End Function
__________________
Kind Regards,
Ivan F Moala From the City of Sails

http://www.xcelfiles.com
Print [Post / Thread]
Old January 10th, 2005
Aaron Blood's Avatar
Aaron Blood Aaron Blood is offline
OzMVP
 
I'm a Spammer:
MS Office Version: 2003
Op System: Windows XP
Assumed Experience: Excel Jedi
Join Date: 10th February 2003
English is 1st Language:
Location: Palm Beach, Florida
Posts: 1,580 -- Threads: 48
Re: Kickbutt VBA Find Function

Quote:
Originally Posted by Ivan F Moala
Hi Aaron, just a couple of things;

1) FirstAddress wasn't defined.

2) I like to use functions where you are given choices rather then guess @ the expected values i.e LookIn = xlValues 'xlFormulas the use wil be expected to places these constant in.

To this end (For Excel2000+) I use Enum like so ......
With Enum writing the Code gives the user intellisence options for the Consts.

Don't take this the wrong way, it is a nice function... one that I will use


Good idea Ivan.
I think I'll incorporate the Enum as well.
__________________
Sub All_Macros(Optional control As Variant)
Print [Post / Thread]
Old January 14th, 2005
Intelligent Converters
FREE DOWNLOADS
File/Password Recovery
v8nsx v8nsx is offline
I agreed to these rules
 
I'm a Spammer:
MS Office Version: 2003
Op System: Windows 2000
Assumed Experience: Just above poor (Basic Formulas SUM etc)
Join Date: 13th January 2005
English is 1st Language:
Posts: 8 -- Threads: 2
Re: Kickbutt VBA Find Function

Is there a quick one liner that would take out all the lines starting with a string (word?) or symbols ("----")? if so what should I put at the the place "what"?

Find_Range(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)


Thanks for the great code. I'll use it a lot!!!!
__________________
ADS BY OZGRID. FREE DOWNLOADS SmartVBA | SmartVB6 | CodeVBA | CodeVB6 | Code Generator Pro | TraderXL Pro Package. MORE..
Print [Post / Thread]
Closed Thread Lifetime Upgrade To Ad Free Styles

   « PREVIOUS Gantt Chart With Sliding Time Scale || Synchronise Appearance Of Charts NEXT »
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +9. The time now is 10:05.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ozgrid is Not Associated With Microsoft. Ozgrid Retains the Rights to ALL Posts and Threads