+ Reply to Thread
Results 1 to 4 of 4

Thread: Copy row based on criteria & clean up record

  1. #1
    Registered User
    Join Date
    09-02-2011
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    2

    Post Copy row based on criteria & clean up record

    Please can someone assist with a macro to do the following:
    1. Copy a row n number of times based on the data in column E. In the example attached, search for ";#". In this case, you need to copy row number 25 3 times.
    You now should have 4 rows with exactly the same data.

    2. Clean up the data in column E to have unique values with the rows just copied ie. in the example attached, one should end up with:-
    "Bank account less than 6 months old" in row number 25
    "Bank account not verified" in row number 26
    "case not rerouted" in row number 27
    "Case not spawned" in row number 28

    Thanks
    Neil
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    351

    Re: Copy row based on criteria & clean up record

    if you're not concerned about the rows being next to each other, this should work
    Sub ParsePounds()
        Dim rSearch     As Range
        Dim rFound      As Range
        Dim vParse      As Variant
        Dim vItem       As Variant
        Const cItem = ";#"
        
        Set rSearch = Range(Cells(2, "E"), Cells(Rows.Count, "E").End(xlUp))
        Set rFound = rSearch.Find(cItem, LookIn:=xlValues, LookAt:=xlPart)
    
        Do Until rFound Is Nothing
            vParse = Split(rFound.Text, cItem)
            For Each vItem In vParse
                If vItem = vParse(0) Then
                    rFound = vItem
                Else
                    Rows(Cells(Rows.Count, "A").End(xlUp).Offset(1).Row).Value = Rows(rFound.Row).Value
                    Cells(Rows.Count, "E").End(xlUp) = vItem
                End If
            Next
            Set rFound = rSearch.FindNext(rFound)
        Loop
        
        Set rSearch = Nothing
    End Sub
    Last edited by gjlindn; 09-02-2011 at 02:39 AM.
    -Greg If this is helpful, pls click Star icon in lower left corner

  3. #3
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    351

    Re: Copy row based on criteria & clean up record

    If you do have data following the row(s) you're fixing and want to keep the new rows all together, this code would be better
    Sub ParsePounds()
        Dim rSearch     As Range
        Dim rFound      As Range
        Dim vParse      As Variant
        Dim vItem       As Variant
        Const cItem = ";#"
        
        Set rSearch = Range(Cells(2, "E"), Cells(Rows.Count, "E").End(xlUp))
        Set rFound = rSearch.Find(cItem, LookIn:=xlValues, LookAt:=xlPart)
        
        Do Until rFound Is Nothing
            vParse = Split(rFound.Text, cItem)
            For Each vItem In vParse
                If vItem = vParse(0) Then
                    rFound = vItem
                Else
                    Rows(rFound.Row).Offset(1).Insert shift:=xlDown
                    Rows(rFound.Row).Offset(1).Value = Rows(rFound.Row).Value
                    Cells(rFound.Row, "E").Offset(1) = vItem
                End If
            Next
            Set rFound = rSearch.FindNext(rFound)
        Loop
        
        Set rSearch = Nothing
    End Sub
    Last edited by gjlindn; 09-02-2011 at 02:39 AM.
    -Greg If this is helpful, pls click Star icon in lower left corner

  4. #4
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    351

    Re: Copy row based on criteria & clean up record

    One more modification...This one keeps them in the same order they were originally listed in.
    Option Explicit
    
    Sub ParsePounds()
        Dim rSearch     As Range
        Dim rFound      As Range
        Dim vParse      As Variant
        Dim vItem       As Variant
        Dim iCount      As Integer
        Dim bScrUpd     As Boolean
        Dim bEvents     As Boolean
        Dim lCalc       As Long
        Const cItem = ";#"
        
        'Improve performance
        With Application
            bScrUpd = .ScreenUpdating
            bEvents = .EnableEvents
            lCalc = .Calculation
            .ScreenUpdating = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With
        
        Set rSearch = Range(Cells(2, "E"), Cells(Rows.Count, "E").End(xlUp))
        Set rFound = rSearch.Find(cItem, LookIn:=xlValues, LookAt:=xlPart)
        
        Do Until rFound Is Nothing
            iCount = 0
            vParse = Split(rFound.Text, cItem)
            For Each vItem In vParse
                If vItem = vParse(0) Then
                    rFound = vItem
                Else
                    iCount = iCount + 1
                    Rows(rFound.Row).Offset(iCount).Insert shift:=xlDown
                    Rows(rFound.Row).Offset(iCount).Value = Rows(rFound.Row).Value
                    Cells(rFound.Row, "E").Offset(iCount) = vItem
                End If
            Next
            Set rFound = rSearch.FindNext(rFound)
        Loop
        
        Set rSearch = Nothing
        
        'Restore Application Settings
        With Application
            .ScreenUpdating = bScrUpd
            .EnableEvents = bEvents
            .Calculation = lCalc
        End With
    End Sub
    Last edited by gjlindn; 09-02-2011 at 02:54 AM. Reason: Added speed up script for larger files
    -Greg If this is helpful, pls click Star icon in lower left corner

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0