+ Reply to Thread
Results 1 to 7 of 7

Count spaces in front of text

  1. #1
    Registered User
    Join Date
    07-07-2004
    Posts
    35

    Count spaces in front of text

    Hi

    Is it possible to count the number of spaces that appear before a text in vba?

    E.g. " Jean" = 3

    I really would appreciate some help on this.

    Thanks and Kind Regards

  2. #2
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Like this:

    For i = 1 To Len(ActiveCell.Value)
    If Mid(ActiveCell.Value, i, 1) <> " " Then
    MsgBox CStr(i - 1) & " spaces"
    Exit For
    End If
    Next


    Col

  3. #3
    Norman Jones
    Guest

    Re: Count spaces in front of text

    Hi Poppy,

    Try:

    '=============>>
    Public Sub Tester()
    Const sStr As String = " Jean"
    Dim iSpaces As Long

    iSpaces = InStr(1, sStr, "J") - 1
    MsgBox iSpaces
    End Sub
    '<<=============


    ---
    Regards,
    Norman


    "poppy" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi
    >
    > Is it possible to count the number of spaces that appear before a text
    > in vba?
    >
    > E.g. " Jean" = 3
    >
    > I really would appreciate some help on this.
    >
    > Thanks and Kind Regards
    >
    >
    > --
    > poppy
    > ------------------------------------------------------------------------
    > poppy's Profile:
    > http://www.excelforum.com/member.php...o&userid=11453
    > View this thread: http://www.excelforum.com/showthread...hreadid=558439
    >




  4. #4
    Registered User
    Join Date
    07-07-2004
    Posts
    35

    Thumbs up

    *Poppy on knees* Thank you!!!! thank you! thank you! thank you! thank you! Col

    You're a life saver, I've been trying all day to figure this out. Thank you so much.

    Kind Regards

  5. #5
    Dave Peterson
    Guest

    Re: Count spaces in front of text

    And maybe a little more generic:

    Option Explicit

    Public Sub Tester()
    Const sStr As String = " Jean"
    Dim iSpaces As Long

    iSpaces = InStr(1, sStr, Left(Trim(sStr), 1), vbTextCompare) - 1
    MsgBox iSpaces
    End Sub

    Norman Jones wrote:
    >
    > Hi Poppy,
    >
    > Try:
    >
    > '=============>>
    > Public Sub Tester()
    > Const sStr As String = " Jean"
    > Dim iSpaces As Long
    >
    > iSpaces = InStr(1, sStr, "J") - 1
    > MsgBox iSpaces
    > End Sub
    > '<<=============
    >
    > ---
    > Regards,
    > Norman
    >
    > "poppy" <[email protected]> wrote in
    > message news:[email protected]...
    > >
    > > Hi
    > >
    > > Is it possible to count the number of spaces that appear before a text
    > > in vba?
    > >
    > > E.g. " Jean" = 3
    > >
    > > I really would appreciate some help on this.
    > >
    > > Thanks and Kind Regards
    > >
    > >
    > > --
    > > poppy
    > > ------------------------------------------------------------------------
    > > poppy's Profile:
    > > http://www.excelforum.com/member.php...o&userid=11453
    > > View this thread: http://www.excelforum.com/showthread...hreadid=558439
    > >


    --

    Dave Peterson

  6. #6
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Got back to this one reviewing my watched threads...
    If brevity is the watchword:

    MyCountOfSpaces=len(TheSTring)-len(ltrim(thestring))

    Any better/more efficient methods?

    C

  7. #7
    Registered User
    Join Date
    07-07-2004
    Posts
    35

    Thumbs up

    Hi Guys

    Thanks once again for all the help. Really appreciate it.

    Kind Regards

+ 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