ID: Q141287
This article contains examples of how to manipulate text strings using the Left, Right, Mid, and Len functions in Microsoft Visual Basic for Applications in Microsoft Excel.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
   http://www.microsoft.com/support/supportnet/refguide/
1. Create a new, blank Microsoft Excel workbook.
2. To insert a new Visual Basic module sheet, click the Insert menu, point
   to Macro, and then click Module.
   In Microsoft Excel 97 or Microsoft Excel 98, click the Tools menu, point
   to Macro, and then click Visual Basic Editor. On the Insert menu, click
   Module.
      Sub String_Len()
          ' Sets MyString.
          MyString = InputBox("Enter some text.")
          ' Displays length of string.
          MsgBox Prompt:="The length of the string is " & _
              Len(MyString) & " characters."
      End Sub
      Sub String_Left()
          ' Sets MyString.
          MyString = InputBox("Enter some text.")
          StringLen = Len(MyString)
          Pos = InputBox("Please enter a number from 1 to " & StringLen)
          ' Takes the left number of specified characters.
          Result = Left(MyString, Pos)
          ' Displays the result.
          MsgBox Prompt:="The left " & Pos & " characters of """ & _
              MyString & """ are: " & _
              Chr(13) & Result
      End Sub
      Sub String_Right()
          ' Sets MyString.
          MyString = InputBox("Enter some text.")
          StringLen = Len(MyString)
          Pos = InputBox("Please enter a number from 1 to " & StringLen)
          ' Takes the right number of specified digits.
          Result = Right(MyString, Pos)
          ' Displays the result.
          MsgBox Prompt:="The right " & Pos & " characters of """ & _
              MyString & """ are: " & _
              Chr(13) & Result
      End Sub
      Sub String_Mid()
          ' Sets MyString.
          MyString = InputBox("Enter some text.")
          ' Sets starting position.
          StartPos = InputBox _
              ("Give me a starting position (1 to " _
              & Len(MyString) & ")")
          ' Determines length of string of text.
          StringLen = Len(MyString) - StartPos + 1
          ' Sets number of characters.
          NumChars = InputBox _
              ("How many characters would you like? (From 1 to " & _
              StringLen & ")")
          MsgBox prompt:="The result is: " & _
              Mid(MyString, StartPos, NumChars)
      End Sub
In Microsoft Excel 97 and Microsoft Excel 98, click the Tools menu, point to Macro, and then click Macros. Click the name of the macro you want to run, and then click Run.
For more information about these functions, type the following on a module sheet
   Len
   Right
   Left
   Mid
Additional query words: 5.00 5.00c 7.00 8.00 XL97 XL98 XL7 XL5
Keywords          : kbprg kbdta kbdtacode PgmHowto KbVBA 
Version           : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowtoLast Reviewed: May 17, 1999