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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,214,780
Messages
6,121,525
Members
449,037
Latest member
tmmotairi

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