VBA: Loop Through a String

January 11th, 2005 | Categories: Strings | Tags: ,
-->

You can perform logic on, or return individual characters from a string in VBA by looping through the string.

The following is an example of looping through a string using a For…Next Loop, and returning each character in a msgbox.

Sub LoopThroughString()

Dim Counter As Integer
Dim MyString As String

MyString = "AutomateExcel" 'define string

For Counter = 1 To Len(MyString)
    'do something to each character in string
    'here we'll msgbox each character
    MsgBox Mid(MyString, Counter, 1)
Next

End Sub
Can't get the tutorial to work for you? Need help with your code?
Get answers right away at our AE Excel Support Forums!
  1. Elena Sara
    May 10th, 2010 at 03:10
    Reply | Quote | #1

    is there any way to output each character in individual cells of MS EXCEL instead of in a msg box??