2013-10-16 102 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