Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Check a range to see if a value exists in it

What stupid little thing am I missing with this 'simple' snippet of test code.

Whe
I have the letter A in Range("D1").

Range("G1:G10")is a range named lData and contains A,B,A,D,A,B,C,A,blank,blank.
(The real range is about 100 rows in a column with perhaps 25 entries followed by 75 blanks, the entry amounts will vary widely but no blanks within the entries, only after the last entry)

When I run the my test code "MsgBox lCnt" returns 0 (zero). Should return 4.

Option Explicit
Sub WFCData()
Dim i As String
Dim lCnt As Integer
Dim lData As Range
i = Range("D1").Value
lCnt = Application.WorksheetFunction.Count("lData", i)
MsgBox lCnt
End Sub

Thanks.
Regards,
Howard
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Check a range to see if a value exists in it

On Monday, September 24, 2012 9:14:06 AM UTC+3, Howard wrote:
What stupid little thing am I missing with this 'simple' snippet of test code.



Whe

I have the letter A in Range("D1").



Range("G1:G10")is a range named lData and contains A,B,A,D,A,B,C,A,blank,blank.

(The real range is about 100 rows in a column with perhaps 25 entries followed by 75 blanks, the entry amounts will vary widely but no blanks within the entries, only after the last entry)



When I run the my test code "MsgBox lCnt" returns 0 (zero). Should return 4.



Option Explicit

Sub WFCData()

Dim i As String

Dim lCnt As Integer

Dim lData As Range

i = Range("D1").Value

lCnt = Application.WorksheetFunction.Count("lData", i)

MsgBox lCnt

End Sub



Thanks.

Regards,

Howard


Count worksheet function only counts numbers, try this instead:
Application.WorksheetFunction.CountA("lData")
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Check a range to see if a value exists in it

Hi Howard,

Am Sun, 23 Sep 2012 23:14:06 -0700 (PDT) schrieb Howard:

When I run the my test code "MsgBox lCnt" returns 0 (zero). Should return 4.

Option Explicit
Sub WFCData()
Dim i As String
Dim lCnt As Integer
Dim lData As Range
i = Range("D1").Value
lCnt = Application.WorksheetFunction.Count("lData", i)
MsgBox lCnt
End Sub


change to:
lCnt = Application.WorksheetFunction.CountIf(Range("lData "), i)


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Check a range to see if a value exists in it

On Sunday, September 23, 2012 11:14:06 PM UTC-7, Howard wrote:
What stupid little thing am I missing with this 'simple' snippet of test code.



Whe

I have the letter A in Range("D1").



Range("G1:G10")is a range named lData and contains A,B,A,D,A,B,C,A,blank,blank.

(The real range is about 100 rows in a column with perhaps 25 entries followed by 75 blanks, the entry amounts will vary widely but no blanks within the entries, only after the last entry)



When I run the my test code "MsgBox lCnt" returns 0 (zero). Should return 4.



Option Explicit

Sub WFCData()

Dim i As String

Dim lCnt As Integer

Dim lData As Range

i = Range("D1").Value

lCnt = Application.WorksheetFunction.Count("lData", i)

MsgBox lCnt

End Sub



Thanks.

Regards,

Howard

Hi Claus,

That worked just perfect!!
Thanks a lot.

Regards,
Howard
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Check a range to see if a value exists in it

On Sunday, September 23, 2012 11:14:06 PM UTC-7, Howard wrote:
What stupid little thing am I missing with this 'simple' snippet of test code.



Whe

I have the letter A in Range("D1").



Range("G1:G10")is a range named lData and contains A,B,A,D,A,B,C,A,blank,blank.

(The real range is about 100 rows in a column with perhaps 25 entries followed by 75 blanks, the entry amounts will vary widely but no blanks within the entries, only after the last entry)



When I run the my test code "MsgBox lCnt" returns 0 (zero). Should return 4.



Option Explicit

Sub WFCData()

Dim i As String

Dim lCnt As Integer

Dim lData As Range

i = Range("D1").Value

lCnt = Application.WorksheetFunction.Count("lData", i)

MsgBox lCnt

End Sub



Thanks.

Regards,

Howard


Hi Claus,

That worked perfectly!!
Thanks a lot.

Regards,Howard


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Check a range to see if a value exists in it

Hi Howard,

I think there are two small changes. First, be sure to set "lData" = your data range. If it is a named range use:

Set lData = Sheet1.Range("ldata")

If it is not named, use:

Set lData = Sheet1.Range("G1:G10")

Then, change your function in the lCnt line to CountIf and drop the quotes:

lCnt = Application.WorksheetFunction.CountIf(lData, i)

Hope this helps

Ben
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
Check if Range EXISTS KIM W Excel Programming 3 May 24th 09 09:34 PM
Check to see if a range name exists on a sheet Sajit Excel Programming 1 October 12th 07 06:32 PM
Check if a named range exists with VBA? [email protected] Excel Programming 3 January 31st 07 06:58 PM
Check if named range exists! steve_doc Excel Programming 2 April 29th 06 11:56 PM
Check if a number exists in a range? gkaste Excel Discussion (Misc queries) 2 July 13th 05 08:00 PM


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