2013-10-16 103 views
2

我想從列中複製公式到行中。公式在地址中沒有$簽名,所以我不能使用特殊的粘貼選項,因爲它會改變每個單元格公式中的地址。VBA簡單的宏來複制公式

我嘗試運行宏,但它只將列中的最後一個單元格複製到行中的所有人。提前幫助:)

Sub Zamiana() 
For Each y In Range("A1:B1") 
    For Each X In Range("A2:A3") 
     y.Formula = X.Formula 
    Next 
Next 
End Sub 

感謝

回答

0

嘗試加入兩個指標,一個列,一個用於行和刪除一個for循環, 像這樣

Sub Zamiana() 
iR = 1 
iC = 0 

For Each y In Range("A1:B1") 
r = y.Row + iR 
c = y.Column - iC  
Cells(r, c).Formula = y.Formula 
iR = iR + 1 
iC = iC + 1 

Next 

End Sub 
1

的簡單方法是通過變體數組和transpose函數傳遞公式。

像這樣:

Sub TransposeFormulas() 
    Dim rSrc As Range 
    Dim rDst As Range 
    Dim vSrc As Variant 

    Set rSrc = Range("A1:B1") 
    Set rDst = Range("A2") ' top left cell of destination 

    vSrc = rSrc.Formula ' copy source formulas into a variant array 

    ' size the destination range and transpose formulas into it 
    rDst.Resize(UBound(vSrc, 2), UBound(vSrc, 1)).Formula = _ 
     Application.Transpose(vSrc) 
End Sub