【问题标题】:Set Excel Fill Based on a (Variable) Cell基于(变量)单元格设置 Excel 填充
【发布时间】:2014-07-01 03:53:27
【问题描述】:

我现在的主要问题是我想根据单元格 I5-35 中的内容填充一系列 Excel 单元格(比如说 K5 到 K35)。此数字对应于 RGB 数字(共 255 个)。

我刚刚进入 VBA,因此我将不胜感激任何输入。这是 excel 向我抛出 424 - Object required 错误的代码。

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 循环),它能够完美地做到这一点,但我无法让它在列表中出现。

非常感谢您的帮助!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您好,这是实现您正在做的事情的快速解决方案。以下是我想出的:

    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 设置为范围。

    【讨论】:

      【解决方案2】:

      你很接近。 Excel 告诉您它没有运行 .Interior.Color 的对象。您只给它文本,而不是列引用。如果您在循环中为第一次运行转换代码,它将如下所示:

      K5.Interior.Color = RGB(255, colorVar, colorVar)
      

      你想要的样子是

      Range("K5").Interior.Color = RGB(255, colorVar, colorVar)
      

      为此,您需要按如下方式更改该行代码:

      Range(cellActive).Interior.Color = RGB(255, colorVar, colorVar)
      

      【讨论】:

        猜你喜欢
        • 2014-10-21
        • 2015-10-09
        • 2019-10-19
        • 1970-01-01
        • 2017-02-12
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多