+ Reply to Thread
Results 1 to 6 of 6

Copy row splitting out text string in one column

Hybrid View

  1. #1
    Registered User
    Join Date
    04-11-2013
    Location
    Brighton, England
    MS-Off Ver
    365
    Posts
    20

    Copy row splitting out text string in one column

    Hi, had some great help here before so hopefully someone can help with this..

    I have a file with potentially 10's of thousands of rows. In each row there is a field called "Country Responsible" and in this there can be one or more countries in the same cell separated by " - ".

    What I need is if, for example, there are two countries, say UK - FRANCE, then a new row inserted below with all the same data except in the country responsible field the original cell shows UK and the one below shows FRANCE

    I've attached a workbook with two sheets, the first one "Start" shows how the data comes to me and the second one "End" shows how I would like it to end up.

    I'd be very grateful for help with a macro that can do this.. note that the resulting data can either be on the original sheet but adjusted for the result i'm after or on a new sheet, I have no preference!

    Thanks in advance
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Copy row splitting out text string in one column

    Try
    Sub test()
        Dim a, b, x, e, i As Long, ii As Long, n As Long
        a = Sheets("start").Cells(1).CurrentRegion.Value
        ReDim b(1 To Rows.Count, 1 To UBound(a, 2) + 1)
        b(1, UBound(b, 2)) = "Percent"
        For i = 1 To UBound(a, 1)
            If a(i, 3) <> "" Then
                x = Split(a(i, 3), "-")
                For Each e In x
                    n = n + 1
                    For ii = 1 To UBound(a, 2)
                        b(n, ii) = a(i, ii)
                    Next
                    b(n, 3) = Trim$(e)
                    If n > 1 Then b(n, UBound(b, 2)) = 1 / (UBound(x) + 1)
                Next
            End If
        Next
        With Sheets("end").Cells(1).Resize(n, UBound(b, 2))
            .CurrentRegion.ClearContents
            .Columns(1).NumberFormat = "@"
            .Value = b: .Columns.AutoFit
        End With
    End Sub

  3. #3
    Registered User
    Join Date
    04-11-2013
    Location
    Brighton, England
    MS-Off Ver
    365
    Posts
    20

    Re: Copy row splitting out text string in one column

    Quote Originally Posted by jindon View Post
    Try
    Seems to work perfectly... thanks loads jindon!! Very much appreciated!

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,662

    Re: Copy row splitting out text string in one column

    I just noticed that you want a macro. Blast!!! Here's a formula. Give it a go!!

    helper in start F2, copied down:
    =LEN(C2)-LEN(SUBSTITUTE(C2,"-",""))+1

    End, A2 copied across to E2 and then down:
    =IFERROR(INDEX(Start!A$2:A$26,MATCH(0,INDEX(--(COUNTIF(A$1:A1,Start!A$2:A$26)=Start!$F$2:$F$26),0),0)),"")

    Overtype this into C2 and copy down:
    =IFERROR(TRIM(MID(SUBSTITUTE("-"&INDEX(Start!C$2:C$26,MATCH(0,INDEX(--(COUNTIF($A$1:$A1,Start!$A$2:$A$26)=Start!$F$2:$F$26),0),0)),"-",REPT(" ",125)),125*COUNTIF($A$2:$A2,$A2),125)),"")
    Attached Files Attached Files
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

  5. #5
    Registered User
    Join Date
    04-11-2013
    Location
    Brighton, England
    MS-Off Ver
    365
    Posts
    20

    Re: Copy row splitting out text string in one column

    Quote Originally Posted by Glenn Kennedy View Post
    I just noticed that you want a macro. Blast!!! Here's a formula. Give it a go!!

    helper in start F2, copied down:
    =LEN(C2)-LEN(SUBSTITUTE(C2,"-",""))+1

    End, A2 copied across to E2 and then down:
    =IFERROR(INDEX(Start!A$2:A$26,MATCH(0,INDEX(--(COUNTIF(A$1:A1,Start!A$2:A$26)=Start!$F$2:$F$26),0),0)),"")

    Overtype this into C2 and copy down:
    =IFERROR(TRIM(MID(SUBSTITUTE("-"&INDEX(Start!C$2:C$26,MATCH(0,INDEX(--(COUNTIF($A$1:$A1,Start!$A$2:$A$26)=Start!$F$2:$F$26),0),0)),"-",REPT(" ",125)),125*COUNTIF($A$2:$A2,$A2),125)),"")
    Thanks for your help Glenn.. I do prefer it in a macro but if I ever change the process to a formula i'll come back to this!

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Copy row splitting out text string in one column

    Here is the code for macro

    Sub Macro1()
    Dim TA As Long, T As Long, K As Integer
    
    Sheets("Start").Activate
    Application.ScreenUpdating = False
    
    T = 2
    
    Do While Range("A" & T) <> ""
    K = Len(Range("C" & T)) - Len(Replace(Range("C" & T), "-", ""))
        
        If K > 0 Then
        M = Split(Range("C" & T), "-")
        Range("A" & T + 1 & ":A" & T + K).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        
            For TA = 0 To K
            Range("A" & T & ":E" & T).Copy Range("A" & T + K & ":E" & T + K)
            Range("C" & T + TA) = Trim(M(TA))
            Range("F" & T + TA) = 1 / (K + 1)
            Next TA
        End If
    
    T = T + K + 1
       
    Loop
    Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

+ 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] Splitting Text String Into Separate Columns
    By robertguy in forum Excel General
    Replies: 2
    Last Post: 02-06-2016, 01:26 PM
  2. Splitting a text string
    By cmb80 in forum Excel General
    Replies: 3
    Last Post: 04-14-2015, 08:01 AM
  3. [SOLVED] splitting text string
    By paynod in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-22-2015, 04:42 AM
  4. splitting text string into different columns
    By lbm1978 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-13-2013, 05:13 PM
  5. [SOLVED] Splitting text (string function using macro )
    By amlan009 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2012, 02:26 PM
  6. Problems splitting a text & number string using VBA
    By Tamor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2009, 11:03 AM
  7. [SOLVED] Splitting a text string into string and number
    By mcambrose in forum Excel General
    Replies: 4
    Last Post: 02-21-2006, 11:50 AM

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