+ Reply to Thread
Results 1 to 11 of 11

Counting Strings in an Array

  1. #1
    ExcelMonkey
    Guest

    Counting Strings in an Array

    How do you count strings within an array?

    Thanks

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588
    Please Login or Register  to view this content.

  3. #3
    Bob Phillips
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > How do you count strings within an array?
    >
    > Thanks




  4. #4
    ExcelMonkey
    Guest

    Re: 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" <[email protected]> wrote

    in message
    >news:[email protected]...
    >> How do you count strings within an array?
    >>
    >> Thanks

    >
    >
    >.
    >


  5. #5
    Bob Phillips
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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" <[email protected]> wrote

    > in message
    > >news:[email protected]...
    > >> How do you count strings within an array?
    > >>
    > >> Thanks

    > >
    > >
    > >.
    > >




  6. #6
    Tom Ogilvy
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > How do you count strings within an array?
    >
    > Thanks




  7. #7
    AA2e72E
    Guest

    RE: 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
    >


  8. #8
    Myrna Larson
    Guest

    Re: 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"
    <[email protected]> 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
    >>



  9. #9
    Dana DeLouis
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > How do you count strings within an array?
    >
    > Thanks




  10. #10
    Daniel.M
    Guest

    Re: 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.



  11. #11
    Bob Phillips
    Guest

    Re: 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" <[email protected]> wrote in message
    news:%23RTfi%[email protected]...
    > 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1