Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Commas in a String

Status
Not open for further replies.

SQLJoe

Technical User
Dec 8, 2010
43
US
I have a string of alpha-numeric codes separated by commas and need to count how many commas are in the string to determine how many codes are listed (one more than the number of commas).

I know instr() will return the first instance of a character, but how can I count how many of them?

For example, given this string;

EI1184,OL9443,GG4657,DF7745

I need code that would count the commas and return the number 3 (I would then add 1 to get the number of codes listed = 4).
 

One way:
Code:
Dim str As String
Dim a() As String

str = "EI1184,OL9443,GG4657,DF7745"

a = Split(str, ",")

MsgBox UBound(a) + 1

Another way, you can loop thru your string, character by character, and count the commas.

Yet another way - if you look up in VBA Help InStr, you will find that you can set the starting point of where to start the look. You can use it in the loop to count the commas.

Have fun.

---- Andy
 
Another way:
myStr="EI1184,OL9443,GG4657,DF7745"
intHowManyCode=Len(myStr)-Len(Replace(myStr,",",""))+1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top