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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explode and Count

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I want to create a macro o take a string, explode it, and count the commas.
Eg:
User enters: "name1,name2,name3"
Two commas present, 3 objects must have been entered.
Sendkeys: 3 object(s) - name1, name2, name3

Is this possible?
 
Yep, what language are you writing in?

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
If you're dealing with a static amount (3 in your example) of items in a comma delimited string, something along the following lines should work. Or do you need something dynamic?
Code:
Sub Main

    Dim sArray(2) as string

    sTest = "John,Q,Public"
    isArrayCnt = 0
    
    While instr(sTest,",")
        sArray(isArrayCnt) = Left(sTest,instr(sTest,",")-1)
        sTest = right(sTest,len(sTest) - instr(sTest,","))
        isArrayCnt = isArrayCnt+1
    Wend
    sArray(isArrayCnt) = sTest
    
    For x = 0 to 2 
        msgbox sArray(x)
    Next
    
End Sub

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
This will count the number of commas in a string.

MyString = "1,2,3,4"

StartPosition = 1
MyCount = 0
Do While Instr(StartPosition, MyString, ",") > 0
StartPosition = Instr(StartPosition, MyString, ",")+1
MyCount = MyCount + 1
Loop


If you use VB(A), you can use the Split function that will put the data into an array. Then using the Ubound function you can determine how many elements there are.

calculus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top