We help IT Professionals succeed at work.
Get Started

Excel - VBA Highlight Cells

Last Modified: 2016-02-10
I have an excel spreadsheet that will highlight the cells from B:N that's empty if column A is populated.  I would like it to un-highlight any cells in that row if it's populated as well as clear all highlighted once A is empty.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim i, l, r As Range, cnt

cnt = 0

i = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
  For l = 1 To i
    If ThisWorkbook.Sheets(1).Range("A" & 1).Value <> "" Then
      Set r = ThisWorkbook.Sheets(1).Range("B" & 1 & ":N" & 1)
        For Each Cell In r
          If IsEmpty(Cell) Then
            r.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 6
            cnt = cnt + 1
            Cancel = True
            Exit For
            r.Interior.ColorIndex = xlNone
        End If
    End If
  Next l

If cnt > 0 Then
    MsgBox ("There are required cells that needs to be filled.")
End If
End Sub

Open in new window

The above is working, but the un-highlight isn't working correctly.  I have a header and it clear that.  It does not clear any other rows afterward.  Am I missing something?
Watch Question
Data Analyst
This problem has been solved!
Unlock 2 Answers and 11 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE