Excel 2010 VBA宏复制/粘贴

时间:2017-07-12 07:10:31

标签: excel vba excel-vba copy

我有这张表,其中许多单元格具有数值(例如:304 550.07),但实际上这取决于同一工作表中其他单元格的值。例如:" D1 = C1 + 1"是什么可以在公式栏中看到'和" 2"是D1单元格的实际数值。

我已经查找了一种方法来复制我需要的列(D)并将其粘贴到另一张纸上,这里是代码(不是我的):

Sub sbCopyRangeToAnotherSheet()
'Method 1
Sheets("Sheet1").Range("D1:D10").Copy Destination:=Sheets("Sheet2").Range("A1")

'Method 2
'Copy the data
Sheets("Sheet1").Range("D1:D10").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Range("A1").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False

End Sub   

因此,这两种方法有效但只有当单元格的值是数字(锁定?)而不是它们依赖于另一个单元格的值时才有效。 因此,当我尝试从' Sheet1'复制D列时到了Sheet2',我得到的只有:

=#REF!

在' Sheet1'中的公式栏中定义的计算,这意味着它采用了&2; Sheet2'的依赖单元格的值。

所有这一切,我试图找到一种方法来复制单元格中写入的实际值,而不是导致结果的计算。

PS:对于那些糟糕的解释真的很抱歉,先发帖后;) 感谢。

3 个答案:

答案 0 :(得分:1)

复制&只粘贴您应该在代码中使用类似内容的值:

'copy range
Sheets("Sheet1").Range("D1:D10").Copy
'paste only values 
Sheets("Sheet2").Range("A1").PasteSpecial xlPasteValues

答案 1 :(得分:0)

If you are only trying to assign values of some cells to be the values of some other cells, and you don't want to copy formats, formulas, etc, just use the .Value property of the cells:

Sub sbCopyRangeToAnotherSheet()
    Sheets("Sheet2").Range("A1:A10").Value = Sheets("Sheet1").Range("D1:D10").Value
End Sub

This avoids all the issues inherent in using the clipboard as an intermediary in the transfer process.

答案 2 :(得分:0)

Sheets.Range("D1:D10").SpecialCells(xlCellTypeVisible).Copy Destination:=Sheet2.Range("A1")
相关问题