Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Counting Strings in an Array

How do you count strings within an array?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Counting Strings in an Array

Do you mean count the number of entries that are strings? If so, I guess you
have to loop through checking the type.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote in message
...
How do you count strings within an array?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default Counting Strings in an Array

I simply have an array loaded with strings only. If I use
Ubound and there are empty elements in the array, will the
Ubound pick up the empties too. I do not want to count
the empties.


-----Original Message-----
Do you mean count the number of entries that are strings?

If so, I guess you
have to loop through checking the type.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote

in message
...
How do you count strings within an array?

Thanks



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Counting Strings in an Array

Yes it will, because Ubound is the upper bound, whatever it has been
dimensioned to. Actually, as LBound could also be any value, the correct
number is UBound((Ary)-LBound(ary)+1

--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote in message
...
I simply have an array loaded with strings only. If I use
Ubound and there are empty elements in the array, will the
Ubound pick up the empties too. I do not want to count
the empties.


-----Original Message-----
Do you mean count the number of entries that are strings?

If so, I guess you
have to loop through checking the type.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote

in message
...
How do you count strings within an array?

Thanks



.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Counting Strings in an Array

Define count strings. How many elements contain a string? If so, how do
you declare the array?

--
Regards,
Tom Ogilvy

"ExcelMonkey" wrote in message
...
How do you count strings within an array?

Thanks





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default Counting Strings in an Array

Given an array, you want to count the numberof rows that are NOT empty:
correct?

Try:

Function xx(ByVal StringArray As Variant) As Integer
StringArray = Join(StringArray, ",")
While 0 < InStr(StringArray, ",,")
StringArray = Replace(StringArray, ",,", ",")
Wend
StringArray = Split(StringArray, ",")
xx = UBound(StringArray)
End Function

"ExcelMonkey" wrote:

How do you count strings within an array?

Thanks

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Counting Strings in an Array

Interesting approach. The "old fashioned" way is:

Function NonEmpty(StringArray() AS String) AS Long
Dim i As Long
Dim N AS Long
For i = LBound(StringArray) to UBound(StringArray)
If Len(StringArray(i)) Then N = N + 1
Next i
NonEmpty = N
End Function

For a 35-element array with 15 empty and 20 filled, the above runs in
approximately 2/3 of the time (87 vs 150 microsec). String operations like
join, replace, and split are expensive time-wise. Getting the string length
amounts to a simple look-up.


On Tue, 15 Mar 2005 05:17:04 -0800, "AA2e72E"
wrote:

Given an array, you want to count the numberof rows that are NOT empty:
correct?

Try:

Function xx(ByVal StringArray As Variant) As Integer
StringArray = Join(StringArray, ",")
While 0 < InStr(StringArray, ",,")
StringArray = Replace(StringArray, ",,", ",")
Wend
StringArray = Split(StringArray, ",")
xx = UBound(StringArray)
End Function

"ExcelMonkey" wrote:

How do you count strings within an array?

Thanks


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default Counting Strings in an Array

I'm not sure, but would this idea work?

Sub Demo()
Dim v As Variant
Dim n As Long
Dim p As Long
Const k As String = "String"

v = Array(1, 2, "aa", "bb", 3, 4, WorksheetFunction.Pi())
For p = LBound(v) To UBound(v)
n = n + 1 - Abs(StrComp(TypeName(v(p)), k))
Next p
Debug.Print "Number of Strings: "; n
End Sub

- Number of Strings: 2

--
Dana DeLouis
Win XP & Office 2003


"ExcelMonkey" wrote in message
...
How do you count strings within an array?

Thanks



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Counting Strings in an Array

Hi,

I simply have an array loaded with strings only. If I use
Ubound and there are empty elements in the array, will the
Ubound pick up the empties too. I do not want to count
the empties.


n = Application.WorksheetFunction.CountA(YourArray)

Regards,

Daniel M.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Counting Strings in an Array

This surprises me, in a satisfying way ;-).

You have to take it away from the array size to count the strings, and it
fails if the array contains another array, or an object, but interesting.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Daniel.M" wrote in message
...
Hi,

I simply have an array loaded with strings only. If I use
Ubound and there are empty elements in the array, will the
Ubound pick up the empties too. I do not want to count
the empties.


n = Application.WorksheetFunction.CountA(YourArray)

Regards,

Daniel M.




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
Counting Text Strings Kholm Excel Worksheet Functions 11 August 21st 07 05:56 PM
Counting strings in excel with Pivot navin Excel Discussion (Misc queries) 4 January 3rd 07 02:17 PM
Array of Strings Zone Excel Discussion (Misc queries) 3 July 7th 06 04:52 PM
Counting text strings 525047 Excel Worksheet Functions 1 April 21st 06 05:35 AM
counting unique strings Sparky Mark Excel Discussion (Misc queries) 3 January 20th 05 11:47 PM


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

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"