在工作表B的工作表B的工作表A的A列中寻找值并更改颜色

时间:2019-04-10 18:23:41

标签: excel vba

我要做的事情有多个步骤。

我有两张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中的值

如果这样做,我需要按以下方式比较一月,二月和三月下的值:

如果值从工作表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

1 个答案:

答案 0 :(得分:0)

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

有两种不同的情况:

  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