将具有条件的MS-excel转换为行值到列中

时间:2018-02-06 09:43:08

标签: excel vba excel-vba

我有像下面这样的Excel数据, enter image description here

参考上面的excel数据,我想过滤从Sheet1到sheet2的数据,如下所示。

enter image description here

2 个答案:

答案 0 :(得分:1)

可能的变体之一:

Sub test()
    Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
    Dim x&, Z%, cl As Range
    x = Cells(Rows.Count, "A").End(xlUp).Row
    Debug.Print (TypeName(Z))
    For Each cl In Range("A2:A" & x)
        append = cl.Offset(, 1) & "|" & cl.Offset(, 2) & "|" & cl.Offset(, 3)
        If Not Dic.exists(UCase(cl.Value2)) Then
            Dic.Add UCase(cl.Value2), append
        Else
            Dic(UCase(cl.Value2)) = Dic(UCase(cl.Value2)) & "|" & append
        End If
    Next cl
    Workbooks.Add
    x = 2
    For Each d In Dic
        Z = UBound(Split(Dic(d), "|")) + 1
        Cells(x, "A") = d: Cells(x, "B").Resize(, Z) = Split(Dic(d), "|")
        x = x + 1
    Next d
End Sub

试验:

源:

enter image description here

结果:

enter image description here

答案 1 :(得分:1)

没有VBA(总是更好的IMO):

=INDEX(
$B$1:$E$8,
MATCH($B12,$B$1:$B$8,0)+QUOTIENT(COLUMN(),3)-COLUMN($B$1)+1,
MOD(COLUMN(),3)+COLUMN($B$1)
)

设置如下:

  1. 在C12中键入公式
  2. 向右和向下拉伸公式
  3. enter image description here