我现在的主要问题是我想根据单元格I5-35填充一系列excel单元格(假设是K5到K35)。这个数字对应于RGB数(255中的)。
我刚刚进入VBA,所以我很感激这里的任何输入。这是代码,excel扔给我一个424 -对象所需的错误。
Sub Colors()
Dim index As Integer
index = 5
While index <= 35
colorVar = Worksheets("Pivot").Cells(index, "I").Value
cellActive = "K" & index
cellActive.Interior.Color = RGB(255, colorVar, colorVar)
index = index + 1
Wend
End Sub
当我让它为一个单独的单元运行时(没有while循环),它能够完美地做到这一点,但我在使它进入列表时遇到了麻烦。
非常感谢您的帮助!
嗨,这是一个非常快速的修复实现你正在做的事情。下面是我想到的:
Sub Colors()
Dim index As Integer
index = 5
While index <= 35
colorVar = Worksheets("Pivot").Cells(index, "I").Value
Set cellActive = Range("K" & index)
cellActive.Interior.Color = RGB(255, colorVar, colorVar)
index = index + 1
Wend
End Sub
您的问题是没有将cellActive
设置为范围。
你很接近了。Excel告诉你它没有对象来运行。interior。颜色上。您只给它文本,而不是列引用。如果将循环中第一次运行的代码转换为如下所示:
K5.Interior.Color = RGB(255, colorVar, colorVar)
你想要的是
Range("K5").Interior.Color = RGB(255, colorVar, colorVar)
要做到这一点,您需要更改这行代码,如下所示:
Range(cellActive).Interior.Color = RGB(255, colorVar, colorVar)