2016-11-15 78 views
-3

我需要幫助,找到一種方法將多行組合到一行中。將多行組合成一行 - Excel

例如,我有8列(A-H)的數據在第1-4行和第5-8行(表1)。我需要一種方法將第2 - 4行的所有列移動到第1行的第I - AF行,將第4 - 8行的所有列移動到第5行(表2)的第I - AF行。產生的空白行也不是必需的。實際上,我需要多次應用這種方法。有任何想法嗎?

Table 1 
    A B C D E F G H 
1 1 2 3 4 5 6 7 8 
2 9 10 11 12 13 14 15 16 
3 17 18 19 20 21 22 23 24 
4 25 26 27 28 29 30 31 32 
5 1 2 3 4 5 6 7 8 
6 9 10 11 12 13 14 15 16 
7 17 18 19 20 21 22 23 24 
8 25 26 27 28 29 30 31 32 

Table 2 
    A B C D E F G H I … AD AE AF 
1 1 2 3 4 5 6 7 8 9 … 30 31 32 
2             
3             
4             
5 1 2 3 4 5 6 7 8 9 … 30 31 32 
6             
7             
8             
+1

「這個論壇的任何想法?」太寬泛了。堆棧溢出不是我網站的代碼。請張貼您嘗試過的任何代碼,並具體解釋錯誤,以便我們幫助您克服該特定錯誤。 –

+0

@ScottCraner - 我很抱歉。我相對較新使用Excel來操作大型數據集。對於未來的職位,我會先發布我的努力。 – KGHicks

回答

0

我小心翼翼地制定了VBA代碼,我不知道您Excel中的水平。我也照顧到讓代碼儘可能簡單。 在此代碼中(如下所示): 表1是顯示和讀取數據的工作表。 Sheet2是寫入數據的工作表。 RowsColsTransformer()是要運行的過程。

「VBA:

Sub RowsColsTransformer() 
Dim CurrentLine As Integer 
Dim CurrentLine2 As Integer 
Dim CurrentCols2 As Integer 
Dim Max As Integer 

CurrentLine2 = 2 
CurrentCols2 = 1 
Max = 10 ' -- Number of lines in the sheet where data will be read. 
For CurrentLine = 2 To Max 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 1) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 2) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 3) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 4) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 5) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 6) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 7) 
    CurrentCols2 = CurrentCols2 + 1 
    Sheets(2).Cells(CurrentLine2, CurrentCols2) = Sheets(1).Cells(CurrentLine, 8) 
    CurrentCols2 = CurrentCols2 + 1 
    '-- Reintialize Line and Column 
    If CurrentLine Mod 4 = 1 And CurrentLine > 1 Then 
     CurrentLine2 = CurrentLine2 + 1 
     CurrentCols2 = 1 
    End If 
Next CurrentLine 
End Sub 

Holp這可以幫助。

+0

感謝您的幫助!你的代碼工作得很好。 – KGHicks