获取具有所有单元属性的一系列单元格

时间:2015-08-14 21:16:39

标签: excel excel-vba vba

是否可以一次性获取一系列具有所有单元格“属性”的单元格,例如值,单元格背景颜色,单元格字体等,然后将其存储在数组中。

而不是多次遍历范围并一次获得一个属性?

例如,这将是意图,但我知道它不起作用:

Dim cellData() As Variant
cellData= Range("A36:W36")
Debug.Print cellData(1,1).Value
Debug.Print cellData(1,1).Interior.ColorIndex

由于

1 个答案:

答案 0 :(得分:1)

这里有一些我很快就能看到它是否正确的道路:

Sub addToArray()
Dim rng As Range, cel As Range
Dim cellAttributes()
Dim i As Integer, k As Integer

' We're going to store 2 cells' attributes.  We're going to use 3 attributes, hence the next line
ReDim cellAttributes(1 To 2, 1 To 3) ' since you want Value, Font, and Color, I chose up to three. Change this if you want more attributes
'The array will be cellAttributes([Cell Address],{value, font, color})

Set rng = Range("A1:A2")
i = 1
k = 1
For Each cel In rng
    cellAttributes(i, k) = cel.Value
    cellAttributes(i, k + 1) = cel.Font.Name
    cellAttributes(i, k + 2) = cel.Interior.ColorIndex
    Debug.Print "Cell " & cel.Address & " has a value of: " & cellAttributes(i, 1) & ", font: " & cellAttributes(i, 2) & " and bg color index: " & cellAttributes(i, 3)
    i = i + 1
    k = 1
Next cel


End Sub

在仔细阅读您的问题之后,您究竟想要做什么?上面的宏将一次查看每个单元格,然后在继续之前将属性应用于数组。当然,你可以让它变得更有活力(我使用了更多"魔术数字"比我更舒服,但我只是想确保这是(或者不是)你的&#t; #39;正在寻找)。