[英]VBA - Macro for copy sheets, insert formulas and change row reference by +1
[英]VBA simple macro to copy formulas
我想將公式從列復制到行。 公式的地址中沒有$簽名,因此我不能使用特殊的粘貼選項,因為它會更改每個單元格的公式中的地址。
我嘗試運行宏,但它僅將列中的最后一個單元格復制到行中的每個人。
Sub Zamiana()
For Each y In Range("A1:B1")
For Each X In Range("A2:A3")
y.Formula = X.Formula
Next
Next
End Sub
在此先感謝您的幫助:)
一種簡單的方法是通過變量數組和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
嘗試添加兩個索引,一個用於列,一個用於行,並刪除一個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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.