![](/img/trans.png)
[英]Copying data from many workbooks to a summary workbook with Excel-VBA. Run time errors
[英]Excel-VBA. Moving formulas without copying them
我從我的宏中獲取了以下代碼,它工作正常:
.Cells(curRow, "M").Copy
.Range("N" & curRow & ":R" & curRow).PasteSpecial Paste:=xlPasteFormulas
我想問一下是否有辦法移動公式而不復制粘貼? 我需要使用更改的引用來移動它們。
我認為復制粘貼不是最快的方法。
如果你想更改引用,我知道的唯一方法是復制和粘貼。 如果您不想這樣,您可以使用:
.Range("N" & curRow & ":R" & curRow).FormulaR1C1 = .Cells(curRow, "M").FormulaR1C1
.Range("N" & curRow & ":R" & curRow).Cells.Formula = .Cells(curRow, "M").Formula
試試這個
.Cells(curRow, "M").Copy .Range("N" & curRow & ":R" & curRow)
這是在公式中移動/移動引用的最佳方法,它使用本機 VBA function ConvertFormula()
!
Function MoveFormulaReferences$(strFormula$, rngRelativeTo As Range, lngOffsetRows&, lngOffsetColumns)
MoveFormulaReferences = Application.ConvertFormula( _
Application.ConvertFormula( _
strFormula, _
XlReferenceStyle.xlA1, _
XlReferenceStyle.xlR1C1, _
, _
rngRelativeTo), _
XlReferenceStyle.xlR1C1, _
XlReferenceStyle.xlA1, _
, _
rngRelativeTo.Offset(lngOffsetRows, lngOffsetRows))
End Function
現在舉一些例子:
?MoveFormulaReferences("=C3*2+C3", Range("$D$4"), -2, -2)
返回=A1*2+A1
?MoveFormulaReferences("=$C3*2+C3", Range("$D$4"), -2, -2)
回報=$C1*2+A1
也可以使用ConvertFormula()
function 的ToAbsolute
參數“強制移動”所有引用,即使是由前導$
設置的引用:在嵌套的ConvertFormula()
調用中傳遞XlReferenceType.xlRelative
作為參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.