highlight empty cells - handling error - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
The code below will highlight empty cells in a Selection. but if I do not have an empty cell then it would give me an error message. Could you please tell me how you would handle that error message? Thank you very much.
Code:
Sub highlight_empty_cells()
    Selection.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 3
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
In that case

VBA Code:
Sub highlight_empty_cells()
  On Error Resume Next
    Selection.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 3
End Sub

Or
VBA Code:
Sub highlight_empty_cells()
  On Error Resume Next
  Selection.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 3
  If Err.Number = 1004 Then
    MsgBox "There are no empty cells"
  End If
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,588
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top