Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default VBA to count periods in a single cell

I am looking to count the number of periods within a single cell using vba.

For instance if I have 4 cells that contain the following:

5M09-0000700.01.10
5M09-0000700.01.10.10
5M09-0000700.01.10.10.10
5M09-0000700.01.10.10.20

Thus the output of the code for the first cell should be '2', the second
cell output would be '3', and the third/fourht cells would be '4' because
that many period characters were encountered. Data within these cells will
contain letters, numbers, periods, a few spaces and the "-" dash symbol. Any
help is greatly appreciated!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA to count periods in a single cell

Hi,

Try this

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

Mike

"Bubba" wrote:

I am looking to count the number of periods within a single cell using vba.

For instance if I have 4 cells that contain the following:

5M09-0000700.01.10
5M09-0000700.01.10.10
5M09-0000700.01.10.10.10
5M09-0000700.01.10.10.20

Thus the output of the code for the first cell should be '2', the second
cell output would be '3', and the third/fourht cells would be '4' because
that many period characters were encountered. Data within these cells will
contain letters, numbers, periods, a few spaces and the "-" dash symbol. Any
help is greatly appreciated!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default VBA to count periods in a single cell

One way

Option Explicit
Sub countperiodsincell()
Dim mc As Long
Dim i As Long
Dim j As Long
Dim ic As Double
mc = 9 ' column I
For i = 3 To 6 ' first row to last row in range
ic = 0
For j = 1 To Len(Cells(i, mc))
If Mid(Cells(i, mc), j, 1) = "." Then ic = ic + 1
Next j
MsgBox ic
Next i
End Sub


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Bubba" wrote in message
...
I am looking to count the number of periods within a single cell using vba.

For instance if I have 4 cells that contain the following:

5M09-0000700.01.10
5M09-0000700.01.10.10
5M09-0000700.01.10.10.10
5M09-0000700.01.10.10.20

Thus the output of the code for the first cell should be '2', the second
cell output would be '3', and the third/fourht cells would be '4' because
that many period characters were encountered. Data within these cells will
contain letters, numbers, periods, a few spaces and the "-" dash symbol.
Any
help is greatly appreciated!


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA to count periods in a single cell

Yes,

I missed that in the header of the post. Thanks.

Mike

"Don Guillett" wrote:

vba

Sub countperiodsincellFormula()
Dim mc As Long
Dim i As Long
mc = 9 ' column I
For i = 3 To 6
MsgBox Len(Cells(i, mc)) - _
Len(Application.Substitute(Cells(i, mc), ".", ""))
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Mike H" wrote in message
...
Hi,

Try this

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

Mike

"Bubba" wrote:

I am looking to count the number of periods within a single cell using
vba.

For instance if I have 4 cells that contain the following:

5M09-0000700.01.10
5M09-0000700.01.10.10
5M09-0000700.01.10.10.10
5M09-0000700.01.10.10.20

Thus the output of the code for the first cell should be '2', the second
cell output would be '3', and the third/fourht cells would be '4' because
that many period characters were encountered. Data within these cells
will
contain letters, numbers, periods, a few spaces and the "-" dash symbol.
Any
help is greatly appreciated!


.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default VBA to count periods in a single cell

Put this in a standard module.

Function CountPeriodsInCell(rw As Long) As Integer

CountPeriodsInCell = Len(Cells(rw, "I")) - _
Len(Application.Substitute(Cells(rw, "I"), ".", ""))

End Function

Then in any cell type "=CountPeriodsInCell(3)"
3- represents the row you want to calculate in Col. I

You will then see the number of periods in Range("I3")

Hope this helps! If so, let me know, click "YES" below.

--
Cheers,
Ryan


"Bubba" wrote:

I am looking to count the number of periods within a single cell using vba.

For instance if I have 4 cells that contain the following:

5M09-0000700.01.10
5M09-0000700.01.10.10
5M09-0000700.01.10.10.10
5M09-0000700.01.10.10.20

Thus the output of the code for the first cell should be '2', the second
cell output would be '3', and the third/fourht cells would be '4' because
that many period characters were encountered. Data within these cells will
contain letters, numbers, periods, a few spaces and the "-" dash symbol. Any
help is greatly appreciated!

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default VBA to count periods in a single cell

Give this a try...

Function CountDots(S As String) As Long
CountDots = UBound(Split(S, ".")) - (Len(S) = 0)
End Function

--
Rick (MVP - Excel)


"Bubba" wrote in message
...
I am looking to count the number of periods within a single cell using vba.

For instance if I have 4 cells that contain the following:

5M09-0000700.01.10
5M09-0000700.01.10.10
5M09-0000700.01.10.10.10
5M09-0000700.01.10.10.20

Thus the output of the code for the first cell should be '2', the second
cell output would be '3', and the third/fourht cells would be '4' because
that many period characters were encountered. Data within these cells will
contain letters, numbers, periods, a few spaces and the "-" dash symbol.
Any
help is greatly appreciated!


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
COUNT IF A VALUE FALLS WITHIN A RANGE IN A SINGLE CELL roland Excel Discussion (Misc queries) 2 August 5th 08 12:05 AM
How to count the number of months between 2 periods? Eric Excel Discussion (Misc queries) 3 February 5th 08 06:52 PM
count of tab characters in a single cell mark Excel Worksheet Functions 5 May 18th 06 06:55 PM
Count periods in a cell Carim[_3_] Excel Programming 3 January 24th 06 11:11 AM
COUNT SEPARATE PERIODS IN COLUMN Paul Bird Excel Programming 1 August 17th 03 05:49 PM


All times are GMT +1. The time now is 09:58 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"