Friday, February 22, 2013

Read every character in a string using excel VBA


This example reads every character in a string from left to right and returns the result in a message box. It makes use of the Mid function.
Sub LoopThroughString()

Dim LookInHere As String
Dim Counter As Integer

'Use your own text here
LookInHere = "AutomateExcel.com"

For Counter = 1 To Len(LookInHere)
    MsgBox Mid(LookInHere, Counter, 1)
Next

End Sub

2 comments:

  1. Thats a cool code, but why dont you use LEFT or RIGHT function to do this? Im not an expert, but according to this: http://www.excel-aid.com/the-excel-left-and-right-functions-2.html, these functions would simplify the whole thing a little, since you only have to define how many characters should be read, instead of additionally defining a starting point like the MID function. Please tell me where im wrong here.

    ReplyDelete
  2. Thanks for sharing such great information. hope you keep sharing such kind of information
    Find and replace in excel

    ReplyDelete