How to align duplicates on the same rows in Excel

前端 未结 2 1945
小蘑菇
小蘑菇 2020-12-08 06:20

This is a simple question that I cannot answer.

I have two columns like these in Excel:

Col1    Col2
 A       C
 B       I
 C       E
 D       D
 E           


        
2条回答
  •  再見小時候
    2020-12-08 06:39

    without VBA

    • insert a blank column into column B
    • in B1 put =IF(ISNA(MATCH(A1,C:C,0)),"",INDEX(C:C,MATCH(A1,C:C,0))) and copy down
    • copy and paste back column B over itself as values to remove the formulae

    In VBA

    Sub Macro1()
        Dim rng1 As Range
        Set rng1 = Range([a1], Cells(Rows.Count, "A").End(xlUp))
        rng1.Offset(0, 1).Columns.Insert
        With rng1.Offset(0, 1)
            .FormulaR1C1 = _
            "=IF(ISNA(MATCH(RC[-1],C[1],0)),"""",INDEX(C[1],MATCH(RC[-1],C[1],0)))"
            .Value = .Value
        End With
    End Sub
    

提交回复
热议问题