簡體   English   中英

在不使用循環的情況下更改單元格范圍的內部顏色

[英]Change interior color of range of cells without using loops

如何使這項工作?

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’類型不匹配”

上面的代碼返回類型不匹配錯誤。

約翰,您如何在代碼中定義colorArray? 這適合我。

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

我相信問題出在你的電話上。 你應該使用:

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

這是我在 Excel VBA 中使用單元格背景顏色的經驗。

-斯科特

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM