开发者

Duplicate & Reduce Multi-column Data in Excel?

Say I have the following data in an Excel spreadsheet:

Andy      Dick          
Brad      Penny       Paisley      开发者_如何学编程 
Charlie   Daniels     Brown       Sheen     Schwab
Dave      Robinson          
Evan      Longoria          
Frank     Sinatra     Thomas    

What I need to do is reduce to two columns based on the parent-child relationship in the data to look like this:

Andy    Dick
Brad    Penny
Brad    Paisley
Charlie Daniels
Charlie Brown
Charlie Sheen
Charlie Schwab
Dave    Robinson
Evan    Longoria
Frank   Sinatra
Frank   Thomas

I'm not really much of an Excel guy, but there's gotta be a macro I can write or something right? Suggestions?


Sub ParentChild()

    Dim vaNames As Variant
    Dim i As Long, j As Long
    Dim aReturn() As String
    Dim lCnt As Long

    'Fill an array with values
    vaNames = Sheet1.Range("A1:E6").Value

    'loop through the "rows"
    For i = LBound(vaNames, 1) To UBound(vaNames, 1)
        'loop through the "cols" starting with 2
        For j = LBound(vaNames, 2) + 1 To UBound(vaNames, 2)
            'if there's a last name present
            If Len(vaNames(i, j)) > 0 Then
                'fill a new array with the first and last names
                lCnt = lCnt + 1
                ReDim Preserve aReturn(1 To 1, 1 To lCnt)
                aReturn(1, lCnt) = vaNames(i, LBound(vaNames, 2)) & " " & vaNames(i, j)
            End If
        Next j
    Next i

    'write the new array out to a range
    Sheet1.Range("A10").Resize(UBound(aReturn, 2), 1).Value = Application.WorksheetFunction.Transpose(aReturn)

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜