ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Stepping through each character in a character string (https://www.excelbanter.com/excel-programming/390801-stepping-through-each-character-character-string.html)

Barb Reinhardt

Stepping through each character in a character string
 
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

JE McGimpsey

Stepping through each character in a character string
 
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

In article ,
Barb Reinhardt wrote:

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


CoRrRan

Stepping through each character in a character string
 
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

Barb Reinhardt wrote:
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


Rick Rothstein \(MVP - VB\)

Stepping through each character in a character string
 
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



All times are GMT +1. The time now is 04:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com