Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Force VBA countif to find string, not value

Excel 2003

In a VBA routine, I'm trying to do a simple countif.

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then

This evaluates to TRUE when I expect FALSE.

In the column it's evaluating, I have...

A
10020
10021
10022

It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.

How do I force the countif to look for the text string, not the value?

Thanks,
Chuck

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Force VBA countif to find string, not value

On Sun, 02 Sep 2007 16:23:23 -0700, c mateland wrote:

Excel 2003

In a VBA routine, I'm trying to do a simple countif.

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then

This evaluates to TRUE when I expect FALSE.

In the column it's evaluating, I have...

A
10020
10021
10022

It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.

How do I force the countif to look for the text string, not the value?

Thanks,
Chuck


Not as fast as COUNTIF, but it should work:

For Each c In Range("a:a")
If c.Text = "010020" Then


--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Force VBA countif to find string, not value

On Sun, 02 Sep 2007 20:39:10 -0400, Ron Rosenfeld
wrote:

On Sun, 02 Sep 2007 16:23:23 -0700, c mateland wrote:

Excel 2003

In a VBA routine, I'm trying to do a simple countif.

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then

This evaluates to TRUE when I expect FALSE.

In the column it's evaluating, I have...

A
10020
10021
10022

It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.

How do I force the countif to look for the text string, not the value?

Thanks,
Chuck


Not as fast as COUNTIF, but it should work:

For Each c In Range("a:a")
If c.Text = "010020" Then


--ron


Or, as was mentioned in another thread by a different Ron (Coderre)

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020*") 0 Then


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Force VBA countif to find string, not value

On Sun, 02 Sep 2007 21:46:19 -0400, Ron Rosenfeld
wrote:

On Sun, 02 Sep 2007 20:39:10 -0400, Ron Rosenfeld
wrote:

On Sun, 02 Sep 2007 16:23:23 -0700, c mateland wrote:

Excel 2003

In a VBA routine, I'm trying to do a simple countif.

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then

This evaluates to TRUE when I expect FALSE.

In the column it's evaluating, I have...

A
10020
10021
10022

It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.

How do I force the countif to look for the text string, not the value?

Thanks,
Chuck


Not as fast as COUNTIF, but it should work:

For Each c In Range("a:a")
If c.Text = "010020" Then


--ron


Or, as was mentioned in another thread by a different Ron (Coderre)

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020*") 0 Then


--ron


And see Ron Coderre's critique of this method as it can match 010020a.
--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Force VBA countif to find string, not value

Thanks, but I have multiple sheets of about 60k records each, so need
a fast method, and the wildcard won't work because I have similar
prefixed items.

On Sep 2, 9:19 pm, Ron Rosenfeld wrote:
On Sun, 02 Sep 2007 21:46:19 -0400, Ron Rosenfeld
wrote:


Not as fast as COUNTIF, but it should work:


For Each c In Range("a:a")
If c.Text = "010020" Then


Or, as was mentioned in another thread by a different Ron (Coderre)


If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020*") 0 Then


And see Ron Coderre's critique of this method as it can match 010020a.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Force VBA countif to find string, not value

On Sun, 02 Sep 2007 20:20:39 -0700, c mateland wrote:

Thanks, but I have multiple sheets of about 60k records each, so need
a fast method, and the wildcard won't work because I have similar
prefixed items.

On Sep 2, 9:19 pm, Ron Rosenfeld wrote:
On Sun, 02 Sep 2007 21:46:19 -0400, Ron Rosenfeld
wrote:


Not as fast as COUNTIF, but it should work:


For Each c In Range("a:a")
If c.Text = "010020" Then


Or, as was mentioned in another thread by a different Ron (Coderre)


If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020*") 0 Then


And see Ron Coderre's critique of this method as it can match 010020a.



OK, here is a VBA routine that I believe will work for what you want:

Evaluate("sumproduct(--exact(a1:a60000,""010020""))")

It matches 010020 but does not match 10020.

So, in your context:

If Evaluate("sumproduct(--exact(a1:a60000,""010020""))") 0 Then
--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Force VBA countif to find string, not value

If you really want VBA, you could use something like:

If ActiveSheet.Evaluate("SumProduct(--(""a1:a100""=""010020""))") 0 then

=sumproduct() can't use the entire column until xl2007.



c mateland wrote:

Excel 2003

In a VBA routine, I'm trying to do a simple countif.

If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then

This evaluates to TRUE when I expect FALSE.

In the column it's evaluating, I have...

A
10020
10021
10022

It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.

How do I force the countif to look for the text string, not the value?

Thanks,
Chuck


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Force VBA countif to find string, not value

That works for that lookup value, which is text. But it won't work
when it's a value.

In my list (60k), some are text and some are values. (It's how it came
in from another program.) What looks like numbers, Excel set as
numbers and what looked like text became text. So, now when I run a
routine matching a string from one array to another, it fails because
sometimes it can be a value and other times it's text. I can't figure
out how to write the syntax to work in both cases.

Specifically, how do you handle a countif or match when the variable
criteria might be looking up either value or text. I add quotes for
text, it fails on numbers. I remove quotes and if fails on text.

How do you deal with this? Is there some way to set my variable as a
certain object type to do this?



On Sep 2, 9:41 pm, Dave Peterson wrote:
If you really want VBA, you could use something like:

If ActiveSheet.Evaluate("SumProduct(--(""a1:a100""=""010020""))") 0 then

=sumproduct() can't use the entire column until xl2007.





c mateland wrote:

Excel 2003


In a VBA routine, I'm trying to do a simple countif.


If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then


This evaluates to TRUE when I expect FALSE.


In the column it's evaluating, I have...


A
10020
10021
10022


It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.


How do I force the countif to look for the text string, not the value?


Thanks,
Chuck


--

Dave Peterson- Hide quoted text -

- Show quoted text -



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Force VBA countif to find string, not value

First, I had a typo in my expression.

It should have been:
If ActiveSheet.Evaluate("SumProduct(--(a1:a100=""010020""))") 0 then

But couldn't you check twice?

Dim myVal As Variant

myVal = "123" '123"

If ActiveSheet.Evaluate("SumProduct(--(a1:a100=""" & myVal & """))") 0 _
or ActiveSheet.Evaluate("SumProduct(--(a1:a100=" & myVal & "))") 0 Then



c mateland wrote:

That works for that lookup value, which is text. But it won't work
when it's a value.

In my list (60k), some are text and some are values. (It's how it came
in from another program.) What looks like numbers, Excel set as
numbers and what looked like text became text. So, now when I run a
routine matching a string from one array to another, it fails because
sometimes it can be a value and other times it's text. I can't figure
out how to write the syntax to work in both cases.

Specifically, how do you handle a countif or match when the variable
criteria might be looking up either value or text. I add quotes for
text, it fails on numbers. I remove quotes and if fails on text.

How do you deal with this? Is there some way to set my variable as a
certain object type to do this?

On Sep 2, 9:41 pm, Dave Peterson wrote:
If you really want VBA, you could use something like:

If ActiveSheet.Evaluate("SumProduct(--(""a1:a100""=""010020""))") 0 then

=sumproduct() can't use the entire column until xl2007.





c mateland wrote:

Excel 2003


In a VBA routine, I'm trying to do a simple countif.


If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then


This evaluates to TRUE when I expect FALSE.


In the column it's evaluating, I have...


A
10020
10021
10022


It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.


How do I force the countif to look for the text string, not the value?


Thanks,
Chuck


--

Dave Peterson- Hide quoted text -

- Show quoted text -


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Force VBA countif to find string, not value

This seems to be about searching for text and functions interpreting
them into numbers.

To elaborate more specifically, somehow I need the routine to check if
the item from the first array is present in the second array, and if
so, which row is it on? Both arrays are text, but some can look like
values (i.e., 150, 0150) and others look only like text (i.e., T150,
150A).

Here's a snippet of what I'm doing now...

'cItemArray1 = lookup value from first array (looping all such items)

If Application.WorksheetFunction _
.CountIf(Range("a:a"), cItemArray1) = 0 Then 'not found

'code to record cItemArray1 as not found (exception)

Else 'countif found the item in array2...

'note row number of match in Array2
iMatchRow = Application.WorksheetFunction _
.Match(cItemArray1, Range("a:a"), 0)

When cItemArray1 = "010020" the countif finds 10020 in Array2, which
is incorrect. It then passes to the match function, which cannot find
010020 because it searches for the literal string, whereas the countif
interprets. The match function then throws an error, because the
countif showed the lookup as existing when it really didn't.

(I use the countif before the match because I can't figure out a way
to do an error handler for a match for a non-existant lookup - any
suggestions?)

I tried your code, but I get a type mismatch error when checking
twice. That's when a text string, which cannot be a number, is my
lookup. I'm sure it's choking on the syntax that removes the quotes.

As far as first converting the arrays to values, I added a routine
that first converts both arrays to text, so there's not a mix of
values and text. However, I can't convert them to numbers because
00569 will become 569, which will hose everything. My items, for
example, include 00569, 0569, 569, 00569T, and 569T.

Thanks for any advice.



On Sep 3, 7:47 am, Dave Peterson wrote:
First, I had a typo in my expression.

It should have been:
If ActiveSheet.Evaluate("SumProduct(--(a1:a100=""010020""))") 0 then

But couldn't you check twice?

Dim myVal As Variant

myVal = "123" '123"

If ActiveSheet.Evaluate("SumProduct(--(a1:a100=""" & myVal & """))") 0 _
or ActiveSheet.Evaluate("SumProduct(--(a1:a100=" & myVal & "))") 0 Then





c mateland wrote:

That works for that lookup value, which is text. But it won't work
when it's a value.


In my list (60k), some are text and some are values. (It's how it came
in from another program.) What looks like numbers, Excel set as
numbers and what looked like text became text. So, now when I run a
routine matching a string from one array to another, it fails because
sometimes it can be a value and other times it's text. I can't figure
out how to write the syntax to work in both cases.


Specifically, how do you handle a countif or match when the variable
criteria might be looking up either value or text. I add quotes for
text, it fails on numbers. I remove quotes and if fails on text.


How do you deal with this? Is there some way to set my variable as a
certain object type to do this?


On Sep 2, 9:41 pm, Dave Peterson wrote:
If you really want VBA, you could use something like:


If ActiveSheet.Evaluate("SumProduct(--(""a1:a100""=""010020""))") 0 then


=sumproduct() can't use the entire column until xl2007.


c mateland wrote:


Excel 2003


In a VBA routine, I'm trying to do a simple countif.


If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then


This evaluates to TRUE when I expect FALSE.


In the column it's evaluating, I have...


A
10020
10021
10022


It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.


How do I force the countif to look for the text string, not the value?


Thanks,
Chuck


--


Dave Peterson- Hide quoted text -


- Show quoted text -


--

Dave Peterson- Hide quoted text -

- Show quoted text -





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Force VBA countif to find string, not value

A simple error trap seems to be working now with a match that cannot
resolve. Since the match is more reliable than the countif, I'll test
using the match with error trapping instead.



On Sep 3, 1:58 pm, c mateland wrote:
(I use the countif before the match because I can't figure out a way
to do an error handler for a match for a non-existant lookup - any
suggestions?)



  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Force VBA countif to find string, not value

Can you convert the text looking cells to numbers first?

copy an empty cell
select the range to change from text numbers to number numbers
edit|paste special|check add

Then you'd always be looking for numbers.

(and you could do that paste special stuff in your code, too.)

c mateland wrote:

That works for that lookup value, which is text. But it won't work
when it's a value.

In my list (60k), some are text and some are values. (It's how it came
in from another program.) What looks like numbers, Excel set as
numbers and what looked like text became text. So, now when I run a
routine matching a string from one array to another, it fails because
sometimes it can be a value and other times it's text. I can't figure
out how to write the syntax to work in both cases.

Specifically, how do you handle a countif or match when the variable
criteria might be looking up either value or text. I add quotes for
text, it fails on numbers. I remove quotes and if fails on text.

How do you deal with this? Is there some way to set my variable as a
certain object type to do this?

On Sep 2, 9:41 pm, Dave Peterson wrote:
If you really want VBA, you could use something like:

If ActiveSheet.Evaluate("SumProduct(--(""a1:a100""=""010020""))") 0 then

=sumproduct() can't use the entire column until xl2007.





c mateland wrote:

Excel 2003


In a VBA routine, I'm trying to do a simple countif.


If Application.WorksheetFunction _
.CountIf(Range("a:a"),"010020") 0 Then


This evaluates to TRUE when I expect FALSE.


In the column it's evaluating, I have...


A
10020
10021
10022


It's obviously counting the 10020 entry, but I want an exact match to
the literal criteria I enter.


How do I force the countif to look for the text string, not the value?


Thanks,
Chuck


--

Dave Peterson- Hide quoted text -

- Show quoted text -


--

Dave Peterson
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 String in another string - only between spaces Nir Excel Worksheet Functions 9 November 2nd 06 11:31 AM
Find Many String options in ONE String Nir Excel Worksheet Functions 6 October 26th 06 07:13 AM
Concatenate & Force argument to End of String-(spsjp) SP Excel Worksheet Functions 3 April 2nd 05 03:37 PM
backwards find function to find character in a string of text Ashleigh K. Excel Programming 1 January 14th 04 04:36 PM
find a string inside another string MarkS Excel Programming 1 January 13th 04 02:55 AM


All times are GMT +1. The time now is 07:39 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"