Clear cell color if cell contain value
-
How are you? I would like to ask for your help inVBA. I have two columns; D and E. Column D contains List of Objects while Column E is used to classify each object where data is selected from a dropdown list (Blue, Red, Green). I've written a short macro to highlight any blank cells found in column E in grey color, and it worked fine for now. However, I realized that after I filled up the blank cells, the cells color still remained. This is what I would like to do: 1. Clear cell color of the blank cells in Column E as I select data from the dropdown list (any value). This is what I have so far: Private Sub Worksheet_Change(ByVal Target As Range) Set sh = ThisWorkbook.Worksheets("Audit Findings") 'I don't know how to set the target for range in column E If Target = sh.Range("E20", sh.Cells(1001, "E").End(xlUp)) Then If Target.Value <> "" Then sh.Range("E20", sh.Cells(1001, "E").End(xlUp)).Interior.ColorIndex = xlNone End If End If End Sub My problem is that I do not know how to define the target for column E (starting row is 20) as it also refers to the last cell that contains object in column D. Please help.
-
Answer:
Jay, I believe this is what you want: Private Sub Worksheet_Change(ByVal Target As Range) If target.count > 1 then exit sub If Target.row > 20 and target.column = 5 then If Target.Value <> "" Then Target.Interior.ColorIndex = xlNone End If End If End Sub An alternate approach would be to clear all the backgrounds in column E beyond row 20, then loop through and gray the empty cells Private Sub Worksheet_Change(ByVal Target As Range) Dim r as Range, cell as Range If Target.row > 20 and target.column = 5 then set r = me.Range("E20",me.cells(me.rows.count,"D").End(xlup).offset(0,1)) r.Interior.ColorIndex = xlNone for each cell in r if len(trim(cell.text)) = 0 then cell.Interior.ColorIndex = 15 'change to your gray colorindex end if Next End if End Sub
Miningco.com Visit the source
Related Q & A:
- When should an Aggregate Root contain another AR (and when should it not?Best solution by Programmers
- Does Green tea contain caffeine?Best solution by Yahoo! Answers
- How much salicylic acid does aspirin contain?Best solution by answers.yahoo.com
- Is there any protein bars that do not contain nuts?Best solution by Quora
- Is there an alternative to cigarettes that doesn't contain nicotine?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.