Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复制值不是公式excel vba_Vba_Excel - Fatal编程技术网

复制值不是公式excel vba

复制值不是公式excel vba,vba,excel,Vba,Excel,我有一段代码可以将一行从一个excel复制到另一个excel。问题在于,列E到G和列N到O引用了另一个excel,当它复制公式时,它复制的是公式,而不是单元格值,从而导致将公式按降序重复到目标列。我尝试过隐藏/取消隐藏,但没有太大区别。目标列D将导致D1=1.xslm/sheet1/formula(n1);D2=2.xslm/sheet1/公式(n2)…-这是源表D列中的参考。在源表中,值是ok,在目标表中,公式是错误的,不应该有n1,n2。如果目标中的源行是122,则它应该是D1=1.xslm

我有一段代码可以将一行从一个excel复制到另一个excel。问题在于,列E到G和列N到O引用了另一个excel,当它复制公式时,它复制的是公式,而不是单元格值,从而导致将公式按降序重复到目标列。我尝试过隐藏/取消隐藏,但没有太大区别。目标列D将导致D1=1.xslm/sheet1/formula(n1);D2=2.xslm/sheet1/公式(n2)…-这是源表D列中的参考。在源表中,值是ok,在目标表中,公式是错误的,不应该有n1,n2。如果目标中的源行是122,则它应该是D1=1.xslm/sheet1/formula(n122),D2=2.xslm/sheet1/formula(n122)

替换:

Source.Rows(c.Row).Copy Target.Rows(1)
作者:

这将粘贴值,而不是公式

编辑
这个答案是一个快速和肮脏的修复!查看Jeped的答案表,了解OP代码的更广泛改进。

要开始,请查看这一行

For Each c In Source.Range("a1:a" & Cells.SpecialCells(xlCellTypeLastCell).Row)
单元格.SpecialCells(…
未显式引用源工作表。它隐式引用。巧合的是,由于打开该工作簿使其成为活动工作表,因此这也恰好是源工作表。但是,不应依赖于此。最好显式定义所有工作表属性

For Each c In Source.Range("a1:a" & SOURCE.Cells.SpecialCells(xlCellTypeLastCell).Row)
就仅复制值而言,您可以使用带有xlPasteValues的。但是,直接值传输是一种更有效的传输值的方法,它不涉及剪贴板或

取代所有这些

For Each c In Source.Range("a1:a" & Cells.SpecialCells(xlCellTypeLastCell).Row)
    If c = lNum Then
       Source.Rows(c.Row).Copy Target.Rows(1)
    End If
Next c
…有了这个

Dim rw as Variant
With Source
    rw = Application.Match(lNum, .Columns(1), 0)
    If Not IsError(rw) Then
        With .Range(.Cells(rw, "A"), .Cells(rw, .Columns.Count).End(xlToLeft))
            Target.Cells(1, 1).Resize(.Rows.Count, .Columns.Count) = .Value
        End With
    End If
End With
这将获取从列A到源工作表匹配行上最后填充的单元格的所有内容,并将值传输到从列A1向外辐射的目标工作表

For Each c In Source.Range("a1:a" & Cells.SpecialCells(xlCellTypeLastCell).Row)
    If c = lNum Then
       Source.Rows(c.Row).Copy Target.Rows(1)
    End If
Next c
Dim rw as Variant
With Source
    rw = Application.Match(lNum, .Columns(1), 0)
    If Not IsError(rw) Then
        With .Range(.Cells(rw, "A"), .Cells(rw, .Columns.Count).End(xlToLeft))
            Target.Cells(1, 1).Resize(.Rows.Count, .Columns.Count) = .Value
        End With
    End If
End With