在不使用循环的情况下更改单元格范围的内部颜色 [英] Change interior color of range of cells without using loops
问题描述
如何进行这项工作?
Range(Cells(1,1),Cells(height,width)).Interior.Color=colorArray
colorArray是长整数(包含颜色值)的一维长整数数组.
colorArray is one-dimensional array of Long Integers of length (width*height) containing color values.
上面的代码返回
类型不匹配错误.
Type mismatch error.
For i = 1 to height
For j = 1 to width
t=(i-1)*width+j
Cells(i,j).Interior.Color=colorArray(t)
Next
Next
此代码有效,但速度太慢.我不想使用循环.
This code works but is too slow. I dont want to use loops.
Range(Cells(1,1),Cells(height,width)).Value=colorArray
此代码使用colorArray中的颜色值填充范围,没有错误.我想要类似的代码来更改此范围的单元格的背景颜色.
This code fills the range with color values from the colorArray with no error. I want a similar code to change background color of cells of this range.
请帮助.
ReDim colorArray(1 To width*height) As Long
Siddharth Rout的示例代码:
Sample code by Siddharth Rout:
Sub Sample()
Dim colorArray(21) 'or Dim colorArray(21) As Long/Integer
Dim Height As Long, Width As Long
For i = 0 To 21
colorArray(i) = i
Next
Height = 10
Width = 2
Range(Cells(1, 1), Cells(Height, Width)).Interior.Color = colorArray
End Sub
此代码也返回
运行时错误:'13'类型不匹配"
"Run-time error: '13' Type mismatch"
推荐答案
我确实认为问题出在您的电话上.您应该使用:
I do believe the problem lies in your call. You should use:
Range(Cells(1, 1), Cells(Height, Width)).Interior.Color***Index*** = colorArray
这是我在Excel VBA中使用单元格背景色的经验.
That is my experience using cell background colors in Excel VBA.
-斯科特
这篇关于在不使用循环的情况下更改单元格范围的内部颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!