【问题标题】:Use Function on range在范围内使用函数
【发布时间】:2016-07-26 11:50:42
【问题描述】:

在我之前的问题之后: Background Color based on difference with cell .我现在想将此函数应用于一系列行。

我要应用的功能是:

If Sheets("X").Range("E18") > Sheets("blocked(R)").Range("D18") Then
    Sheets("X").Range("E18").Interior.ColorIndex = 10

我需要应用这个函数的范围是固定的:D18:E1200。 但是,在这个范围内会有一个主动过滤器。

Autofill 代码当然不起作用,为 1200 行中的每一行编写一行代码会很疯狂。

我一直在搜索和阅读,我认为它一定是这样的:

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("A1:C2")

For Each row In rng.Rows
  For Each cell in row.Cells
    'Do Something
  Next cell
Next row

但我似乎没有让它发挥作用,我希望有人能指出社区的正确方向。

【问题讨论】:

  • Conditional Formatting 使用本机工作表公式是更好的解决方案。
  • @Jeeped 因为将插入行和列。有条件的不是选择。每次添加后,我都需要能够在此特定范围内运行格式化代码。

标签: vba excel loops


【解决方案1】:
Sub test()

    Dim rngApply As Range
    Set rngApply = Sheets("X").Range("D18:E1200")
    Dim varIndex As Variant

    For Each varIndex In rngApply
        If varIndex.Value > Sheets("blocked(R)").Range("D18") Then
            varIndex.Interior.ColorIndex = 10
        End If
    Next

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2016-08-27
    • 2014-07-07
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    相关资源
    最近更新 更多