基于1列的值对数据进行分组,然后使用宏在Excel中以行格式打印

时间:2015-05-08 09:32:16

标签: excel excel-vba vba

我的数据格式为

1   45  
1   34  
1   35  
1   36  
1   37  
1   38  
1   23  
2   24  
2   25  
2   26  
2   27  
3   28  
3   29  

我希望输出像

1   45  34  35  36  37  38 23    
2   24  25  26  27  
3   28  29          

我尝试过使用宏但我无法做到。请通过宏来帮助解决它。

1 个答案:

答案 0 :(得分:0)

从这开始:

enter image description here

运行此宏:

Sub DataReorganizer()
    Dim N As Long, K As Long, L As Long, i As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    K = 1
    L = 4
    Cells(K, L).Value = Cells(1, 1).Value
    Cells(K, L + 1).Value = Cells(1, 2).Value
    L = L + 2

    For i = 2 To N
        If Cells(i, 1) = Cells(i - 1, 1) Then
            Cells(K, L) = Cells(i, 2)
            L = L + 1
        Else
            L = 4
            K = K + 1
            Cells(K, L) = Cells(i, 1)
            Cells(K, L + 1) = Cells(i, 2)
            L = L + 2
        End If
    Next i
End Sub

产生这个:

enter image description here