You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多工作表单行复制到新工作表遇公式引用错误,如何仅粘贴值?

解决VBA复制公式行出现引用错误的问题

原代码直接复制整行时会保留公式,导致目标工作表中的公式引用原工作表单元格,引发引用错误。以下两种修改方式可实现仅粘贴值:

方法1:使用选择性粘贴(PasteSpecial)

这种方式在原代码基础上修改,通过PasteSpecial指定仅粘贴单元格值:

Sub copyrow()
    Dim Nrow As Long, Nsheet As Long
    Dim i As Long

    Worksheets.Add(before:=Worksheets(1)).Name = "All Rows"

    Nrow = 30    'row to copy
    Nsheet = Worksheets.Count  'the count AFTER adding the destination worksheet

    For i = 2 To Nsheet
        '复制源工作表的第30行
        Worksheets(i).Cells(Nrow, "A").EntireRow.Copy
        '选择性粘贴值到目标工作表
        Worksheets(1).Cells(i - 1, "A").PasteSpecial Paste:=xlPasteValues
        '清除复制状态,避免剪贴板占用
        Application.CutCopyMode = False
    Next i
End Sub

方法2:直接赋值(更高效)

跳过复制粘贴操作,直接将源行的值赋值给目标行,执行速度更快,适合处理大量数据:

Sub copyrowValuesOnly()
    Dim Nrow As Long, Nsheet As Long
    Dim i As Long
    Dim sourceRow As Range, destRow As Range

    Worksheets.Add(before:=Worksheets(1)).Name = "All Rows"

    Nrow = 30    'row to copy
    Nsheet = Worksheets.Count  'the count AFTER adding the destination worksheet

    For i = 2 To Nsheet
        Set sourceRow = Worksheets(i).Rows(Nrow)
        Set destRow = Worksheets(1).Rows(i - 1)
        '直接将源行的值赋值给目标行
        destRow.Value = sourceRow.Value
    Next i
End Sub

内容的提问来源于stack exchange,提问作者Izehiuan Ideho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 18:23:12