简体   繁体   中英

Change cell color based on another cell value

In a workbook I have, the D column has a formula in it to derive the last six digits of a value in column C. These columns are located in a sheet titled "JE". I have a dynamic SQL connected query that has values in the A column. That query is located in a sheet titled "required_refs". I essentially, want to write: If the value in the D column cell matches/equals any of the values in that query in sheet "required_refs", turn the F column cell red in sheet JE.

Example: If cell D10 has a value that equals any of the values in column A in "required_refs", turn cell F10 red. In addition, if cell D13 has a value that matches/equals a value in column A in sheet "required_refs", turn F13 red. And so on.

Here is the code I tried. I added it in Sheet "JE": Code:

  Sub ChangeCellColor()

    Dim ref_code As Range: Set ref_code = Range("D7:D446").Value
    Dim refCode_Confirm As Range: Set refCode_Confirm = Worksheets("required_refs").Range("A:A").Value
    Dim colorChange As Range: Set colorChange = Worksheets("required_refs").Range("A:A")

    For Each cell In ref_code
        If cell.Value = refCode_Confirm.Value Then
            Range("F7:F446").ActiveCell.Interior.ColorIndex = 3
            Next cell
        End If
    End Sub

Currently, this code just doesn't do anything. It doesn't turn the F column cell red. I've asked a question similar to this but, the workbook I'm using has changed a bunch since then, and this question is a bit more simple than the previous one. If anyone could help, I'd really appreciate it. Thanks!

Your code has a number of issues.

  1. .Value returns a basic type, like a string or long. You can't assign this to a range variable.
  2. Your End If and Next cell statements are swapped around. Always use correct indentation so these errors become more obvious.
  3. You have an undeclared variable cell . This can potentially cause bugs. In the VBE, turn on the Tools > Options > Editor > Required Variable Declaration option to force the use of Option Explicit in new modules.

Fixing these issues leads us to this:

Sub ChangeCellColor()

  Dim cell As Range
  Dim ref_code As Range: Set ref_code = Range("D7:D446")
  Dim refCode_Confirm As Range: Set refCode_Confirm = Worksheets("required_refs").Range("A:A")
  Dim colorChange As Range: Set colorChange = Worksheets("required_refs").Range("A:A")

  For Each cell In ref_code
    If cell.Value = refCode_Confirm.Value Then
      Range("F7:F446").ActiveCell.Interior.ColorIndex = 3
    End If
  Next cell

End Sub

Unfortunately, it still doesn't work as you can't compare a single value directly against a column of values in VBA.

This following code corrects this remaining issue. Note the choosing of good meaningful names as well as the use of RVBA for the variables. This is a good tip for how to avoid making similar errors. Also note the use of .Value2 instead of .Value . This is highly recommended.

Sub ChangeCellColor()

  Dim rngRef As Range
  Dim rngRefsToCheck As Range: Set rngRefsToCheck = Range("D7:D446")
  Dim rngRequiredRefs As Range: Set rngRequiredRefs = Worksheets("required_refs").Columns("A")
  Dim rngColorChangeRequired As Range: Set rngColorChangeRequired = Columns("F")

  For Each rngRef In rngRefsToCheck
    If Not IsError(Application.Match(rngRef.Value2, rngRequiredRefs, 0)) Then
      rngColorChangeRequired.Cells(rngRef.Row).Interior.ColorIndex = 3
    End If
  Next rngRef

End Sub

The best and fastest way to achieve the color change would be to use Advanced Filters, thus avoiding the need to loop. However, since you're still learning the basics, I've shown the looping version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM