Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Most efficient way to search a range

I have a named range with eight columns and 3700 plus rows. I need a fast
and efficient manner of searching for all incidents of a given name in column
E and return the values of column B for each row where a column E value
matched the search name. I've kludged some code that works, but it's takes
way too long to return results. Any help or direction would be greatly
appreciated.

Ken Warthen
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Most efficient way to search a range

I usually import all the data into memory to speed things up, rather than
referencing cells and ranges directly. When I'm done, I can quickly blast
the data back onto the worksheet, or onto a new sheet, etc. Here's some
sample code of what I would do.

HTH,

Eric

Option Explicit
Option Base 1
'
Sub Test_It()
Call Find_Name("Joe2") ' I made up data...
End Sub
'
Sub Find_Name(theName)
Dim i As Long, j As Long
Dim nRows As Long, nCols As Long
Dim theData() As Variant
Dim resultVector() As Variant
'
ActiveSheet.Cells(1, 1).Select
nRows = ActiveCell.CurrentRegion.Rows.Count
nCols = ActiveCell.CurrentRegion.Columns.Count
'
ReDim theData(nRows, nCols)
ReDim resultVector(2, 1)
'
' Grab the entire range and store in memory
'
theData = ActiveSheet.Range(ActiveSheet.Cells(1, 1), _
ActiveSheet.Cells(nRows, nCols)).Value
'
' Search through theData to find the name of interest...
'
j = 0
For i = 2 To nRows
If (StrComp(theName, theData(i, 5), vbBinaryCompare) = 0) Then
j = j + 1
ReDim Preserve resultVector(2, j)
resultVector(1, j) = i ' Save which row the item was on
resultVector(2, j) = theData(i, 2) ' Whatever is in Column B
End If
Next i
'
' Do something with the results in 'resultVector'...
'
End Sub


"Ken Warthen" wrote:

I have a named range with eight columns and 3700 plus rows. I need a fast
and efficient manner of searching for all incidents of a given name in column
E and return the values of column B for each row where a column E value
matched the search name. I've kludged some code that works, but it's takes
way too long to return results. Any help or direction would be greatly
appreciated.

Ken Warthen

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Most efficient way to search a range

Ken,

Using the built-in Find function on a range is much faster than using
VBA to examine the contents of each cell. I have at
http://www.cpearson.com/excel/FindAll.aspx a function named FindAll
that searches a specified range for some value and returns a Range
object containing the cells in which the value was found. Using that,
you can easily get the reference to column A of the row of each found
cell. For example,


Dim R As Range
Dim FoundCells As Range
Set FoundCells = FindAll(Range("YourRange").Columns(5), "findwhat")
If FoundCells Is Nothing Then
Debug.Print "not found"
Else
For Each R In FoundCells
Debug.Print R.EntireRow.Cells(1,"A").Value
Next R
End If

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Tue, 21 Apr 2009 08:25:01 -0700, Ken War then
wrote:

I have a named range with eight columns and 3700 plus rows. I need a fast
and efficient manner of searching for all incidents of a given name in column
E and return the values of column B for each row where a column E value
matched the search name. I've kludged some code that works, but it's takes
way too long to return results. Any help or direction would be greatly
appreciated.

Ken Warthen

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Most efficient way to search a range Dan Thompson Excel Programming 0 April 21st 09 04:38 PM
most efficient large range summing? jonigr Excel Programming 5 May 10th 07 06:26 AM
more efficient way to lookup a range? [email protected] Excel Discussion (Misc queries) 1 May 12th 06 05:41 AM
Efficient STRING search with SpecialCells Ilan[_2_] Excel Programming 1 February 18th 04 01:49 PM
Efficient Search / Find MSNEWS Excel Programming 1 August 27th 03 08:34 AM


All times are GMT +1. The time now is 12:32 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"