如何在Excel VBA中将单元格string值RGB转换为颜色编号?

我有包含颜色值的单元格,例如:

RGB(0, 128, 0)

试图在VBA中使用这个cell.value来为我的单元格着色,但是我的颜色无法识别。 它显示为string,它不工作

 "RGB(0, 128, 0)" 

如何转换这个文本或删除“”所以我的颜色function将正常工作?

 While Not Worksheets("Sheet3").Range("A" & j).Value = "" color = Sheets("Sheet3").Cells(j, 2).Value Sheets("Sheet3").Cells(j, 4).Interior.color = color j = j + 1 Wend 

您不能将string解释为代码variables/函数。

换句话说,你不能期望用string来写这个:

 str = "a = a + 2" 

…并期望您的代码读取它:

 a = a + 2 

然而,你可以做的是从string中分割出三个数字,并在代码中使用它们:

 color = Sheets("Sheet3").Cells(j, 2).Value Sheets("Sheet3").Cells(j,4).Interior.Color = RGB(Replace(Split(color,",")(0),"RGB(",""), Split(color,",")(1), Replace(Split(color,",")(2),")","") 

如果你总是有相同的模式(比如写在单元格中的RGB(0,10,20) ),你可以考虑创build一个返回三个数字的自定义函数:

 Private Function splitRGB(ByVal vl As String) As Integer() splitRGB = yourArrayWithTheThreeValues '(split as above) End Function