gpt4 book ai didi

excel - 使用 RGB 值的颜色单元 "Error 13"

转载 作者:行者123 更新时间:2023-12-03 02:33:52 25 4
gpt4 key购买 nike

非常简单的vba问题,不知道哪里出了问题,但是:

Range("J21").Select
For tastetherainbow = 1 To 1000
skittle = ActiveCell.Offset(0, 2).Value
Selection.Interior.Color = skittle
ActiveCell.Offset(1, 0).Select
Next

包含每个适当的 Skittle 值的单元格包含 RGB(r,g,b) 形式的 RGB 代码,与 VBA 的格式完全相同。我已经通过将单元格的值复制粘贴到 Selection.Interior.Color = paste 中进行了测试,没有任何问题,但当它只使用skittle时,我得到“类型不匹配”。

事实上,我使用skittle作为变量的唯一原因是当我使用Selection.Offset(0,2).Value设置颜色时遇到了同样的问题。

有点迷失了!您能否让我知道如何解决这个问题,以及为什么我遇到这个问题。

谢谢!

最佳答案

建议不要使用 SelectSelectionActiveCell ,而是我更喜欢从以下位置启动 For 循环:单元格“J21”并将行前进 1。

代码

Option Explicit

Sub CellColors()

Dim Skittle As Long
Dim tastetherainbow As Long

' modify "Sheet1" to your sheet's name
With Sheets("Sheet1")
For tastetherainbow = 1 To 1000
Skittle = .Cells(tastetherainbow + 20, "L").Value
.Cells(tastetherainbow + 20, "J").Interior.Color = Skittle
Next tastetherainbow
End With

End Sub

编辑代码:将“RGB(0,0,74)”的单元格字符串格式转换为0,0,74,然后使用Split函数将String 转换为数组的 3 个元素。然后使用 CIntRGB 方法计算 Skittle Long 数值。

Option Explicit

Sub CellColors()

Dim Skittle As Long
Dim CellRGBStr As String
Dim RGBInd() As String
Dim tastetherainbow As Long

With Sheets("Sheet3")
For tastetherainbow = 1 To 1000
If .Cells(tastetherainbow + 20, "L").Value <> "" Then

' use a string to store the "RGB(0,0,74)" as 0,0,74
CellRGBStr = Mid(.Cells(tastetherainbow + 20, "L").Value, 6, Len(.Cells(tastetherainbow + 20, "L").Value) - 7)

' split the CellRGBStr to 3 array elements
RGBInd = Split(CellRGBStr, ",")

' calculate the value of Skittle (using the RGB method)
Skittle = (CInt(RGBInd(0))) ^ 3 + (CInt(RGBInd(1))) ^ 2 + (CInt(RGBInd(2))) ^ 1
.Cells(tastetherainbow + 20, "J").Interior.Color = Skittle
End If
Next tastetherainbow
End With

End Sub

计算 Skittle 的另一种方法是使用 RGB 函数:

Skittle = RGB(CInt(RGBInd(0)), CInt(RGBInd(1)), CInt(RGBInd(2)))

关于excel - 使用 RGB 值的颜色单元 "Error 13",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40781651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com