【发布时间】: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" & x), 1, False),你在一个大小为1的范围内搜索cellValue? -
工作表有多大,即
lngLastRowNon是什么?一般来说,我建议使用 Scripting.Dictionary 从工作表中读取值,然后在那里进行查找,而不是使用 VLOOKUP 函数。 -
两个列范围(从 Tab2 和 Tab3)中最大的现在将是大约 90K 行,大约有 1600 个目标单元格要着色。我正在使用 WorksheetFunction,因为这是我第 53 次尝试解决此问题。我会查看字典,看看我的位置。