Excel - combine multiple columns to a single column

后端 未结 2 693
粉色の甜心
粉色の甜心 2021-01-06 19:16

My excel sheet looks like this

      c1        c2      c3      c4

ROW1   abc      def      1       2

ROW2   abc      def      3       4

ROW3   klm      e         


        
相关标签:
2条回答
  • 2021-01-06 19:44

    You can do this using the excel concatenate function. Here's a link to a good tutorial

    Also, to deal with the duplicates, you can have excel highlight duplicate entries so they can be easily deleted. See here

    0 讨论(0)
  • 2021-01-06 19:45

    This code will

    • Run on columns A:D on the current sheet
    • Group records that are common in columns A and B togther with combined column C and column D values respectively
    • runs case insensitive
    • outputs to a new sheet

    enter image description here

        Sub QuickCombine()
        Dim X()
        Dim Y()
        Dim objDic As Object
        Dim lngRow As Long
        Dim lngCol As Long
        Dim ws As Worksheet
    
        X = Range([a1], Cells(Rows.Count, "D").End(xlUp))
        Y = X
        Set objDic = CreateObject("scripting.dictionary")
    
        For lngRow = 1 To UBound(X, 1)
            If Not objDic.exists(LCase$(X(lngRow, 1) & X(lngRow, 2))) Then
                objDic.Add LCase$(X(lngRow, 1) & X(lngRow, 2)), lngRow
            Else
                Y(lngRow, 1) = vbNullString
                Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 3) = Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 3) & "," & X(lngRow, 3)
                Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 4) = Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 4) & "," & X(lngRow, 4)
            End If
        Next
    
        Set ws = Sheets.Add
    
        ws.[a1].Resize(UBound(X, 1), UBound(X, 2)) = Y
        ws.Columns("A").SpecialCells(xlBlanks).EntireRow.Delete
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题