+ Reply to Thread
Results 1 to 8 of 8

Manipulating strings

Hybrid View

  1. #1
    Registered User
    Join Date
    12-21-2016
    Location
    London
    MS-Off Ver
    2016 Professional
    Posts
    3

    Manipulating strings

    Hello,

    I want to manipulate strings of text/numbers. See attached my spreadsheet.

    If part of the string has one number in parentheses e.g. A(1), that can be left as it is. For parts with more than one number e.g. F(1,100,200), I want this broken down into F(1),F(100),F(200).

    The "Original" is my string currently and "New" is what I want it to finish as.

    I have hundreds of such strings so it cannot be done manually. Any suggestions/assistance will be appreciated.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Manipulating strings

    Put this Code into a Macro module:

    Function Expand(S As String)
    MyArray = Split(S, "),")
    
    T = UBound(MyArray)
    
    For Count = 0 To UBound(MyArray)
    
    If InStr(MyArray(Count), ",") = 0 Then
    
    Expand = Expand & MyArray(Count) & "),"
    
    Else
    
    T1 = Split(MyArray(Count), "(")
    T2 = Split(T1(1), ",")
    
    T = UBound(T2)
    
    For Counter = 0 To UBound(T2)
    Expand = Expand & T1(0) & "(" & T2(Counter) & "),"
    
    Next
    End If
    
    Next
    
    Expand = Left(Expand, Len(Expand) - 2)
    End Function

    Then this formula will do what you want:=

    Formula: copy to clipboard
    =Expand(A2)
    Attached Files Attached Files
    Last edited by mehmetcik; 12-21-2016 at 07:30 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    12-21-2016
    Location
    London
    MS-Off Ver
    2016 Professional
    Posts
    3

    Re: Manipulating strings

    Thanks. Using the code, how could I convert the string text into columns?

  4. #4
    Forum Expert José Augusto's Avatar
    Join Date
    10-29-2014
    Location
    Portugal
    MS-Off Ver
    2013-2016
    Posts
    3,329

    Re: Manipulating strings

    Hi
    You can try this approach with this code in a module
    Option Explicit
    
    Function ExpandStr_Col(ByVal rng As Range, col As Integer) As Variant
    
    On Error Resume Next
    
        Dim s As String, f As String, r As String
        Dim i As Integer, j As Integer, k As Integer, m As Integer, w As Integer
        
        s = rng.Value: k = 1: f = ""
        Do
            i = InStr(k, s, "("): r = Mid(s, k, i - k): j = InStr(i + 1, s, ")")
            f = f & r & "("
            For m = i + 1 To j
                If Mid(s, m, 1) = "," Then f = f & ")," & r & "(" Else f = f & Mid(s, m, 1)
            Next m
            If m < Len(s) Then f = f & ","
            k = m + 1
        Loop Until k >= Len(s)
        w = Len(s) - Len(Replace(s, ",", "")) + 1
        For i = w To col: f = f & ",": Next i
        
        ExpandStr_Col = Split(f, ",")
        
    End Function

    and using a array formula (need to be enter with Ctrl+Shift+Enter)
    Select C2:Q2 and use this formula
    Formula: copy to clipboard
    =ExpandStr_Col(A2,15)

    where 15 is the maximum length of the array. (you can change that)
    Select C2:Q2 and copy down

    See the file
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Manipulating strings

    You would need a Macro like this:-

    
    Sub Macro3()
    C = ActiveCell.Column
        Range(C & ":" & C).Value = Range(C & ":" & C).Value
        Columns(C).TextToColumns Destination:=Cells(1, C), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
            :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
            Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), TrailingMinusNumbers:=True
    End Sub

  6. #6
    Registered User
    Join Date
    12-21-2016
    Location
    London
    MS-Off Ver
    2016 Professional
    Posts
    3

    Re: Manipulating strings

    Thank you both for your help.

    I have noticed that some of my strings do not include any parentheses at all e.g. A(1),BA(1),C1(1,100),D1A(10),E(1),F(1,100,200),G(1),H,J,K,L,M

    Using the code in the first spreadsheet, such a string returned a #VALUE! error and in the second spreadsheet my Excel crashed. Could you assist in modifying the code to incorporate such instances?
    Attached Files Attached Files

  7. #7
    Forum Expert José Augusto's Avatar
    Join Date
    10-29-2014
    Location
    Portugal
    MS-Off Ver
    2013-2016
    Posts
    3,329

    Re: Manipulating strings

    Hi
    Use in C2 this
    Formula: copy to clipboard
    =ExpandStr(A2)
    to expand the string to your desired format and then use in D2
    Formula: copy to clipboard
    =Text2Column($C2,COLS($A$1:A$1))

    and copy right to perform Text to columns.

    See the file
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Manipulating strings

    
    Function Expand(S As String)
    
    'Handle floating letters
    
    'Test Start
    If Mid(S, 2, 1) = "," Then S = Left(S, 1) & "(0)" & Right(S, Len(S) - 1)
    
    'Test End
    If Mid(S, Len(S) - 1, 1) = "," Then S = S & "(0)"
    
    
    'Test In Between
    For Count = 1 To Len(S) - 2
    T = Mid(S, Count, 3)
    
    If Left(T, 1) = "," And Right(T, 1) = "," And Asc(Mid(T, 2, 1)) > 64 And Asc(Mid(T, 2, 1)) < 91 Then
    
    'Found Letter
    
    S = Left(S, Count + 1) & "(0)" & Right(S, Len(S) - Count - 1)
    
    End If
    
    Next
    
    MyArray = Split(S, "),")
    
    T = UBound(MyArray)
    
    For Count = 0 To UBound(MyArray)
    
    If InStr(MyArray(Count), ",") = 0 Then
    
    Expand = Expand & MyArray(Count) & "),"
    
    Else
    
    T1 = Split(MyArray(Count), "(")
    T2 = Split(T1(1), ",")
    
    T = UBound(T2)
    
    For Counter = 0 To UBound(T2)
    Expand = Expand & T1(0) & "(" & T2(Counter) & "),"
    
    Next
    End If
    
    Next
    
    Expand = Left(Expand, Len(Expand) - 2)
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Manipulating Strings in VB
    By hvincent in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-24-2014, 01:36 PM
  2. Manipulating strings in VBA
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-27-2010, 01:36 PM
  3. Manipulating strings
    By therealjag in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-29-2010, 08:32 AM
  4. Manipulating Text strings
    By xmoore in forum Excel General
    Replies: 13
    Last Post: 01-15-2010, 07:16 AM
  5. Manipulating Strings
    By OlYeller21 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-06-2009, 02:08 PM
  6. Manipulating strings in two columns
    By osalcido in forum Excel General
    Replies: 2
    Last Post: 03-05-2007, 08:41 PM
  7. [SOLVED] Manipulating Strings
    By Leslie Coover in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2005, 02:05 AM

Tags for this Thread

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