将宽数据更改为长格式 – VBA。 如何select工作表?

我一直在寻找一个宽到长的格式的代码,现在在这里find一些非常有用的东西。 我采取了这一点,并作了一些修改,以适用于我的数据…完美的作品。

我的问题是,我在哪里添加图纸select? 即我想能够从一个不同的页面上的button运行macros。 目前代码只能在原始源表单中执行。

Sub Wide_To_Long() Dim Rng As Range, Dn As Range, Dta, col As Integer Dim c As Integer, LastDt As Integer, LastVis As Integer LastDt = Cells("1", Columns.Count).End(xlToLeft).Column Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp)) ReDim Ray(1 To Rng.Count * LastDt, 1 To LastDt) For Each Dn In Rng LastVis = Cells(Dn.Row, Columns.Count).End(xlToLeft).Column For col = 8 To LastVis c = c + 1 For Dta = 0 To 8 Select Case Dta Case Is = 7 Ray(c, Dta + 1) = Cells(1, col) Case Is = 8 Ray(c, Dta + 1) = Dn.Offset(, col - 1) Case Else Ray(c, Dta + 1) = Dn.Offset(, Dta) End Select Next Dta Next col Next Dn Sheets("Sheet2").Range("A2").Resize(c, LastDt).Value = Ray End Sub 

有可能是更好的做法(如果来自Lowpar知道在哪里做出改变),但是我发现在第一个dim之前插入一个表格(“”)。select到表格的数据范围工作得很好。