XL2000: How to Use the Left, Right, Mid, and Len Functions in Visual Basic

ID: Q213646


The information in this article applies to:
  • Microsoft Excel 2000


SUMMARY

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.


MORE INFORMATION

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 a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
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/overview/overview.asp
To following procedure demonstrates the use of the Left, Right, Mid, and Len functions of Microsoft Visual Basic for Applications in Microsoft Excel:
  1. Create a new, blank Microsoft Excel workbook.


  2. Open the Visual Basic Editor by pressing ALT+F11.


  3. On the Insert menu, click Module.


  4. Type the following macro in the new module sheet:



      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 
  • To see an example of the Len function, point to Macro on the Tools menu, click Macros, select the String_Len macro, and then click Run.


  • To see an example of the Left function, point to Macro on the Tools menu, click Macros, select the String_Left macro, and then click Run.


  • To see an example of the Right function, point to Macro on the Tools menu, click Macros, select the String_Right macro, and then click Run.


  • To see an example of the Mid function, point to Macro on the Tools menu, click Macros, select the String_Mid macro, and then click Run.



REFERENCES

For more information about these functions, type the following on a module sheet:

Len
Right
Left
Mid
Highlight the function that you want more information about, and then press F1.

Additional query words:

Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: December 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.