Be it for troubleshooting formula errors or data integrity, you might find yourself trying to identify those cells going through the entire workbook. In this article we’re going to show to find in Excel if cell is blank using VBA and automate this process.
How to find in Excel if cell is blank using VBA
VBA has some predefined types for the cells that contains special values. These can be accessed with the SpecialCells method. The SpecialCells method uses two arguments to specify the cells; Type and Value. Using a combination of these two arguments you can easily access the cell or range type you desired. Below is a list of values both arguments can get.
- xlCellTypeAllFormatConditions. Cells of any format
- xlCellTypeAllValidation. Cells having validation criteria
- xlCellTypeBlanks. Empty cells
- xlCellTypeComments. Cells containing comments
- xlCellTypeConstants. Cells containing constants
- xlCellTypeFormulas. Cells containing formulas
- xlCellTypeLastCell. The last cell in the selected range. Note that this will include empty cells that have had any cells that changed from their default format.
- xlCellTypeSameFormatConditions. Cells having the same format
- xlCellTypeSameValidation. Cells having the same validation criteria
- xlCellTypeVisible. All visible cells
To find the blank cells using VBA, using xlCellTypeBlanks constant for the type argument is enough. The value argument can be omitted.
An important point in here is that the Selection.SpecialCells(xlCellTypeBlanks).Cells returns an array of cells. So, before returning any information, we need to step through each cell in the array. A For Each…Loop is perfect for arrays consisting of objects. We designed our code to return the address of all blank cells in a Message Box.
To be able to run codes, you need to add a module into the workbook or the add-in file. Copy and paste the code into the module to run it. The main advantage of the module method is that it allows saving the code in the file, so that it can be used again later. Furthermore, the subroutines in modules can be used by icons in the menu ribbons or keyboard shortcuts. Remember to save your file in either XLSM or XLAM format to save your VBA code.
Find blank cells
Sub FindBlankCells() Dim rng As Range, message As String For Each rng In Selection.SpecialCells(xlCellTypeBlanks).Cells message = message & Chr(10) & rng.Address 'Chr(10) is for new line Next MsgBox message, vbOKOnly, "Empty Cells" 'vbOKOnly is the type of the message box End Sub