如何实现这一点?
Range(Cells(1,1),Cells(height,width)).Interior.Color=colorArray
colorArray 是包含颜色值的长度(宽度*高度)长整数的一维数组。
上面的代码返回
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
这段代码可以工作,但是速度太慢。我不想使用循环。
Range(Cells(1,1),Cells(height,width)).Value=colorArray
此代码使用 colorArray 中的颜色值填充范围,没有错误。我想要一个类似的代码来更改此范围的单元格的背景颜色。
请帮忙。
ReDim colorArray(1 To width*height) As Long
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
此代码还返回
"Run-time error: '13' Type mismatch"
最佳答案
我确实相信问题出在你的电话上。您应该使用:
Range(Cells(1, 1), Cells(Height, Width)).Interior.Color***Index*** = colorArray
这是我在 Excel VBA 中使用单元格背景颜色的经验。
-斯科特
关于excel - 不使用循环更改单元格范围的内部颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9325072/