PC Review


Reply
Thread Tools Rate Thread

Determine if item is in a range of items

 
 
Mark
Guest
Posts: n/a
 
      28th Feb 2008
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.
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      28th Feb 2008
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.

 
Reply With Quote
 
Gary''s Student
Guest
Posts: n/a
 
      28th Feb 2008
You can try to Find the value in the range:

Sub Macro1()

Set r = Range("A14,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.

 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      28th Feb 2008
Thanks

"Gary''s Student" wrote:

> You can try to Find the value in the range:
>
> Sub Macro1()
>
> Set r = Range("A14,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.

 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      28th Feb 2008
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.

 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      28th Feb 2008
On Thu, 28 Feb 2008 05:08:01 -0800, Mark <(E-Mail Removed)>
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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine if an Item is New =?Utf-8?B?ZGNoMw==?= Microsoft Outlook Form Programming 1 26th Jul 2007 03:17 AM
Determine if range has NO Blank Cells without looping through each cell in range Excelenator Microsoft Excel Programming 4 4th Aug 2006 06:30 AM
Determine if an item is in a Collection =?Utf-8?B?am1obWFpbmU=?= Microsoft VB .NET 3 19th Jul 2005 07:37 AM
Ho to determine the type of item? Alan Microsoft Outlook VBA Programming 2 13th Oct 2004 10:02 AM
Determine whether the item is open or not? =?Utf-8?B?Sm9obiBSaWRkbGU=?= Microsoft Outlook Form Programming 1 16th Jul 2004 08:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:50 PM.