How do I parse a string...

Posted on 2007-08-01
Last Modified: 2010-04-30
I have a variable that contains a "-" hyphen that separates two pieces of information.  How do I parse for it in Visual Basic:

Dim myFullVar as String
myFullVar = "Z23XYU53389-383838"
myVar1 =  'Your code will go here and return Z23XYU53389
myVar2 = 'Your code will go here and return 383838
Question by:frankmorrison
    LVL 76

    Accepted Solution

    MyVar1 = Split(myFullVar, "-")(0)
    MyVar2 = Split(myFullVar, "-")(1)
    LVL 18

    Expert Comment

    You can use Instr to find the position of the "-" in the string and then use the Mid function to extract the first and the last string. Or you can also use the Split function. I am displaying the Split function here.

    Dim strSplit() as String
    myVar1 =strSplit(0)
    myVar2 =strSplit(1)

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now