简体   繁体   English

Excel VBA公式的不更新行

[英]Excel VBA formula's not updating row

So my code is to insert a row, copy its format from the previous row, and then insert the formulas that I have preset on a different row. 所以我的代码是插入一行,从上一行复制其格式,然后将我预设的公式插入另一行。 So lets say I insert a new row, row 11, the formula should copy row 10's formats and insert the formulas from row 44 (which I designated as my formula row). 因此,可以说我在第11行插入了新行,公式应该复制第10行的格式,并从第44行(我指定为我的公式行)插入公式。

Now, the formulas will all reference row 10 instead of row 11. I'm not sure why that is. 现在,公式将全部引用第10行而不是第11行。我不确定为什么会这样。

Here is my code: 这是我的代码:

Dim i As Integer

'loop through position sheets and insert blank rows
For i = 1 To 4
    Sheets(i).Select
    Rows(row_to_insert).EntireRow.Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Next i

'loop through sheets and copy/paste formulas into new rows
For i = 1 To 4
    Sheets(i).Select
    Rows(blank_row_to_use + 1).EntireRow.Select
    Selection.Copy
    Rows(row_to_insert).EntireRow.Select
    ActiveSheet.Paste
Next I

End Sub

This worked fine until I added a new sheet, and expanded from For i = 1 To 3 to For i = 1 to 4 . 在添加新工作表之前,此方法一直很好,然后从For i = 1 To 3扩展For i = 1 To 3For i = 1 to 4

Any ideas why it suddenly stopped working? 有什么想法为什么突然停止工作?

the issue lays in those relative references to another sheet that don't get updated by any rows shifts taking place in the sheet where they are used 问题出在那些相对引用另一个工作表的地方,这些引用不会因使用它们的工作表中发生的任何行移位而更新

while they'd get updated if you shifted rows in the sheet they are referencing 如果您在工作表中移动了行,它们就会更新

so you have to play a little bit with relative /absolute references to mimic some referenced sheet rows shifting, but without doing it! 因此,您必须使用相对/绝对引用来模拟一些引用的工作表行移动,但不要这样做!

for instance you could use a Function that converts some range formulas to absolute or relative reference type, like the following: 例如,您可以使用将某些范围公式转换为绝对相对引用类型的函数,如下所示:

Sub Convert(rng As Range, toReferenceType As XlReferenceType)
    Dim cell As Range

    For Each cell In rng.SpecialCells(XlCellType.xlCellTypeFormulas) ' loop thorugh passed range relevant cells (i.e. those containing formulas)
        If InStr(cell.Formula, "!") > 0 Then cell.Formula = Application.ConvertFormula(cell.Formula, xlA1, xlA1, toReferenceType) ' if current cell has an explicit sheet reference, then convert its formula to the passed reference type
    Next
End Sub

and the use it in your code 并在您的代码中使用

For i = 1 To 1
    With ThisWorkbook.Sheets(i)
        .Rows(blank_row_to_use).Copy .Rows(blank_row_to_use + 1) ' copy formulas "template" row one "helper" row below
        Convert .Rows(blank_row_to_use + 1), xlAbsolute ' convert "helper" row formulas with some explicit sheet reference to absolute type so they don't get updated by any subsequent row shift
        .Rows(blank_row_to_use + 1).Copy .Rows(blank_row_to_use) ' copy "helper" row converted formulas and paste them back to formula "template" row -> now you have a formula with an absolute row reference one below its own row
        .Rows(blank_row_to_use + 1).ClearContents ' clear "helper" row

        .Rows(row_to_insert).Insert Shift:=xlDown, opyOrigin:=xlFormatFromLeftOrAbove ' insert new row -> formulas "template" row references don't get updated and now you have a formula with an absolute row reference to its own row 
        Convert .Rows(blank_row_to_use + 1), xlRelative ' convert formulas "template" row formulas with some explicit sheet reference to relative type so they do get updated by any subsequent row shift

        .Rows(row_to_insert).EntireRow.Formula = .Rows(blank_row_to_use + 1).EntireRow.Formula ' copy formulas "template" row formulas row to the new row and have them updated
    End With
Next

Please note also that 另请注意

.Rows(row_to_insert).EntireRow.Formula = .Rows(blank_row_to_use + 1).EntireRow.Formula 

is better than any Copy and subsequent PasteSpecial approach in that it doesn't use the clipboard 比任何Copy和后续的PasteSpecial方法都更好,因为它不使用剪贴板

Try the much shorter and cleaner version of what you are trying to achieve. 尝试使用要实现的功能更短,更清洁的版本。

Note : try to avoid using Select , Selection and ActiveSheet , and use fully qualified Sheet objects. 注意 :尽量避免使用SelectSelectionActiveSheet ,而应使用完全限定的Sheet对象。

Modified Code 修改后的代码

'loop through position sheets and insert blank rows
For i = 1 To 4
    With ThisWorkbook.Sheets(i)
        .Rows(row_to_insert).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        .Rows(blank_row_to_use + 1).EntireRow.Copy
        .Rows(row_to_insert).EntireRow.PasteSpecial xlPasteFormulas
    End With
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM