2015-01-26 47 views
-1

我正在嘗試將值轉置到同一工作表上的新行中。有人可以幫助我使用適當的vba代碼作爲我正在尋找的輸出嗎?在VBA中轉置循環

考慮:

Col1 Col2 Col3 Col4 
Title1 A  B  C 
Title2 D  E  
Title3 F 
title4 G  H 

通緝:

Col1 Col2 Col3 Col4 
Title1 A   
Title1 B 
Title1 C 
Title2 D  
Title2 E 
Title3 F 
Title4 G 
Title4 H 
+1

請澄清當前佈局和所需的一個。使用固定寬度的文字可能會有幫助。 – ExactaBox 2015-01-26 18:49:59

+0

你可能想看看http://stackoverflow.com/questions/13174916/transpose-a-range-in-vba和http://stackoverflow.com/questions/24456328/creating-and-transposing-array-in -vba – Codo 2015-01-26 18:56:57

回答

0

這個快速宏應該使該進程的短期工作。

Sub transpose_in_place() 
    Dim rw As Long, cl As Long 
    With ActiveSheet 
     For rw = .Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1 
      For cl = .Cells(rw, Columns.Count).End(xlToLeft).Column To 3 Step -1 
       If Not IsEmpty(.Cells(rw, cl)) Then 
        .Rows(rw + 1).Insert 
        .Cells(rw + 1, 1) = .Cells(rw, 1).Value2 
        .Cells(rw + 1, 2) = .Cells(rw, cl).Value2 
        .Cells(rw, cl).Clear 
       End If 
      Next cl 
     Next rw 
    End With 
End Sub 

,顧名思義,它會執行到位換位所以也許你提交到滿的過程之前應測試您的實際數據的副本。

enter image description here