如何将excel行堆叠到列中? [如下所示]

时间:2017-09-20 05:51:09

标签: excel excel-vba excel-formula vba

我有一个包含50,000条记录的数据集。格式如下所示。

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData("text");
  var clonedElement = document.getElementById(data).cloneNode(true);
  clonedElement.id = "element_1"; // Set unique id for each element I have used static one.
  ev.target.appendChild(clonedElement);
}

我希望结果表看起来如下所示。

Category | A   | B   | C
---------|-----|-----|-----
X        | 100 | 120 | 150
Y        | 200 | 220 | 250
Z        | 300 | 320 | 350

如何在MS Excel中执行此操作?

1 个答案:

答案 0 :(得分:-1)

希望这会对你有帮助;)

        Public Type res
            cat As String
            mtype As String
            price As Long
        End Type
        Sub arranger()
        'defs+inits
            Set src = Sheets("src")
            Set res = Sheets("result")
            Dim mydatas() As res
            lastrow = src.Cells(1, 1).End(xlDown).Row
            'just for safe :)
            If lastrow > 65000 Then Exit Sub
            ReDim mydatas(2 To lastrow * 3)
            act = 2

        'fill array
            For actrow = 2 To lastrow
                For col = 2 To 4
                    mydatas(act).cat = src.Cells(actrow, 1)
                    mydatas(act).mtype = src.Cells(1, col)
                    mydatas(act).price = src.Cells(actrow, col)
                    act = act + 1
                Next col
            Next actrow
        'write back in new format
            'headers
            res.Cells(1, 1) = "Cat"
            res.Cells(1, 2) = "Type"
            res.Cells(1, 3) = "price"
            'datas
            For act = 2 To UBound(mydatas) - 2
                res.Cells(act, 1) = mydatas(act).cat
                res.Cells(act, 2) = mydatas(act).mtype
                res.Cells(act, 3) = mydatas(act).price
            Next act

        End Sub