LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default searching array for text from array

Here is an example - next to the value in B, it puts the row of match in A

Sub SearchFor()
Dim rng1 as Range, rng2 as Range
Dim rng3 as Range, cell as Range
Dim res as Variant
set rng1 = Range(cells(1,1),Cells(1,1).End(xldown))
set rng2 = Range(cells(1,2),Cells(1,2).End(xldown))

for each cell in rng1
cell.Select
res = Application.Match(cell,rng2,0)
if not iserror(res) then
set rng3 = rng2(res)
rng3.offset(0,1).Value = cell.row
else
cell.Interior.colorIndex = 3
end if
Next
End Sub

this one puts the row in B in the same row as the value in A

Sub SearchFor()
Dim rng1 as Range, rng2 as Range
Dim rng3 as Range, cell as Range
Dim res as Variant
set rng1 = Range(cells(1,1),Cells(1,1).End(xldown))
set rng2 = Range(cells(1,2),Cells(1,2).End(xldown))

for each cell in rng1
cell.Select
res = Application.Match(cell,rng2,0)
if not iserror(res) then
set rng3 = rng2(res)
cell.offset(0,2).Value = rng3.Row
else
cell.Interior.colorIndex = 3
end if
Next
End Sub

http://www.j-walk.com/ss/excel and look on the left at the link for Books.
His power programming book for the appropriate version of Excel is good.

--
Regards,
Tom Ogilvy


" wrote:

Tom,

That's great! My knowledge of Excel is too simplistic. I've got two
followup questions for you. Could you tell me how to set this up to
report the location into column C? I'm going to run a query array of
60 vs a searched array of about a thousand cells. Having a message box
is good, but it will become tedious.

Secondly, do you recommend a good VBA/Excel book? I'm a biochemsity
with no programming knowledge, but can write basic macros. I'd like to
be able to do more, but find programming books difficult to interpret
with my lack of computer knowledge.

Thanks,
jordan


Tom Ogilvy wrote:
Here is a different interpretation:


Sub SearchFor()
Dim rng1 as Range, rng2 as Range
Dim rng3 as Range, cell as Range
Dim res as Variant
set rng1 = Range(cells(1,1),Cells(1,1).End(xldown))
set rng2 = Range(cells(1,2),Cells(1,2).End(xldown))

for each cell in rng1
cell.Select
res = Application.Match(cell,rng2,0)
if not iserror(res) then
set rng3 = rng2(res)
msgbox cell & " value found at " & rng3.Address
else
msgbox cell & " value not found"
end if
Next
End Sub

--
Regards,
Tom Ogilvy


"skyboy_psu" wrote in message
ups.com...
Hey all,

Here is what I'm trying to write a macro to do.
I have a column, A, of text. I want to to search/find where this text
exists in a separate range. I am having trouble getting the macro to
shift the active cell and then search for that term.
So, I first want to search range B1:B6 for "dog" and get a report of
it's location. Then, I want the macro to shift my active cell to A2
and search B1:B6 for "cat". There will be cases where the search term
(ie
"cat") doesn't exist.

however, all I get is:
Columns("A:A").EntireColumn.Select
Selection.FindNext(After:=ActiveCell).Activate

and this doesn't give me the option to move the active cell (the search
criteria).

A B
1 dog cat
2 cat horse
3 horse elephant
4 sheep cow
5 cow cat
6 pig dog



Or, a simple IF/search formula would work, but I can't get that to work
either. For example, I can't get a correct answer for Search(A1,
B$1:B$100), even though I know the values exist.

thanks,

Jordan





 
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
Searching for data in an array. Tom Excel Discussion (Misc queries) 5 July 9th 09 12:56 PM
Searching/Counting an array Greg2582 Excel Discussion (Misc queries) 0 March 8th 07 07:12 PM
Searching for a value in an Array Farah Excel Worksheet Functions 16 August 25th 06 04:53 PM
searching in an array sisko101 Excel Discussion (Misc queries) 1 August 4th 05 10:30 PM
Searching an Array Otto Moehrbach[_6_] Excel Programming 7 August 17th 04 04:27 PM


All times are GMT +1. The time now is 09:44 PM.

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

About Us

"It's about Microsoft Excel"