【问题标题】:vba method copy of object range failed对象范围的vba方法副本失败
【发布时间】:2014-06-06 15:11:33
【问题描述】:

我只是想将工作表上的一个范围复制到不同工作表上的另一个范围。据我了解,该方法是 Range1.Copy(DestRange) 我需要像这样指定工作表吗? Worksheets("sheet1").Range1.Copy(Worksheets("Destination").DestRange)

我的代码抛出一个错误: 运行时错误 1004: 对象“范围”的方法“复制”失败

我的代码:

Sub adsadsf()

Dim currentR As Range
Dim rawIput As Worksheet
Dim excitationWs As Worksheet

Set currentR = Range("B2", Range("B2").End(xlDown))
currentR.Select
Set rawInput = Worksheets("rawInput")
Set excitationWs = Worksheets("Excitation Curve")

'none of these work. all throw same error
'currentR.Copy (Range("H1"))
'currentR.Copy (Range("H1"))
'currentR.Copy (excitationWs.Range("A1"))
rawInput.Copy (excitationWs.Range("A1"))

End Sub

【问题讨论】:

  • currentR.Copy Range("H1") 不带括号。如果您想复制所有单元格,rawInput.Copy (excitationWs.Range("A1")) 也应该是 rawInput.Cells.Copy excitationWs.Range("A1")(效率低 - 最好复制工作表)。这可能很有趣:What is the difference between entering parameters in these four different ways
  • @simoco - 如果您在评论中回答问题,您希望如何获得高分? (+1)
  • @simoco 谢谢!这一直把我逼到一堵墙,我真的不明白一些有括号的方法之间的逻辑,然后突然有括号会导致一切陷入困境。你太棒了

标签: vba excel


【解决方案1】:

试试这个(如果需要,您可以重新添加工作表暗淡)

Dim currentR As Range
Dim destR As Range

Set currentR = Worksheets("rawInput").Range("B2", Range("B2").End(xlDown))
Set destR = Worksheets("Excitation Curve").Range("A1")

currentR.Cells.Copy destR

【讨论】:

  • 是的,我也这么认为。仅当将变量设置为方程式的一部分时才使用括号(即x=do.function (variable)do.function variable)。最好的做法是硬编码你的范围并远离.Select,如果它也不需要的话(因此我的代码演变)
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多