PDA

View Full Version : Solved: Find Position and Count instances of a character in a string



jaminben
02-24-2012, 04:27 PM
Hello,

I've been trying to work out what the best way would be to convert a string which contains numbers seperated by coma's into individual numbers.

So my userform textbox contains something like "2,5,8,11" and I need to get the first number (2) hold this number and wait until Cell.Row = 2 then do something, get the next number (5) hold this number and wait until Cell.Row = 5 then do something etc. The userform textbox changes all the time from job to job.

I've been messing around with stuff like:

myString = "2,5,8,11"

Occurences = Len(myString) - Len(Replace(myString, ",", ""))

Which gives me the number of coma's

Position = InStr(myString, ",")

Which gives me the position of the first coma.

And finally using Mid(myString, start_position, number_of_characters)

But I don't seem to be able to combine everything :think:

Its late and I can't think of a way to do it.... anyone got any suggestions?

Thanks

Paul_Hossler
02-24-2012, 06:27 PM
Dim v as variant

v = Split (MyString, ",")

Msgbox v(0)
Msgbox v(1)

etc.



Paul

jaminben
02-25-2012, 02:38 AM
Thanks Paul,

A nice and simple answer, thats what I like.

:thumb