简体   繁体   English

Excel vba:将所有属性分配给范围数组中的单元格

[英]Excel vba : assign all properties to a cell from an array of range

My goal is to save a Cell and all its properties and value in an array of range, and then write back all properties and values of this cell to another cell from the array (like a copy paste function but pasting from the array). 我的目标是将一个Cell及其所有属性和值保存在一个范围数组中,然后将该单元格的所有属性和值写回到数组中的另一个单元格(如复制粘贴函数,但从数组中粘贴)。

Here is a simple test procedure : 这是一个简单的测试程序:

Sub Test()
    Dim Range_Grid(1) As Range
    Dim CellAdress As String
    Dim i As Long

    Set Range_Grid(1) = ActiveSheet.Cells(2, 3)
    ActiveSheet.Cells(4, 1) = Range_Grid(1)
End Sub

So here in the first element of the array Range_Grid(1), I really get the full range saved and I can access every property the original range ActiveSheet.Cells(2, 3) had, like font style, format, color, comment, etc.. 所以这里在数组Range_Grid(1)的第一个元素中,我真的得到了全范围保存,我可以访问ActiveSheet.Cells(2,3)原始范围的每个属性,如字体样式,格式,颜色,注释,等等..

But when I try to write this range from array to another empty cell, it only write the value... 但是当我尝试将此范围从数组写入另一个空单元格时,它只会写入值...

Any idea how to write all the properties from the array like if it was a copy/paste from sheet to sheet? 知道怎么写数组中的所有属性,就好像它是从一张到另一张的复制/粘贴一样?

There is no way of moving all of the formatting from a range in an array back into excel at once. 无法一次将所有格式从数组中的范围移回excel。 This leaves you with two options: either you copy within excel as Scott Craner suggested, or you copy each type of formatting individually. 这将为您提供两个选项:您可以像Scott Craner建议的那样在excel中复制,也可以单独复制每种类型的格式。 For example, if you wanted to copy the value (which is the default and hence is what is copied in your original code) and the cell background colour, then you could use the following code: 例如,如果要复制值(默认值,因此是原始代码中复制的值)和单元格背景颜色,则可以使用以下代码:

Sub Test()
    Dim Range_Grid(1) As Range

    Set Range_Grid(1) = ActiveSheet.Cells(2, 3)
    ActiveSheet.Cells(4, 1) = Range_Grid(1)
    ActiveSheet.Cells(4, 1).Interior.ColorIndex = Range_Grid(1).Interior.ColorIndex
End Sub

Unfortunately there are probably over a dozen types of formatting that you may want to deal with at any given time... 不幸的是,您可能希望在任何给定时间处理十几种类型的格式...

There's an alternative: use Copy and PasteSpecial functions. 还有另一种选择:使用Copy和PasteSpecial函数。

Sub Test()

    ActiveSheet.Cells(2, 3).Copy
    ActiveSheet.Cells(4, 1).PasteSpecial(xlPasteAll)

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM