+ Reply to Thread
Results 1 to 4 of 4

Parse String into Variables

  1. #1
    Gary''s Student
    Guest

    Parse String into Variables

    I need to parse a string like:

    732:1:29:18457:3

    Into an array of variables:
    DIM ntach(513) AS LONG

    So ntach(1) would contain 732
    ntach(2) would contain 1
    ntach(3) would contain 29
    ntach(4) would contain 18457 and
    ntach(5) would contain 3

    Each string can contain between 1 and 513 multi-digit numbers.
    Each number can have between 1 and 6 digits.

    If I was in a worksheet and if there were less than 256 numbers per string,
    I would use Text to Columns…

    What should I do in VBA?

    --
    Gary''s Student

  2. #2
    Registered User
    Join Date
    09-14-2005
    Posts
    6
    InStr is your best bet. Don't really have time to throw together how to do this in code, but suffice to say Instr(1,MyString,":") should get you started.

  3. #3
    Harald Staff
    Guest

    Re: Parse String into Variables

    Hi

    SPLIT does that. But you have to convert every datatype afterwards I
    believe:

    Sub test()
    Dim s As String
    Dim t() As String
    Dim ntach() As Long
    Dim i As Long

    s = "732:1:29:18457:3"
    t = Split(s, ":")
    ReDim ntach(LBound(t) To UBound(t))

    For i = LBound(t) To UBound(t)
    ntach(i) = Val(t(i))
    Next

    For i = LBound(ntach) To UBound(ntach)
    MsgBox ntach(i), , i
    Next

    End Sub

    HTH. Best wishes Harald

    "Gary''s Student" <[email protected]> skrev i melding
    news:[email protected]...
    > I need to parse a string like:
    >
    > 732:1:29:18457:3
    >
    > Into an array of variables:
    > DIM ntach(513) AS LONG
    >
    > So ntach(1) would contain 732
    > ntach(2) would contain 1
    > ntach(3) would contain 29
    > ntach(4) would contain 18457 and
    > ntach(5) would contain 3
    >
    > Each string can contain between 1 and 513 multi-digit numbers.
    > Each number can have between 1 and 6 digits.
    >
    > If I was in a worksheet and if there were less than 256 numbers per

    string,
    > I would use Text to Columns.
    >
    > What should I do in VBA?
    >
    > --
    > Gary''s Student




  4. #4
    Gary''s Student
    Guest

    Re: Parse String into Variables

    Thnk you very much.
    --
    Gary''s Student


    "Harald Staff" wrote:

    > Hi
    >
    > SPLIT does that. But you have to convert every datatype afterwards I
    > believe:
    >
    > Sub test()
    > Dim s As String
    > Dim t() As String
    > Dim ntach() As Long
    > Dim i As Long
    >
    > s = "732:1:29:18457:3"
    > t = Split(s, ":")
    > ReDim ntach(LBound(t) To UBound(t))
    >
    > For i = LBound(t) To UBound(t)
    > ntach(i) = Val(t(i))
    > Next
    >
    > For i = LBound(ntach) To UBound(ntach)
    > MsgBox ntach(i), , i
    > Next
    >
    > End Sub
    >
    > HTH. Best wishes Harald
    >
    > "Gary''s Student" <[email protected]> skrev i melding
    > news:[email protected]...
    > > I need to parse a string like:
    > >
    > > 732:1:29:18457:3
    > >
    > > Into an array of variables:
    > > DIM ntach(513) AS LONG
    > >
    > > So ntach(1) would contain 732
    > > ntach(2) would contain 1
    > > ntach(3) would contain 29
    > > ntach(4) would contain 18457 and
    > > ntach(5) would contain 3
    > >
    > > Each string can contain between 1 and 513 multi-digit numbers.
    > > Each number can have between 1 and 6 digits.
    > >
    > > If I was in a worksheet and if there were less than 256 numbers per

    > string,
    > > I would use Text to Columns.
    > >
    > > What should I do in VBA?
    > >
    > > --
    > > Gary''s Student

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1