Excel VBA公式的不更新行

时间:2018-09-30 22:34:51

标签: excel vba excel-vba

所以我的代码是插入一行,从上一行复制其格式,然后将我预设的公式插入另一行。因此,可以说我在第11行插入了新行,公式应该复制第10行的格式,并从第44行(我指定为我的公式行)插入公式。

现在,这些公式将全部引用第10行而不是第11行。我不确定为什么会这样。

这是我的代码:

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

在我添加新工作表并将其从For i = 1 To 3扩展到For i = 1 to 4之前,此方法工作正常。

有什么想法为什么突然停止工作?

2 个答案:

答案 0 :(得分:1)

问题在于对其他工作表的相对引用,这些引用不会因使用它们的工作表中发生的任何行移位而更新

如果您移动工作表中所引用的行,它们将得到更新

所以您必须在相对/绝对引用中扮演一些角色,以模仿某些被引用的工作表行移动,但不这样做!

例如,

您可以使用将某些范围公式转换为 absolute relative 引用类型的函数,如下所示:

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

并在您的代码中使用

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

请注意

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

比任何Copy及其后继的PasteSpecial方法都更好,因为它不使用剪贴板

答案 1 :(得分:0)

尝试要实现的目标短得多,更干净的版本。

注意:尝试避免使用SelectSelectionActiveSheet,并使用完全合格的Sheet对象。

修改后的代码

'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