You are here:

Excel/Using a macro to highlight a range


Hi Tom

I am writing a macro, and part of it needs to highlight a range of empty cells in column P, so that these rows can then be grouped.  The data in column P will always begin in cell P6, and can end anywhere between P8 and P35.  A new set of data begins in cell P36, and I need to highlight any empty cells inbetween P6 and P36.   This empty range is not fixed, and will probably be different each time I run the macro.  However, the last cell of the empty range will always be P35.  I have tried various combinations of shift, end, down, but cant seem to get the correct range highlighted.

Do you know the code I should be using?




Dim r as Range
Set r = Nothing
On Error Resume Next
set r = Range("P6:P35").SpecialCells(xlBlanks)
On Error goto 0
if not r is nothing then
 r.interior.ColorIndex = 3
end if

that would make the background of the empty cells red as an example.  (the cells must actually be empty)

does that work for you?

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.