excel vba通过其他两个范围对象设置一个范围对象

这是我的问题。 我有两个Range对象。 例如,

Set rg3 = Range("B2") Set rg4 = Range("B3000") 

我想做这个

  Range("rg3:rg4").PasteSpecial (xlPasteAll) 

但它显示错误。 我怎样才能select两个范围对象的区域。 范围(“B2:B3000”)在我的情况下是不正确的,因为这两个范围总是通过偏移函数更新。

谢谢你的帮助!!!

当您inputRange(智能感知将显示Range(Cell1, Cell2) as Range指示范围对象期待两个单元格。

所以,看到rg3和rg4是两个单元格,你可以使用Range(rg3, rg4)

你正在使用xlPasteAll所以你可以使用RangeBeingCopiedReference.Copy Destination:=Range(rg3,rg4)

编辑 – 正如@罗宾所说,你是什么意思的抵消?

编辑2:
如果你想遍历一个范围,那么使用Cells很容易,因为它接受列号而不是列字母。

这个例子会一次将列A:J复制到U:AD一列。

 Sub Test() Dim rg3 As Range, rg4 As Range Dim x As Long With ThisWorkbook.Worksheets("Sheet1") For x = 1 To 10 .Range(.Cells(2, x), .Cells(3000, x)).Copy _ Destination:=.Range(.Cells(2, x + 20), .Cells(3000, x + 20)) Next x End With End Sub 

另请参阅WITH... END WITHhttps://msdn.microsoft.com/en-us/library/wc500chb.aspx

我想更好地了解您的需求,以获得更好的帮助

作为一个开始,因为你使用.PasteSpecial xlPasteAll我相信你设置一个循环外的源范围,并粘贴多次,在后面的移动粘贴范围

你也解释了“rg3和rg4在for循环中,每次它将通过offset(0,1)移动到下一个colmn”

所以这最初会导致:

 Option Explicit Sub main() Dim copyRng As Range, rg3 As Range, rg4 As Range Dim i As Long Set rg3 = Range("B2") '<~~ your rg3 range setting Set rg4 = Range("B3000") '<~~ your rg4 range setting Set copyRng = ... '<~~ your setting of the "source" range to be copied once and pasted many copyRng.Copy '<~~ copy "source" Range once ... With Range(rg3, rg4) '<~~ ... set your initial "target" range ... For i = 1 To 10 .Offset(, i).PasteSpecial xlPasteAll '<~~ ... and paste "source" range offseting "target" once Next i End With End Sub 

但是这也是无用而漫长的,因为你可以简单地写:

 Option Explicit Sub main() Dim copyRng As Range, rg3 As Range Set rg3 = Range("B2") '<~~ just set the "beginning" of the target range Set copyRng = ... '<~~ your setting of the "source" range to be copied once and pasted many copyRng.Copy copyRng.Copy Destination:=rg3.Resize(, 10) End Sub 

那么你真正的需求是什么?