Excel帮助进行数据转换

时间:2018-05-26 06:28:48

标签: excel vba excel-vba

需要Excel帮助:我正在使用包含以下格式数据的Excel工作表。

From/to | C1  | C2  | C3  |
---------------------------
R1      | 30  | 31  | 32  |
R2      | 40  | 41  | 42  |

我想使用micros / VBA Script或excel中的任何其他功能以下面的格式重新排列它们。

S.no   | From| TO  |Rate|
-------------------------
1      | R1  | C1  | 30 |
2      | R1  | C2  | 31 |
3      | R1  | C3  | 32 |
4      | R2  | C1  | 40 |
5      | R2  | C2  | 41 |
6      | R2  | C3  | 42 |

无需人工操作即可重新安排excel的任何帮助

1 个答案:

答案 0 :(得分:1)

测试下面。

Sub transData()
    Dim vDB, vR()
    Dim i As Long, r As Long, j As Integer
    Dim n As Long
    vDB = Range("a1").CurrentRegion
    r = UBound(vDB, 1)

    For i = 2 To r
        For j = 2 To 4
            n = n + 1
            ReDim Preserve vR(1 To 4, 1 To n)
            vR(1, n) = n
            vR(2, n) = vDB(i, 1)
            vR(3, n) = vDB(1, j)
            vR(4, n) = vDB(i, j)
        Next j
    Next i
    Sheets.Add
    Range("a1").Resize(1, 4) = Array("S.no", "From", "TO", "Rate")
    Range("a2").Resize(n, 4) = WorksheetFunction.Transpose(vR)
End Sub