2013-01-15 96 views
0

我有多個行和列的Excel表格是這樣的:Excel宏:從表到

1 2 3 
4 5 6 
7 8 9 

我需要寫宏,這將改變在一個單列我的所有數據,並將其放置在sheet2

1 
2 
3 
4 
5 
6 
7 
8 
9 

回答

0
Sub Macro1() 

    Dim StartTable As Range  ' The original table of data 
    Dim RNdx As Long   ' Row index 
    Dim CNdx As Long   ' Column index 

    Dim TabeRowIndex As Long  ' Row number of Range("StartTable") 
    Dim TableColumnIndex As Long ' Column number of Range("StartTable") 
    Dim N As Long    ' Index into ColumnData 
    Dim WS As Worksheet   ' Worksheet reference 


    ''''''''''''''''''''''''''' 
    ' Initialize the variables. 
    ''''''''''''''''''''''''''' 
    Set ColumnData = Range("ColumnData") 
    Set StartTable = Range("StartTable") 
    TabeRowIndex = Range("StartTable").Row 
    TableColumnIndex = Range("StartTable").Column 
    Set WS = Worksheets("Sheet1") 
    N = 0 

    '''''''''''''''''''''''''''''''''''''' 
    ' Loop across then down filling 
    ' cells with element N of ColumnData. 
    '''''''''''''''''''''''''''''''''''''' 
    For RNdx = TabeRowIndex To (TabeRowIndex + (StartTable.Rows.Count)) 
     For CNdx = TableColumnIndex To TableColumnIndex + StartTable.Columns.Count - 1 
      N = N + 1 
      ColumnData.Cells(N, 1) = WS.Cells(RNdx, CNdx).Value 
     Next CNdx 
    Next RNdx 

End Sub 
0

sheet2使用公式備選答案:
(您列數爲3在這裏 - 你可以改變它)

=INDIRECT(CONCATENATE("'Sheet1'!"; ADDRESS(FLOOR(ROW()-1; 3)/3+1; MOD(ROW()-1; 3)+1)))