VBA code to check if a cell is blank

Home/VBA/VBA code to check if a cell is blank

We can use VBA to check if a cell is blank and run the remaining statements. Sometimes, we need to determine and make sure a particular range is not empty before proceeding to the next statements. This example will help you to know how to check if a cell or a range is blank or not using Excel VBA. This can be used in Excel 2003,2007,2010,2013.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


VBA code to check if a cell is blank – Syntax

Here is the example Excel VBA Syntax to check if a cell is blank or not. The below macro will check whether a Range A1 is blank or not.

If Cells(1, 1) = "" Then
... Statements to execute if the Cell is Empty 
Else
...statements to execute if Cell is not Blank.
end if

VBA code to check if a cell is blank – Example

Here is the example macro to check if a cell is blank or not. The below macro will show a message box based on the Cell.

Sub vba_code_to_check_if_a_cell_is_blank()
If Cells(1, 1) = "" Then
MsgBox "Cell is Blank"
Else
MsgBox "Cell is not blank"
End If
End Sub

VBA code to check if a cell is blank – Better Example: Avoid Empty Spaces

Sometimes, A cell or rang looks blank and we notice that the condition is failing. It is because of the empty spaces in the cell. So, whenever you want to check if a cell is empty, you can trim the Cell value and check it.

Sub vba_code_to_check_if_a_cell_is_blank()
'use trim to avoide blank spaces
If Trim(Cells(1, 1)) = "" Then
MsgBox "Cell is Blank"
Else
MsgBox "Cell is not blank"
End If
End Sub

VBA code to check if a cell is blank – Instructions

Please follow the below step by step instructions to test this Example VBA Macro codes:

  • Step 1: Open a New Excel workbook
  • Step 2: Press Alt+F11 – This will open the VBA Editor (alternatively, you can open it from Developer Tab in Excel Ribbon)
  • Step 3: Insert a code module from then insert menu of the VBE
  • Step 4: Copy the above code and paste in the code module which have inserted in the above step
  • Step 5: Enter some sample data at range A1 to check if the cell is blank or not using VBA.
  • Step 6: Now press F5 to execute the code or F8 to debug the Macro to check the if Range A1 is blank or not
LIMITED TIME OFFER
By |January 20th, 2015|VBA|1 Comment

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

One Comment

  1. Jorge Cabral March 12, 2017 at 6:01 AM - Reply

    What about if the cell as a formula, but the result is “” or zero?

Leave A Comment


Related pages


vbscript programs examplesddl dml dqlvba singlebeginners excel tutorialsample programs in vbmultiple project timeline template excelaccess 2010 vba recordsetexcel functions in vbavba multiplicationexcel project management templatesvba sort columnreturn value vbaestimation templatesdeleting a pivot tableexcel vba exitlistbox vba accessexcel 2007 calendar templatevba in excel 2007vba select a worksheetsum vlookuplookup formulavba usedrangehlookup function excelcountif functionsendmail attachment exampleunprotect workbook excel 2007unhide a column in excelclear listboxhow to insert checkbox in excel 2007delete blank rows from excelexcel macro write to cellexcel macros how tovlookup funtionexcel formula list with examples 2007vba sort listboxredim vbavba save worksheetpivot table setupuse of vlookup function in excelexcel vba active workbookreplace formula in excelvba dashboardoffset function in vbasimple vba code examplesnot iserrorexcel vba msgbox variableinserting check boxes in excelexcel sheet shortcutshow to write if statement in excelexcel combo boxvba excel formatprotect formulas in excel 2007excel 2007 dashboardsconcatenation in vbasas interview questions on macrosdashboard report excelinsert checkbox in excelvba containsvba sub procedureweekly gantt chart templateexcel vba sort arrayfresher technical interview questions and answerscomboboxeswhat is vlookup excelexcel remove duplicate cellsexcel macro databasems office 2007 interview questions answers pdfhow to develop macros in excelvba worksheetfunction vlookupsql pivot tutorialcheckbox excelexcel hyperlink to filearrays in vbaeasy vlookup example