【问题标题】:Conditional colorization of a given cell using VBA, based on multiple criteria from other cells/worksheets基于来自其他单元格/工作表的多个标准,使用 VBA 对给定单元格进行条件着色
【发布时间】:2019-05-23 15:26:01
【问题描述】:

我正在尝试有条件地为 Tab1 上的一组单元格着色。我正在使用 Tab2 上的列和 Tab3 上的列中的数据,以尝试匹配给定的单元格变量。

基本逻辑是: 如果与 Tab2 的 B 列中的单元格匹配,则检查 Tab2 上同一行的 E 列中的值。

如果 E 列中的值大于 Tab2 中的零,则在颜色上对 Tab1 上搜索范围中的初始单元格值进行着色...但如果我也存在于 Tab3 上,则为其他颜色着色。

复制并粘贴部分代码。这是代码的“非工作”版本。如果它确实运行,它需要永远运行。

For Each cellValue In mainRng2

‘if I do not exist in SerializedInvtLocations, but do exist in NonSerializedInventory then check the value in cell E is greater than zero. 
   If VBA.IsError(Application.match(cellValue, Sheets("SerializedInvtLocations").Range("A2:A" & lngLastRowSer), 0)) And Not VBA.IsError(Application.match(cellValue, Sheets("NonSerializedInventory").Range("B2:B" & lngLastRowNon), 0)) Then


    For Each cell In Sheets("NonSerializedInventory").Range("B2:B" & lngLastRowNon)

        x = x + 1
         checker = Application.WorksheetFunction.VLookup(cellValue, Range("B" & x), 1, False)

         'if the vlookup value in B2
         If (checker = cellValue) Then
        'i exist in non-serialized list, do I have a quant > 0?

            quant = Application.WorksheetFunction.VLookup(cellValue, Range("E" & x), 1, False)

            If quant >= 1 Then
                     cellValue.Interior.ColorIndex = 8 'teal
                     ‘Sheets("Serialized and Non-Serialized").Range(cell.Address).Interior.Color = RGB(0, 255, 0)
                   ‘   Debug.Print "Checker value is: " & checker & " and " & cell.Address & "/" & cell.Value

                   i3 = i3 + 1 ‘ counter

            Else
                       cellValue.Interior.ColorIndex = 15 'gray

           End If
          End If
    Next cell
   End If

Next cellValue

目前,该文件只是挂起并且没有产生结果(或者它需要超过 40 分钟才能运行,我只是退出了)。如果我修改代码并进行更改 - 我可以获得结果,但它们并不准确。

编辑: 另一个尝试:

If inSer = cellValue.Value And inNon = cellValue.Value Then
    If inNonQuan >= 1 Then
    cellValue.Interior.ColorIndex = 46
    Else
    cellValue.Interior.ColorIndex = 4
    End If
End If
If inSer <> cellValue.Value And inNon = cellValue.Value Then
    If inNonQuan >= 1 Then
    cellValue.Interior.ColorIndex = 8
    Else
    cellValue.Interior.ColorIndex = 15
    End If
 End If
If inSer = cellValue.Value And inNon <> cellValue.Value Then
    cellValue.Interior.ColorIndex = 4
End If
If inSer <> cellValue.Value And inNon <> cellValue.Value Then
    cellValue.Interior.ColorIndex = 15
End If

【问题讨论】:

  • 您为什么使用 VBA 而不仅仅是使用内置的条件格式?
  • 有不同数量的动态位置需要着色。除了着色之外,还使用其他 VBA 元素“钻入”着色单元格以从中提取更多信息。所以该项目是在 VBA 中启动的,并希望继续使用 VBA。
  • 你为什么用Application.WorksheetFunction.VLookup(cellValue, Range("B" &amp; x), 1, False),你在一个大小为1的范围内搜索cellValue
  • 工作表有多大,即lngLastRowNon是什么?一般来说,我建议使用 Scripting.Dictionary 从工作表中读取值,然后在那里进行查找,而不是使用 VLOOKUP 函数。
  • 两个列范围(从 Tab2 和 Tab3)中最大的现在将是大约 90K 行,大约有 1600 个目标单元格要着色。我正在使用 WorksheetFunction,因为这是我第 53 次尝试解决此问题。我会查看字典,看看我的位置。

标签: excel vba


【解决方案1】:

你应该可以用这个做点什么:

Sub Tester()

    Dim c As Range, mainRng2 As Range, t2q As Variant, t3m As Boolean, Tab2, Tab3, wb

    Set wb = ActiveWorkbook 'or ThisWorkbook ?
    Set Tab2 = wb.Worksheets("Tab2")
    Set Tab3 = wb.Worksheets("Tab3")

    Set mainRng2 = wb.Worksheets("Tab1").Range("A2:A1000") 'for example

    For Each c In mainRng2

        'quantity on Tab2 from colE, based on ColB match
        '  will be an error value if no match found
        t2q = Application.VLookup(c.Value, Tab2.Range("B:E"), 4, False)

        'any match on Tab3 ColA ?
        t3m = Not IsError(Application.Match(c.Value, Tab3.Range("A:A"), 0))

        'did we get a quantity from Tab2 (was there any match)?
        If Not IsError(t2q) Then
            If t2q >= 1 Then
                '15 if also a match on tab3, else 8
                c.Interior.ColorIndex = IIf(t3m, 15, 8)
            End If
        End If

    Next c

End Sub

【讨论】:

  • 谢谢蒂姆。与我期望看到的结果相比,我尝试实施您的解决方案并且实际上得到了相同的“无效”结果。事情仍然是错误的颜色,并且显示不准确。我已经更新了我最初的帖子,以包含另一个打破逻辑的尝试。
  • 这是我的最佳猜测,如果没有您的任何实际数据,很难说出问题所在。您需要调试并准确确定它在哪里不起作用。
【解决方案2】:

我的解决方案非常简单——VLookup 只返回匹配值的第一个实例,而不是所有后续值。而不是 vlookup,我应该基本上“总结”一列的值以获得大于零的值。

【讨论】:

    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2021-07-26
    相关资源
    最近更新 更多