使用RGB值的颜色单元格“错误13”

时间:2016-11-24 08:50:24

标签: excel-vba rgb vba excel

非常简单的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设置颜色时我遇到了同样的问题。

相当丢失!你可以让我知道如何解决它,为什么我有这个问题。

谢谢!

3 个答案:

答案 0 :(得分:2)

建议不要使用SelectSelectionActiveCell,而是让我从Cell" J21&#34开始For循环;然后将行前进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

已编辑的代码:转换&#34; RGB(0,0,74)&#34;的单元格字符串格式到0,0,74,然后使用Split函数将String s放入数组的3个元素中。 然后使用Skittle RGB 方法计算Long CInt数值。

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)))

答案 1 :(得分:1)

感谢所有帮助人员,我修改了它并且作了一点...这是作为很多更大的宏的一部分运行,所以我决定只生成R,G的数组,B值是明确的,并且这样做。从在线阅读Victor,它说RGB()期望值为整数 - 龙更有效但现在没有相同的东西吗?这是一个工作版本。如果没有你们,我们都非常感谢你们!

 Sub ColourMeImpressed()

    Dim Skittler As Long
    Dim Skittleg As Long
    Dim Skittleb As Long

    Dim tastetherainbow As Long


    With Sheets("Converter")
        For tastetherainbow = 21 To 1000
            Skittler = .Cells(tastetherainbow, "G")
            Skittleg = .Cells(tastetherainbow, "H")
            Skittleb = .Cells(tastetherainbow, "I")
                Skittle = RGB(Skittler, Skittleg, Skittleb)
                .Cells(tastetherainbow, "J").Interior.Color = Skittle
        Next tastetherainbow
    End With

    End Sub

对于那些感兴趣的人,这里是最终结果的屏幕截图:

enter image description here

答案 2 :(得分:0)

'little modification is required in your code as follows 
Range("J21").Select
For tastetherainbow = 1 To 1000
    skittle = ActiveCell.Offset(0, 2).Value
    Selection.Interior.Color = RGB(skittle)        
    ActiveCell.Offset(1, 0).Select    
Next