Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Count Space, comma and fullstop in a string

Hello,

How can i count Count Space, comma and fullstop in a string through macro.

For Ex. if A1= My name is Rahul. Then B1=4
For Ex. if A1= My name is Rahul, my brother's name is NewName & our surname
is Gupta. Then B1 =14

and so on.

Thanks in advance,

Rahul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 138
Default Count Space, comma and fullstop in a string

Rahul,

Use the following function ...

Function CountCharacters(ByVal Str As String) As Long

Dim i As Integer

For i = 1 To Len(Str)
If Mid(Str, i, 1) = " " Or Mid(Str, i, 1) = "," Or Mid(Str, i, 1) =
"." Then
CountCharacters = CountCharacters + 1
End If
Next i

End Function

Rgds,

Alan

Rahul Gupta wrote:
Hello,

How can i count Count Space, comma and fullstop in a string through macro.

For Ex. if A1= My name is Rahul. Then B1=4
For Ex. if A1= My name is Rahul, my brother's name is NewName & our surname
is Gupta. Then B1 =14

and so on.

Thanks in advance,

Rahul


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Count Space, comma and fullstop in a string

In the worksheet use a combination of LEN() and SUBSTITUTE():

=LEN(A1)-LEN(SUBSTITUTE(A1," ","")) will count the number of spaces
=LEN(A1)-LEN(SUBSTITUTE(A1,",","")) will count the number of commas, etc
--
Gary's Student


"Rahul Gupta" wrote:

Hello,

How can i count Count Space, comma and fullstop in a string through macro.

For Ex. if A1= My name is Rahul. Then B1=4
For Ex. if A1= My name is Rahul, my brother's name is NewName & our surname
is Gupta. Then B1 =14

and so on.

Thanks in advance,

Rahul

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Count Space, comma and fullstop in a string

Hi Rahul,

Try something like:

'=============
Public Sub Tester()
Dim arr As Variant
Dim sStr As String
Dim i As Long
Dim iCtr As Long
Const sStr2 As String = " My name is Rahul, my " _
& "brother's name is NewName & our surname "

sStr = sStr2
arr = Array(" ", ",", ".")

For i = LBound(arr) To UBound(arr)
sStr = Replace(sStr, arr(i), vbNullString)
Next i

iCtr = Len(sStr2) - Len(sStr)

MsgBox iCtr

End Sub
'<<=============


---
Regards,
Norman


"Rahul Gupta" wrote in message
...
Hello,

How can i count Count Space, comma and fullstop in a string through macro.

For Ex. if A1= My name is Rahul. Then B1=4
For Ex. if A1= My name is Rahul, my brother's name is NewName & our
surname
is Gupta. Then B1 =14

and so on.

Thanks in advance,

Rahul



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Count Space, comma and fullstop in a string

Hello Again Gary,

SUBSTITUTE is not working in VBA? It is a worksheet function, as u have also
mentioned. Any other way?

This method wont help.

Regards,
Rahul.

"Gary''s Student" wrote:

In the worksheet use a combination of LEN() and SUBSTITUTE():

=LEN(A1)-LEN(SUBSTITUTE(A1," ","")) will count the number of spaces
=LEN(A1)-LEN(SUBSTITUTE(A1,",","")) will count the number of commas, etc
--
Gary's Student


"Rahul Gupta" wrote:

Hello,

How can i count Count Space, comma and fullstop in a string through macro.

For Ex. if A1= My name is Rahul. Then B1=4
For Ex. if A1= My name is Rahul, my brother's name is NewName & our surname
is Gupta. Then B1 =14

and so on.

Thanks in advance,

Rahul



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default Count Space, comma and fullstop in a string

But you need to test for errors; there may not be a "," like in the first example.
To put it all in one formula makes it very long and almost unreadable. Best is to use some intermediate cells, if you want a
formula approach rather than a VBA function.

--
Kind regards,

Niek Otten
Microsoft MVP - Excel

"Gary''s Student" wrote in message
...
| In the worksheet use a combination of LEN() and SUBSTITUTE():
|
| =LEN(A1)-LEN(SUBSTITUTE(A1," ","")) will count the number of spaces
| =LEN(A1)-LEN(SUBSTITUTE(A1,",","")) will count the number of commas, etc
| --
| Gary's Student
|
|
| "Rahul Gupta" wrote:
|
| Hello,
|
| How can i count Count Space, comma and fullstop in a string through macro.
|
| For Ex. if A1= My name is Rahul. Then B1=4
| For Ex. if A1= My name is Rahul, my brother's name is NewName & our surname
| is Gupta. Then B1 =14
|
| and so on.
|
| Thanks in advance,
|
| Rahul


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Count Space, comma and fullstop in a string

MsgBox Len(myString) - Len(Replace(Replace(Replace(myString, " ", ""),
".", ""), ",", ""))


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Rahul Gupta" wrote in message
...
Hello Again Gary,

SUBSTITUTE is not working in VBA? It is a worksheet function, as u have

also
mentioned. Any other way?

This method wont help.

Regards,
Rahul.

"Gary''s Student" wrote:

In the worksheet use a combination of LEN() and SUBSTITUTE():

=LEN(A1)-LEN(SUBSTITUTE(A1," ","")) will count the number of spaces
=LEN(A1)-LEN(SUBSTITUTE(A1,",","")) will count the number of commas, etc
--
Gary's Student


"Rahul Gupta" wrote:

Hello,

How can i count Count Space, comma and fullstop in a string through

macro.

For Ex. if A1= My name is Rahul. Then B1=4
For Ex. if A1= My name is Rahul, my brother's name is NewName & our

surname
is Gupta. Then B1 =14

and so on.

Thanks in advance,

Rahul



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
Extract name before comma, space & number SherryScrapDog Excel Discussion (Misc queries) 3 January 26th 09 02:46 AM
add extra space before comma clarknv Excel Worksheet Functions 6 March 19th 07 03:52 PM
Inserting a space after a comma Visual Calendar Dilemma Excel Worksheet Functions 2 September 11th 06 11:20 PM
Removing a space after a comma DebbieK9 New Users to Excel 3 April 1st 05 10:08 PM
HELP - I need to change space delimited to comma? Mayer Excel Discussion (Misc queries) 1 December 18th 04 06:21 PM


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

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"