0

I am hoping someone can help. I am comparing prices of products between a master sheet and a local sheet. I need to flag when the prices don't match. The master sheet contains all possible products but the local sheets do not, so first I need to match the products based on their product code and then based on that result, match the prices. VLookup didn't quite get me where I wanted to go, although I am open to suggestion, so I have tried my hand at the following code:

Sub match_price()

Dim ws1 As Worksheet
Dim ws2 As Worksheet

Set ws1 = ActiveSheet
Set ws2 = Worksheets("master")

For Each i In ws1.Range("A2:A100")
    For Each C In ws2.Range("A2:A75")
        If i.Cells.Value = C.Cells.Value Then
        ws1.Range("C2:C10").Select
      End If
     Next C
    Next i
    For Each i In ws1.Range("C2:C100")
        For Each C In ws2.Range("C2:C75")
        If i.Cells.Value < C.Cells.Value Then
         i.Cells.Interior.ColorIndex = 3
        End If
    Next C
    Next i
End Sub

However, my solution is not recognizing the differences, I suspect this is because it is not fully picking up that the differences should only be based on matched products. Any help is appreciated, thanks in advance.

EDIT

SAMPLE DATA

Master Sheet

ID          Descrip     Invoice
14562738    A   119
25364058    B   245
26584024    C   375
67489542    D   19

Local Sheet
ID          Descrip Invoice
14562738    A   115
25364058    B   240
67489542    D   19

Edit 2: My resolution:

In case anyone is interested, I recorded a macro with my Vlookup code and added user2140261 conditional formatting for the flagging. I only wanted to highlight the text so I removed the coloring of the cell itself. I should mention that I need to use VBA as there are other types of calculations that I need to do across many cells and sheets, but user2140261 highlighting solution helped me take Vlookup to where I need to be. Thank you for your help!

Sub Macro()
'
'I always select D2 as the default active cell.
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],master,3,FALSE)"
    Selection.AutoFill Destination:=Range("D2:D10"), Type:=xlFillDefault
    Range("C2:C10").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=D2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
4

1 回答 1

0
=VLOOKUP(A2,Master!A2:C5,3,FALSE)

根据您的样本调整范围以适应。在本地工作表的 D 列中输入此内容并向下拖动。然后突出使用条件格式,其中 C 中的值 < D 中的值

以下是您的示例数据的副本。在本地工作表上。

样本

在图片中,您可以看到 VLOOKUP 从匹配的行中返回所有值。

要突出显示这些值,您需要使用条件格式:

样品2

请注意,在屏幕截图中,您必须选择要突出显示的所有数据。然后在公式框中输入

=D2

确保如果您尝试单击单元格 D2 并且公式变为=$D$2Then you MUST remove the Doller Signs ( $)

您的最终数据将如下所示:

样品3

两者都是红色的115240因为它们小于从主表中获取的值。并不是19因为它只等于主表中的值

从您的编辑到您的问题,我认为您可以稍微缩短宏,这也会加快速度,您应该尽可能避免使用 select 功能。尽管 VBA 中存在一个奇怪的错误,即无论您如何在 VBA 中输入单元格引用,它始终被视为行列引用,而不是实际引用。尽管如此,这应该会稍微清理您的示例,并且如果将来您在更大范围内使用它,也有助于提高速度。

Sub Macro()
    Range("D2:D10").FormulaR1C1 = "=VLOOKUP(RC[-3],master,3,FALSE)"
    With Range("C2:C10")
        .Select
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
          Formula1:="=D2"
        .FormatConditions(1).Font.Color = -16383844
    End With
End Sub
于 2013-05-20T21:30:48.103 回答