首页 > 解决方案 > 在工作表 B 中存在的 B 列中的工作表 A 中的 A 列中查找值并更改颜色

问题描述

我正在尝试做的事情有多个步骤,请耐心等待。

我有两张纸 A 和 B

表 A

                    Jan     Feb     March   Apr Jun Jul Aug Sep Oct Nov Dec         
Names   SubNames                
Apple   Apple1      30.00                    -   -   -   -   -   -   -   -
        Apple2      45.00   10               -   -   -   -   -   -   -   -
        Apple3                               -   -   -   -   -   -   -   -
        Apple4                               -   -   -   -   -   -   -   -
        SubTotal    75.00   0.00    10.00    
Banana  Banana11                             -   -   -   -   -   -   -   -
        Banana22    15.00                    -   -   -   -   -   -   -   -
        Banana33    32.00   17.75   65.00    -   -   -   -   -   -   -   -
        SubTotal    37.00   32.75   34.50   
Cherry  1Cherry1    28.00   13.78   43.00    -   -   -   -   -   -   -   -
        2Cherry2            20.00       
        3Cherry3    20.00   16.50   100.00   -   -   -   -   -   -   -   -  
        Subtotal    48.00   50.28   143.00   -   -   -   -   -   -   -   -
Dragon  DragonF1    31.00   35.00            -   -   -   -   -   -   -   -
        Subtotal    31.00   35.00   0.00    
        Grand Total 191.00  118.03  187.50  
                            496.53          

表 B

                    Jan     Feb     March   Apr Jun Jul Aug Sep Oct Nov Dec     
Names   SubNames            
Apple   Apple1      30.00                    -   -   -   -   -   -   -   -
        Apple2      60.00                    -   -   -   -   -   -   -   -
        Apple3                               -   -   -   -   -   -   -   -
        Apple4                               -   -   -   -   -   -   -   -
        SubTotal    90.00   0.00    0.00
Banana  Banana11                             -   -   -   -   -   -   -   -
        Banana22                             -   -   -   -   -   -   -   -
        Banana33    37.00   17.75   34.50    -   -   -   -   -   -   -   -
        SubTotal    37.00   17.75   34.50
Cherry  1Cherry1    28.00   13.78   63.15    -   -   -   -   -   -   -   -
        2Cherry2            15.35            -   -   -   -   -   -   -   -
        3Cherry3    35.00   16.50   97.65    -   -   -   -   -   -   -   -
        Subtotal    63.00   45.63   160.80
Extra   Extra1234   30.00                    -   -   -   -   -   -   -   -
        Extra4321                            -   -   -   -   -   -   -   -
        Subtotal    30.00   0.00    0.00
Dragon  DragonF1    31.00   34.50            -   -   -   -   -   -   -   -
        Subtotal    31.00   34.50   0.00     -   -   -   -   -   -   -   -
     Grand Total    251.00  97.88   195.30
                            544.18      

有从 A - O 的列

我需要检查工作表 B 列 B 中的值是否与工作表 A 和 B 列中的值匹配

如果是这样,我需要比较 Jan、Feb 和 March 下的值,如下所示:

如果值从工作表 A 增加到 B,则将工作表 B 中的单元格颜色更改为绿色 如果值从工作表 A 减小到 B,则将工作表 B 中的单元格颜色更改为减少

可以忽略小计和总计字段。

我不断收到错误,但我不确定我的代码是否完整。

Public Sub Edit()
    Dim myRange As Range
    Dim iCell As Range
    Dim LastRow As Integer, Row As Integer, Col As Integer
    Dim CSheet As Integer, PSheet As Integer
    CSheet = ActiveSheet.Index
    PSheet = CSheet - 1

    Set cs = ActiveSheet
    Set ps = Worksheets(PSheet)

    'selecting range of active region
    Set myRange = Range(ActiveSheet.Range("A3:O3"), ActiveSheet.Range("A3:O3").End(xlDown))

    LastRow = Cells(Rows.Count, 15).End(xlUp).Row

    'Highlighting all rows with no entries for forecast
    For Row = 3 To LastRow
        If WorksheetFunction.CountBlank(Range("C" & Row & ":O" & Row)) = "13" Then
            Range("C" & Row & ":O" & Row).Style = "Note"
        End If

        'Highlighting green and red
        If cs.VLookup(Range("B" & Row).Value, ps.Range("B2:B100"), 0) Then
            For Col = 3 To 15
                If cs.Cells(Row, Col) >= ps.Cells(Row & Col) Then
                    cs.Cells(Row, Col).Style = "Good"
                Else
                    cs.Cells(Row, Col).Style = "Bad"
                End If
            Next Col

        End If

    Next Row
End Sub

标签: excelvba

解决方案


如果我没看错你的问题,你想根据一些可以从公式中检索到的信息来修改单元格的格式。

有两种不同的情况:

  1. 您希望仅根据用户交互(例如单击按钮)对该单元格进行格式化。在这种情况下,可以选择 VBA。
  2. 您希望该单元格的格式自动发生。在这种情况下,最好选择条件格式。

如果您选择条件格式,您可能需要一个基于以下内容的公式:

=VLookup(A!B3;B!$B$2:$B$100;2) // this will give the value for January for Apple1 in sheet B.
                               // In case "Apple1" is not there, you get a #N/A error.
=ISNA(...)                     // You might use this for catching #N/A errors.
...

所以,基本上,你最终会得到一个这样的公式:

=ISNA(VLOOKUP(...);0) - ISNA(VLOOKUP(...);0)>0

推荐阅读