【问题标题】:Change interior color of range of cells without using loops在不使用循环的情况下更改单元格范围的内部颜色
【发布时间】:2012-03-08 15:57:55
【问题描述】:

如何做到这一点?

Range(Cells(1,1),Cells(height,width)).Interior.Color=colorArray

colorArray 是包含颜色值的长度 (width*height) 长整数的一维数组。

以上代码返回

类型不匹配错误。

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

这段代码也返回

“运行时错误:‘13’类型不匹配”

【问题讨论】:

  • 嗯,这令人惊讶。我重新测试了我的代码,它工作得很好。您可以在 www.wikisend.com 上上传您的工作簿并将链接粘贴到此处以便我查看吗?

标签: excel vba


【解决方案1】:

我相信问题出在你的电话上。你应该使用:

Range(Cells(1, 1), Cells(Height, Width)).Interior.Color***Index*** = colorArray

这是我在 Excel VBA 中使用单元格背景颜色的经验。

-斯科特

【讨论】:

    猜你喜欢
    • 2014-06-16
    • 2016-04-20
    • 2023-02-04
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多