将范围复制到虚拟范围

是否有可能复制一个范围到一个虚拟的范围或需要我sloppily粘贴到另一个范围的工作簿?

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

显然我通常使用下面的代码

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

但在上面的例子中,它只会使xa“快捷”到该范围,而不是范围对象本身的副本。 这通常是我想要的,但最近我发现将范围及其所有属性存储在内存中而非工作簿中是非常有用的。

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

不,这是不可能的。 范围allways表示工作簿中工作表上某些现有的单元格实例。

这是否需要我将它粘贴到工作簿中的另一个范围?

这取决于你想要做什么。 你可以从一个范围粘贴到另一个范围,你可以粘贴只是像公式到另一个范围。

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

但是在上面的例子中,它只会使xa“快捷”到该范围,而不是范围对象本身的副本。

variablesx保存对特定范围的引用。 这是不可能做任何一个范围的独立副本。 有可能创build一个范围的引用,并将从一个范围复制到另一个范围。

最近我发现将范围和所有属性存储在内存中而不是在工作簿中是非常有用的。

同样,将所有范围属性保存到特定范围的某个虚拟独立副本是不可能的,因为范围总是代performance有的具体的单元集。 你可以做的是创build一个范围的属性,甚至所有的属性,你自己的类…但这将是一些额外的工作要做。

这里有一些例子,如何使用范围作为参数,并将其复制到另一个范围。 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 eg xlPasteFormulas, xlPasteValues etc. End Sub 

工作表Sheet1Sheet2中表Sheet 3范围值数组

我想这是你正在做的事情:

 '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, eg '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