highlight blank cells - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi the code below (not mine thanks for your help) is to highlight blank cells (no cells has spacebar). It works perfectly but is the Selection has no empty cells or cells with spacebar then i get run-time "no cells we found" and the program terminate. Can that code modified so if the Selection has no empty cells then a message display (msgbox no empty cells found).
Thank you so much

Sub blank()
Dim x As Range
Set x = Selection
x.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
How about
Code:
Sub noblank()
On Error GoTo NoBlanks
Selection.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
On Error GoTo 0
Exit Sub
NoBlanks:
MsgBox "No Blanks"
End Sub
 
Upvote 0
Thank you so much. It works perfectly. Just one more question I have. This code, if I do not select (highlight anything) and the sheet is empty one then this code will make range A1:G47 red?! why is that
 
Upvote 0
I get the "No blanks" message.
Save the file & try again, does it still fill the range?
 
Upvote 0
Thanks for your reply. I create a new sheet and it works fine when nothing in the sheet. The I did the following

filled the range A1:C25 with random number, using he following code
++++++++++++++
Sub newfill()
Dim i As Integer
Dim j As Integer
For j = 1 To 3
For i = 1 To 25
Workbooks(1).ActiveSheet.Cells(i, j).Value = WorksheetFunction.RandBetween(1, 1000)
Next i
Next j
End Sub
+++++++++++
Then I cleared all the sheet using this code

Sub clearcontents2()
Workbooks(1).ActiveSheet.Cells.clear
End Sub

++++++

then when I tried to use your code, it will highlight the range A1:C25 only.

Thank you
 
Upvote 0
If you've cleared the cell they are blank, which is why the code highlights them
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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