Consulting

Results 1 to 4 of 4

Thread: Split a string

  1. #1
    VBAX Regular
    Joined
    Jun 2009
    Posts
    10
    Location

    Question Split a string

    I have a string like, let's say, "aabbccddeeffgghh" in one cell and I want to convert it in something like "aa" "bb" "cc" "dd" "ee" "ff" "gg" "hh", with each group of 2 characters in a separate cell. I think I know how to put each group in separate cells (using Data/Text to columns), but I don't know how to convert from "aabbccddeeffgghh" to "aa bb cc dd ee ff gg hh" (how to add the space after each 2 chars). How can I do it, using a macro (so automatically, not manual ).

    Thanks in advance!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    For i = 1 To Len(Range("A1").Value) Ste p 2

    tmp = tmp & Mid$(Range("A1").Value, i, 2) & " "
    Next i
    Range("B1").Value = tmp
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Howdy All,

    I read this a bit differently, but pretty beat up, so may well be off...

    [vba]
    Sub ex()
    Dim i As Long, s As String

    For i = 1 To Int(Len(Range("A1")) / 2)
    s = s & "[xx]"
    Next

    Range("A1").Parse s
    End Sub
    [/vba]

    ...or if you knew the initial string would never be longer than x...
    [vba]
    Sub ex2()
    Range("A1").Parse Left("[xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx]" & _
    "[xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx][xx]" & _
    "[xx][xx]", Len(Range("A1").Value) * 2)
    End Sub
    [/vba]

    Again, I may well be off...

    Mark

    Edit: ACK! I had an Int() in the second one, which was my bad...
    Last edited by GTO; 06-17-2009 at 06:48 AM.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Adapting a recorded macro you might use this to split the selected block of cells:[vba]Selection.TextToColumns Destination:=Selection.Offset(, 1), _
    DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
    Array(2, 1), Array(4, 1), Array(6, 1), Array(8, 1), _
    Array(10, 1), Array(12, 1)), TrailingMinusNumbers:=True[/vba]but this would only ever handle up to 7 pairs of letters.
    However you can just manually add to it, or you can let the code do it for you; just alter the value of j to the max number of pairs of letters:[vba]Sub Macro1()
    Dim yy()
    j = 15
    ReDim yy(0 To j)
    For i = 0 To j
    yy(i) = Array(i * 2, 1)
    Next i
    Selection.TextToColumns Destination:=Selection.Offset(, 1), _
    DataType:=xlFixedWidth, FieldInfo:=yy, _
    TrailingMinusNumbers:=True
    End Sub[/vba]You could even let the code determine j for you.
    (You can remove the .Offset(, 1) if you don't want to preserve your original column of info.)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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