
問題 :是否可以使用灰階值與儲存格值相同的顏色填入電子表格中的每個儲存格?
解釋 :
考慮我有一個電子表格如下:
所有數值都在 0-255 之間(對應於 256 種灰階顏色)。現在我想用其值是單元格中的值的顏色填充每個單元格。所以預期輸出如下:
另外,請注意每個單元格中的字體顏色是與背景顏色相對應的黑色或白色。
Excel 中是否有任何簡單的方法可以透過幾個步驟或一次執行此操作?
答案1
建立一個循環存取單元格的宏,讀取單元格中的值並將其指派給顏色,如下所示:
Range("A1:A6").Interior.Color = RGB(200,160,35)
答案2
User1024 是對的,並且有我的投票。
這是一個現成的解決方案:
Public Sub Demo()
Dim cel As Range
Dim myRange As Range
Set myRange = Range("A1:E10")
For Each cel In myRange
cel.Interior.Color = RGB(cel.Value, cel.Value, cel.Value)
If cel.Value > 127 Then
cel.Font.Color = RGB(0, 0, 0)
Else
cel.Font.Color = RGB(255, 255, 255)
End If
Next cel
End Sub
編輯:
如果出現字串或負值,此程式碼不會中斷...
Public Sub Demo()
Dim cel As Range
Dim greytone As Long
Dim myRange As Range
Set myRange = Range("A1:E10")
For Each cel In myRange
greytone = CLng(Abs(Val(cel.Value)))
cel.Interior.Color = RGB(greytone, greytone, greytone)
If greytone > 127 Then
cel.Font.Color = RGB(0, 0, 0)
Else
cel.Font.Color = RGB(255, 255, 255)
End If
Next cel
End Sub