PC Review


Reply
Thread Tools Rate Thread

Check if value does not exist in range.

 
 
Juan Correa
Guest
Posts: n/a
 
      10th Mar 2010
Hello

I have a small question for the gurus here:

I have a spreadsheet with Data. The data is stored in columns A through P
with column labels in row 1. So my column lables are A1:P1

I know how to use:
Cells.Find(What:="SomeColumnLable").Column
To determine which column any particular bit of information is located at.

My question is:
Is there a way that I can use the Cells.Find to check if a column label does
not exist in the range?

Thanks
JC
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      10th Mar 2010
You might find a call to this function simpler to use within your own
code...

Function IsColumnLabel(Lbl As String, Addr As String) As Boolean
Dim R As Range
On Error Resume Next
Set R = Range(Addr).Find(What:=Lbl, LookAt:=xlWhole, MatchCase:=False)
IsColumnLabel = Not R Is Nothing
End Function

Just pass the function the label text and the range address as a string. For
example...

MsgBox IsColumnLabel("SomeColumnLabel", "A1:P1")

and it will respond True if the label text exists within the specified range
and False otherwise.

--
Rick (MVP - Excel)


"Juan Correa" <(E-Mail Removed)> wrote in message
news:1E2C1A99-346E-4709-BF06-(E-Mail Removed)...
> Hello
>
> I have a small question for the gurus here:
>
> I have a spreadsheet with Data. The data is stored in columns A through P
> with column labels in row 1. So my column lables are A1:P1
>
> I know how to use:
> Cells.Find(What:="SomeColumnLable").Column
> To determine which column any particular bit of information is located at.
>
> My question is:
> Is there a way that I can use the Cells.Find to check if a column label
> does
> not exist in the range?
>
> Thanks
> JC


 
Reply With Quote
 
tompl
Guest
Posts: n/a
 
      10th Mar 2010
Try:

Cells.Find(What:="").Column

Tom
 
Reply With Quote
 
Juan Correa
Guest
Posts: n/a
 
      11th Mar 2010
Thank you all for your responses.

Joel's approach is the one that works best for my particular project where I
need to check for the non-existance of a particular label and execute code
based on that. So Joel gets the check-mark this time.

Thanks again
JC

"Rick Rothstein" wrote:

> You might find a call to this function simpler to use within your own
> code...
>
> Function IsColumnLabel(Lbl As String, Addr As String) As Boolean
> Dim R As Range
> On Error Resume Next
> Set R = Range(Addr).Find(What:=Lbl, LookAt:=xlWhole, MatchCase:=False)
> IsColumnLabel = Not R Is Nothing
> End Function
>
> Just pass the function the label text and the range address as a string. For
> example...
>
> MsgBox IsColumnLabel("SomeColumnLabel", "A1:P1")
>
> and it will respond True if the label text exists within the specified range
> and False otherwise.
>
> --
> Rick (MVP - Excel)
>
>
> "Juan Correa" <(E-Mail Removed)> wrote in message
> news:1E2C1A99-346E-4709-BF06-(E-Mail Removed)...
> > Hello
> >
> > I have a small question for the gurus here:
> >
> > I have a spreadsheet with Data. The data is stored in columns A through P
> > with column labels in row 1. So my column lables are A1:P1
> >
> > I know how to use:
> > Cells.Find(What:="SomeColumnLable").Column
> > To determine which column any particular bit of information is located at.
> >
> > My question is:
> > Is there a way that I can use the Cells.Find to check if a column label
> > does
> > not exist in the range?
> >
> > Thanks
> > JC

>
> .
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      11th Mar 2010
You might want to consider substituting your exact range (A1:P1) in place of
joel's more general Cells reference just in case your last use of Find
searched by columns instead of by rows; otherwise the Find function will
search every cell in each column until it either finds the label text or
runs out of columns to search. Alternately, you can specify the xlByRows
parameter of the XlSearchOrder argument in joel's function call to make sure
it searches Row 1 first, although if the label is not there, joel's function
call will still do more work than if you specify the range directly as the
search would go through every cell on Row 1 until it runs out of columns.

--
Rick (MVP - Excel)


"Juan Correa" <(E-Mail Removed)> wrote in message
news:FC3CD7CB-BA3E-4AE8-B8A7-(E-Mail Removed)...
> Thank you all for your responses.
>
> Joel's approach is the one that works best for my particular project where
> I
> need to check for the non-existance of a particular label and execute code
> based on that. So Joel gets the check-mark this time.
>
> Thanks again
> JC
>
> "Rick Rothstein" wrote:
>
>> You might find a call to this function simpler to use within your own
>> code...
>>
>> Function IsColumnLabel(Lbl As String, Addr As String) As Boolean
>> Dim R As Range
>> On Error Resume Next
>> Set R = Range(Addr).Find(What:=Lbl, LookAt:=xlWhole, MatchCase:=False)
>> IsColumnLabel = Not R Is Nothing
>> End Function
>>
>> Just pass the function the label text and the range address as a string.
>> For
>> example...
>>
>> MsgBox IsColumnLabel("SomeColumnLabel", "A1:P1")
>>
>> and it will respond True if the label text exists within the specified
>> range
>> and False otherwise.
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>> "Juan Correa" <(E-Mail Removed)> wrote in message
>> news:1E2C1A99-346E-4709-BF06-(E-Mail Removed)...
>> > Hello
>> >
>> > I have a small question for the gurus here:
>> >
>> > I have a spreadsheet with Data. The data is stored in columns A
>> > through P
>> > with column labels in row 1. So my column lables are A1:P1
>> >
>> > I know how to use:
>> > Cells.Find(What:="SomeColumnLable").Column
>> > To determine which column any particular bit of information is located
>> > at.
>> >
>> > My question is:
>> > Is there a way that I can use the Cells.Find to check if a column label
>> > does
>> > not exist in the range?
>> >
>> > Thanks
>> > JC

>>
>> .
>>


 
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
A macro to check range and then check another range if falset-then Jim A Microsoft Excel Discussion 2 13th Jan 2009 06:09 PM
Check for a tab if it is exist Farhad Microsoft Excel Misc 2 21st Dec 2008 07:31 PM
Compare a selected Range with a Named range and select cells that do not exist PCLIVE Microsoft Excel Programming 1 18th Oct 2005 07:09 PM
check SQL db exist... =?Utf-8?B?Sm9u?= Microsoft VB .NET 3 17th May 2005 12:57 PM
Check if value exist Daniel Microsoft Access Form Coding 2 14th Oct 2003 10:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:33 AM.