Option Explicit
'As you can see function can be modified with different optional values (only logicals,
'only errors, only constants, only formulas, etc...)
Function CoolRange(MyRange As Range, Optional NoNumbers As Boolean, Optional NoTexts As Boolean) As Range
Dim RgConstants As Range, RgFormulas As Range 'Ranges to contain cells with constants and formulas
Dim MyOption As Integer 'string to consider optional values
If NoNumbers = False Then MyOption = 1 'including cells that contain numbers
If NoTexts = False Then MyOption = MyOption + 2 'including cells that contain text
MyOption = MyOption + 20 'get the value for XlSpecialCellsValue, argument of SpecialCells
'regarding the optional values
On Error Resume Next 'in case there are no constants or formulas in the range
Set RgConstants = MyRange.SpecialCells(xlCellTypeConstants, MyOption) 'get constants cells
Set RgFormulas = MyRange.SpecialCells(xlCellTypeFormulas, MyOption) 'get formulas cells
On Error Goto 0
Select Case True
Case RgConstants Is Nothing And RgFormulas Is Nothing: Exit Function 'range is empty
Case RgConstants Is Nothing: Set CoolRange = RgFormulas 'no constants found
Case RgFormulas Is Nothing: Set CoolRange = RgConstants 'no formulas found
Case Else: Set CoolRange = Union(RgConstants, RgFormulas) 'GET THE CELLS NOT BLANKS
End Select
'clear
Set RgConstants = Nothing
Set RgFormulas = Nothing
End Function
|