【问题标题】:Assign cell background color to rgb from referenced cells and repeat for many or all rows将单元格背景颜色分配给引用单元格中的 rgb 并重复许多或所有行
【发布时间】:2020-05-19 12:55:02
【问题描述】:

尝试将 col D 的背景颜色设置为从单元格 A、B、C 中获取的 RGB 代码 [rgb背景厕所示例]

此公式已提供给另一个查询,并且适用于 1 行 Range("F1").Interior.Color = RGB(Range("C1"), Range("D1"), Range("E1"))

如何使多行或所有行重复

我是 Visual Basic 的新手,如果有任何帮助将不胜感激

【问题讨论】:

  • 听起来你需要一个循环?
  • 假设您想在第 1 行和第 10 行之间的 A 列的所有单元格中写入一个值。然后您将使用 BigBen 建议的循环。您将编写一个从 1 开始到 10 的循环。循环的迭代变量类似于currentRow,每次迭代都会增加 1。这是一个关于如何使用循环的简明教程:excel-easy.com/vba/loop.html

标签: excel vba


【解决方案1】:

也许是这样的?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Column > 3 Then Exit Sub
Set oFill = Range("D" & ActiveCell.Row)
Set Rng = Range("A" & ActiveCell.Row, Range("C" & ActiveCell.Row))

For Each cell In Rng
If IsEmpty(cell) = False And IsNumeric(cell) = True Then n = n + 1
If n = 1 Then x1 = cell.Value
If n = 2 Then x2 = cell.Value
If n = 3 Then x3 = cell.Value
Next cell

If n = 3 Then
oFill.Interior.Color = RGB(x1, x2, x3)
Else
oFill.Interior.Pattern = xlNone
End If

End Sub

代码将放在工作表模块中。
如果同一行中的所有三列(A B C)都有值,并且用户在填充同一行中的三个单元格(A B C)后选择了A到C列中的任何单元格,则会发生颜色填充。

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 2015-10-21
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多