Stepping through each character in a character string

G

Guest

I'd like to step through the characters in a string and parse out some data.
I want to do something like this

For each Character in myString

Next Character

I get an error that I can only iterate over a collection object or array.
What do I need to do so that I can do this?

Thanks,
Barb Reinhardt
 
J

JE McGimpsey

A string isn't a collection, so you can't use For Each...Next


One way:

Dim i As Long
For i = 1 To Len(myString)
Debug.Print Mid(myString, i, 1)
Next i
 
C

CoRrRan

Sub StepThroughString()

Dim str As String, myString As String

myString = "Test string"

Dim i As Integer

For i = 1 To Len(myString)
str = Mid(myString, i, 1)
Next i

End Sub

HTH, CoRrRan
 
R

Rick Rothstein \(MVP - VB\)

I'd like to step through the characters in a string and parse out
some data. I want to do something like this

For each Character in myString

Next Character

I get an error that I can only iterate over a collection object or array.
What do I need to do so that I can do this?

Depending on what you mean by "parse out some data", there MAY be other ways
to do what you want instead of doing a character by character search. Can
you give us an example of a typical string you might want to parse and what
in it you want to remove?

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top