SpecialCells() what is used for?

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi I am trying to understand the code below which highlight blank cells. 2 parts I did not understand

1) what does this line do " Set Dataset = Selection "?

2) what SpecialCells() function does here --> Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed

Thank you so much.

source: https://trumpexcel.com/excel-macro-examples/
'This code will highlight all the blank cells in the dataset
Sub HighlightBlankCells()
[FONT=&quot][/FONT][FONT=&quot][/FONT]Dim Dataset as Range
Set Dataset = Selection
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
1) Dataset is a variable. the line
Code:
Set Dataset = Selection
sets the value of the variable, Dataset, to those cells that are currently selected.

2)The line
Code:
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
takes those cells in Dataset that are blank* and makes them red.

*- one should be cautious about "blank" cells. Cells that contain formulas that evaluate to "" are considered "not blank".
 
Last edited:
Upvote 0
1) what does this line do " Set Dataset = Selection "?
It just sets the "Dataset" range to be whatever cells are currently selected when the macro starts (so it will run on whatever range you have selected).

2) what SpecialCells() function does here --> Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
Special cells are a set of special cell commands. This is simply selecting all blank cells in your selected range and making their color red.
You can see the Special Cells options if you hit F5 while on the sheet, and click the "Special" button.
 
Upvote 0
Thank you all so much for helping out to understand the code. I understood.
I just want to know, if that code can be modified to "undo" the highlight.
I can clear the selection but that is not "undo". is it doable?
thanks a lot once again.
 
Upvote 0
Just to follow up, the SpecialCells function is the VBA equivalent of the Go To dialog's Special button. Press F5 (or CTRL+G) to bring up the Go To dialog box and then click the button labeled "Special"... most of the possible options you see are available (some are not) via the SpecialCells function in your code.
 
Upvote 0
There is no undo in VBA. You could apply a different color, or no fill, but any prior fill is permanently gone unless you write a lot more code.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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