将范围复制到虚拟范围

时间:2015-02-19 20:35:48

标签: excel vba excel-vba

是否可以将范围复制到虚拟范围,还是要求我将其粘贴到工作簿的另一个范围内?

dim x as range
x = copy of Range("A1:A4")

显然我通常使用以下代码

dim x as range
set x = Range("A1:A4")

但是在上面的例子中它只使x a"快捷方式"到该范围而不是范围对象本身的副本。这通常是我想要的,但最近我发现在内存中而不是在某个工作簿中完全保存范围及其所有属性是非常有用的。

2 个答案:

答案 0 :(得分:6)

我认为这就是你要做的事情:

'Set reference to range
Dim r As Range
Set r = Range("A1:A4")

'Load range contents to an array (in memory)
Dim v As Variant
v = r.Value

'Do stuff with the data just loaded, e.g.
'Add 123 to value of cell in 1st row, 3rd column of range 
v(1,3) = v(1,3) + 123

'Write modified data back to some other range
Range("B1:B4").Value = v

答案 1 :(得分:2)

  

是否可以将范围复制到虚拟范围?

不,不可能。 Range allways表示工作簿中工作表上的一些现有单元实例。

  

是否要求我将它粘贴在另一个范围内   工作簿?

这取决于你想做什么。您可以将一个范围内的任意粘贴到另一个范围,您只能粘贴例如公式到另一个范围。

dim x as range
set x = Range("A1:A4")
  

但是在上面的例子中它只使x a"快捷方式"到那个范围   而不是范围对象本身的副本。

变量x包含对该特定范围的引用。无法制作任何范围的独立副本。可以创建对范围的引用,并将everithing / something从一个范围复制到另一个范围。

  

最近我发现完全省钱是非常有用的   范围及其在内存中的所有属性,而不是在工作簿中   某处。

同样,无法将所有范围属性保存到特定范围的某个虚拟独立副本,因为Range allways表示现有的具体单元格集。你可以做的是用Range的一些属性甚至所有属性来创建你自己的类......但这将是一些额外的工作要做。

这里有一些例子如何使用range作为参数并将其复制到另一个范围。 HTH。

Option Explicit

Sub Main()
    Dim primaryRange As Range
    Set primaryRange = Worksheets(1).Range("A1:D3")

    CopyRangeAll someRange:=primaryRange
    CopyRangeFormat someRange:=primaryRange

    ' Value property of a range represents and 2D array of values
    ' So it is usefull if only values are important and all the other properties do not matter.
    Dim primaryRangeValues As Variant
    primaryRangeValues = primaryRange.value
    Debug.Print "primaryRangeValues (" & _
        LBound(primaryRangeValues, 1) & " To " & UBound(primaryRangeValues, 1) & ", " & _
        LBound(primaryRangeValues, 2) & " To " & UBound(primaryRangeValues, 2) & ")"
    ' Prints primaryRangeValues (1 To 3, 1 To 4)

    Dim value As Variant
    For Each value In primaryRangeValues
        ' This loop throught values is much quicker then to iterate through primaryRange.Cells itself.
        ' Use it to iterate through range when other properties except value does not matter.
        Debug.Print value
    Next value
End Sub

Private Sub CopyRangeAll(ByVal someRange As Range)
    ' Here all properties of someRange which can be copied are copied to another range.
    ' So the function gets a reference to specific range and uses all its properties for another range.
    Dim secondaryRange As Range
    Set secondaryRange = Worksheets(2).Range("D4:G6")
    someRange.Copy secondaryRange
End Sub

Private Sub CopyRangeFormat(ByVal someRange As Range)
    ' Here only formats are copied.
    ' Function receives reference to specific range but uses only one special property of it in that another range.
    Dim secondaryRange As Range
    Set secondaryRange = Worksheets(3).Range("G7:J9")
    someRange.Copy
    secondaryRange.PasteSpecial xlPasteFormats ' and many more e.g. xlPasteFormulas, xlPasteValues etc.
End Sub

Sheet1 Sheet2 Sheet3 Range values array