ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Number of space in a string (https://www.excelbanter.com/excel-discussion-misc-queries/118032-number-space-string.html)

[email protected]

Number of space in a string
 
Hi,

I'm trying to find a way to count the number of space in a given
string. Is there any function that can do that?

Thanks for your help,


L. Howard Kittle

Number of space in a string
 
Hi Kathy,

Try this.

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))

If you want to know the number of WORDS in the string:

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1

HTH
Regards,
Howard

wrote in message
ps.com...
Hi,

I'm trying to find a way to count the number of space in a given
string. Is there any function that can do that?

Thanks for your help,




Dave Peterson

Number of space in a string
 
=(len(a1)-len(substitute(a1," ","")))/len(" ")

Since you're only counting a single character, you could eliminate the
denominator:

=len(a1)-len(substitute(a1," ",""))

wrote:

Hi,

I'm trying to find a way to count the number of space in a given
string. Is there any function that can do that?

Thanks for your help,


--

Dave Peterson

Biff

Number of space in a string
 
If you want to know the number of WORDS in the string:
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1


<space<spacetry<space<spacethis<space<space

Safer to trim first:

=LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1

Biff

"L. Howard Kittle" wrote in message
. ..
Hi Kathy,

Try this.

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))

If you want to know the number of WORDS in the string:

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1

HTH
Regards,
Howard

wrote in message
ps.com...
Hi,

I'm trying to find a way to count the number of space in a given
string. Is there any function that can do that?

Thanks for your help,






[email protected]

Number of space in a string
 
I forgot to say that I'm in VBA and not in Excel.
Is the Substitute function working in VBA too cause I got an error

Thanks


Dave Peterson

Number of space in a string
 
If you're using xl2k or higher, VBA has Replace. If you're using xl97 or below,
you can use application.substitute.

Option Explicit
Sub testme()
Dim SpaceCtr As Long
Dim myStr As String
myStr = "asdf qwer qwer"
'myStr = Worksheets("sheet1").Range("a1").Value
SpaceCtr = Len(myStr) - Len(Replace(myStr, " ", ""))
MsgBox SpaceCtr
'or
SpaceCtr = Len(myStr) - Len(Application.Substitute(myStr, " ", ""))
MsgBox SpaceCtr
End Sub



wrote:

I forgot to say that I'm in VBA and not in Excel.
Is the Substitute function working in VBA too cause I got an error

Thanks


--

Dave Peterson


All times are GMT +1. The time now is 06:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com