ピボットテーブルの元データ作成

x1 x2 x3
y1 1 3 5
y2 2 4 6

上のようなピボットテーブルになっていない普通の表をワークシートの左上にコピペして、
マクロを実行すると、その下に以下のような表を作成する。
そうすれば、これを元データにしてピボットテーブルが作れる。

x y value
x1 y1 1
x1 y2 2
x2 y1 3
x2 y2 4
x3 y1 5
x3 y2 6

マクロのソース

Sub inv()
    ymax = Cells(2, 1).End(xlDown).Row - 1
    Cells(ymax + 3, 1) = "x"
    Cells(ymax + 3, 2) = "y"
    Cells(ymax + 3, 3) = "value"
    For x = 2 To Cells(1, 2).End(xlToRight).Column
        For y = 2 To ymax + 1
            r = (x - 1) * ymax + y + 2
            Cells(r, 1) = Cells(1, x)
            Cells(r, 2) = Cells(y, 1)
            Cells(r, 3) = Cells(y, x)
        Next y
    Next x
End Sub

Ruby版はこちら

ruby -ne \'l="";$.==1?c=.split(/,|\n/):.split(/,|\n/).each_with_index{|v,i|i==0?l=v:puts([c[i],l,v].join(","))}\'

http://d.hatena.ne.jp/n9d/20070723/1185171375