Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Determine if item is in a range of items

I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Determine if item is in a range of items

Mark,

You don't say what you want to do if you find it so this generates a
messagebox. Right click the sheet tab, view code and paste this in

Sub Mersible()
Dim MyRange As Range
Set MyRange = Range("B1:B10")
For Each c In MyRange
If c.Text = Range("A1").Text Then
MsgBox Range("A1").Text & " at " & c.Address
Exit Sub
End If
Next
MsgBox Range("A1").Text & " Not found "
End Sub

You correct about the one liner because this is easily dooable using a
worksheet function:-
=COUNTIF(B1:B10,A1)0

Mike
"Mark" wrote:

I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Determine if item is in a range of items

You can try to Find the value in the range:

Sub Macro1()

Set r = Range("A1:D4,G10:H11")
On Error GoTo qwerty
r.Find(What:="1", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate
MsgBox ("it is there")
Exit Sub
qwerty:
MsgBox ("it is not there")

End Sub

--
Gary''s Student - gsnu200770


"Mark" wrote:

I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Determine if item is in a range of items

Thanks

"Gary''s Student" wrote:

You can try to Find the value in the range:

Sub Macro1()

Set r = Range("A1:D4,G10:H11")
On Error GoTo qwerty
r.Find(What:="1", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate
MsgBox ("it is there")
Exit Sub
qwerty:
MsgBox ("it is not there")

End Sub

--
Gary''s Student - gsnu200770


"Mark" wrote:

I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 989
Default Determine if item is in a range of items

Thanks

"Mike H" wrote:

Mark,

You don't say what you want to do if you find it so this generates a
messagebox. Right click the sheet tab, view code and paste this in

Sub Mersible()
Dim MyRange As Range
Set MyRange = Range("B1:B10")
For Each c In MyRange
If c.Text = Range("A1").Text Then
MsgBox Range("A1").Text & " at " & c.Address
Exit Sub
End If
Next
MsgBox Range("A1").Text & " Not found "
End Sub

You correct about the one liner because this is easily dooable using a
worksheet function:-
=COUNTIF(B1:B10,A1)0

Mike
"Mark" wrote:

I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Determine if item is in a range of items

On Thu, 28 Feb 2008 05:08:01 -0800, Mark
wrote:

I'm wanting to check if a value in one cell is in a range of other cells.
(For example, A1 contains "dog" and B1 through B10 contains a list of various
animals. I want to know if "dog" in the list.) I know I can program a loop
and compare item-by-item, but I'm guessing there's a couple of one-liners
that will do it more efficiently, like loading the range of values in a
collection then inquiring if the single cell value is in the collection.
Cannot seem to find anything in Help to steer me in the right direction.


You can use the VBA FIND method:

==================
Sub foo()
'"Rng" is a named range on the active worksheet
'containing the list of items

Const LookFor As String = "dog"
Dim ItemPresent As Boolean

If Range("Rng").Find(LookFor) Is Nothing Then
ItemPresent = False
Else
ItemPresent = True
End If

Debug.Print LookFor, ItemPresent
End Sub
==============

So in your case

LookFor = [A1].value

and

you could substitute "B1:B10" for Rng
--ron
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
Find all item codes for all items under one category Kevin K[_2_] Excel Discussion (Misc queries) 3 December 25th 09 07:46 AM
remove items from listbox together with add item [email protected] Excel Programming 7 September 23rd 07 01:18 PM
number of items fit into item Dreamstar_1961 Excel Worksheet Functions 9 May 6th 07 03:33 PM
determine if Pivot Table field item is hidden badger_rich Excel Programming 2 April 20th 07 06:34 PM
How do i sort items using two rows per item in Excel? primus4000 Excel Discussion (Misc queries) 1 July 1st 05 12:16 PM


All times are GMT +1. The time now is 04:11 AM.

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"