从另一范围复制行时,复制了同一范围的行

时间:2019-06-05 14:07:15

标签: excel vba

我有代码将具有公式错误的行粘贴到另一张纸上。

问题出在复制以下行:
“运行MF公式,复制/粘贴到MF输出表”(下半部分)。

即使这些单元格中的内容已清除,它似乎仍会复制“运行Lago公式,复制/粘贴到Lago输出表”时出错的行。我搜索了那些行中的任何错误,没有错误。

相同的代码可以在其他电子表格上正常工作。

我的理论是,它会记住错误行并重新复制它们,但是在第二次运行时应将其重置。

'Run Formulas for Lago, copy/Paste to Lago Output Sheet
With ws
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("AT2:BC" & lrow).Formula = .Range("AT2:BC2").Formula
End With

With Worksheets("Fall").Range("A3:CU" & lrow)
    On Error Resume Next
    Set eRng = .SpecialCells(xlCellTypeFormulas, xlErrors)
    On Error GoTo 0
    If Not eRng Is Nothing Then
        Intersect(.Parent.Range("A:CU"), eRng.EntireRow).Copy
        Worksheets("Lago").Range("A3").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    End With
Range("AT3:BC" & lrow).ClearContents

'Run Formulas for MF, copy/paste to MF output sheet
With ws
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("BE2:BN" & lrow).Formula = .Range("BE2:BN2").Formula
End With

With Worksheets("Fall").Range("A3:CU" & lrow)
    On Error Resume Next
    Set eRng = .SpecialCells(xlCellTypeFormulas, xlErrors)
    On Error GoTo 0
    If Not eRng Is Nothing Then
        Intersect(.Parent.Range("A:CU"), eRng.EntireRow).Copy
        Worksheets("MF").Range("A3").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    End With
Range("BE3:BN" & lrow).ClearContents

1 个答案:

答案 0 :(得分:0)

'Run Formulas for Lago, copy/Paste to Lago Output Sheet
With ws
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("AT2:BC" & lrow).Formula = .Range("AT2:BC2").Formula
End With

With Worksheets("Fall").Range("A3:CU" & lrow)
    On Error Resume Next
    Set eRng = .SpecialCells(xlCellTypeFormulas, xlErrors)
    On Error GoTo 0
    If Not eRng Is Nothing Then
        Intersect(.Parent.Range("A:CU"), eRng.EntireRow).Copy
        Worksheets("Lago").Range("A3").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    End With
Range("AT3:BC" & lrow).ClearContents

'Run Formulas for MF, copy/paste to MF output sheet
With ws
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("BE2:BN" & lrow).Formula = .Range("BE2:BN2").Formula
End With

With Worksheets("Fall").Range("A3:CU" & lrow)
    On Error Resume Next
    Set eRg = .SpecialCells(xlCellTypeFormulas, xlErrors) '<---- Changed to eRg from eRng
    On Error GoTo 0
    If Not eRg Is Nothing Then '<---- Changed to eRg from eRng
        Intersect(.Parent.Range("A:CU"), eRng.EntireRow).Copy
        Worksheets("MF").Range("A3").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
    End If
    End With
  Range("BE3:BN" & lrow).ClearContents