+ Reply to Thread
Results 1 to 6 of 6

How to count the number of 'spaces' in a cell.

  1. #1
    Registered User
    Join Date
    02-29-2012
    Location
    Gloucester, England
    MS-Off Ver
    Excel, Word, Access 2007 and XP
    Posts
    58

    How to count the number of 'spaces' in a cell.

    Using Excel 2007 VBA I'm trying to find the number of spaces in a text string so I can assign the total to a variable.

    I could probably create a loop for the length of the string then test each character for a space and then iterate the variable.

    However, is there a simple built-in function that does the same?
    Dave

    I'm no expert I just like solving problems

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,452

    Re: How to count the number of 'spaces' in a cell.

    Perhaps
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    02-29-2012
    Location
    Gloucester, England
    MS-Off Ver
    Excel, Word, Access 2007 and XP
    Posts
    58

    Re: How to count the number of 'spaces' in a cell.

    Hi Pepe,

    that certainly works on the worksheet but I get zero value if used in VBA:

    namelen = Len(a1) - Len(WorksheetFunction.Substitute(a1, " ", ""))

    Strangly, if I just test on MsgBox(Len(a1)) it displays 0, so it appears that I'm doing something wrong but I just can't see it!

    Yes, I do have some text in A1 before you ask

    ---------- Post added at 11:44 AM ---------- Previous post was at 11:40 AM ----------

    It works if I do this though

    namelen = Len(ActiveCell.Value) - Len(WorksheetFunction.Substitute(ActiveCell.Value, " ", ""))

    seems a bit over the top!

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: How to count the number of 'spaces' in a cell.

    Try this

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    08-19-2012
    Location
    Cambodia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How to count the number of 'spaces' in a cell.

    The number of spaces in a cell can be counted simply by using the Split method:
    Dim x() As String
    x = Split(Cells(1, 1), " ") 'count number of spaces in cell A1
    MsgBox UBound(x)

    You can find a lot of excel vba example code at: http://www.worldbestlearningcenter.c...ds-example.htm

  6. #6
    Registered User
    Join Date
    02-29-2012
    Location
    Gloucester, England
    MS-Off Ver
    Excel, Word, Access 2007 and XP
    Posts
    58

    Re: How to count the number of 'spaces' in a cell.

    Quote Originally Posted by darayuk View Post
    The number of spaces in a cell can be counted simply by using the Split method:
    Dim x() As String
    x = Split(Cells(1, 1), " ") 'count number of spaces in cell A1
    MsgBox UBound(x)

    You can find a lot of excel vba example code at: http://www.worldbestlearningcenter.c...ds-example.htm
    Sorted, thanks to you all for your guidance, thanks to darayuk for the best answer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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