Isempty, Nothing, Null...for optional range object?

MikeAshton55Mad

New Member
Joined
Nov 4, 2004
Messages
49
I have a function where I am passing an optional Range object and trying to check whether or not the function is being passed a range of data.

I've tried cells.count, but if the Range object doesn't exist Excel doesn't like that; I've tried If TheRange=Nothing. I've tried ISEMPTY(TheRange). Nothing seems to work, and I can't find anything that is designed for ranges. What's the trick?

Thanks,

Mike
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Mike.

Drop these two routines in an empty worksheet and try them out.

Code:
'This sub checks if the range has been set.
Sub RangeSet()

Dim TheRange As Range

If Not TheRange Is Nothing Then
Else
    MsgBox "Before Setting the Range"
    Set TheRange = Range("A1")
End If
    
If Not TheRange Is Nothing Then
    MsgBox "After the range is set.  TheRange = " & TheRange.Address
End If

End Sub

'This sub checks to see if an existing range contains any data.
Sub RangeEmpty()

Dim TheRangeNonEmpty As Range, TheRangeEmpty As Range
Dim loop_break As Boolean
Dim rw As Long, col As Integer, rwCtr As Long, colCtr As Integer

wsName = ActiveSheet.Name

Worksheets(wsName).Cells(1, 1) = "Not Empty"

Set TheRangeNonEmpty = Range("A1:A2")
Set TheRangeEmpty = Range("b1:b2")

loop_break = False
rw = TheRangeEmpty.Rows.Count
col = TheRangeEmpty.Columns.Count

rwCtr = 0
colCtr = 0

Do
    rwCtr = rwCtr + 1
    Do
        colCtr = colCtr + 1
        If TheRangeEmpty.Cells(rwCtr, colCtr) <> "" Then
            loop_break = True
        End If
    Loop While loop_break = False And colCtr < col
Loop While loop_break = False And rwCtr < rw

TheRangeEmpty.Activate

If rwCtr = rw And colCtr = col Then
    MsgBox "TheRangeEmpty is empty"
Else
    MsgBox "TheRangeEmpty is non-empty"
End If

rwCtr = 0
colCtr = 0
Do
    rwCtr = rwCtr + 1
    Do
        colCtr = colCtr + 1
        If TheRangeNonEmpty.Cells(rwCtr, colCtr) <> "" Then
            loop_break = True
        End If
    Loop While loop_break = False And colCtr < col
Loop While loop_break = False And rwCtr < rw

TheRangeNonEmpty.Activate

If rwCtr = rw And colCtr = col Then
    MsgBox "TheRangeNonEmpty is empty"
Else
    MsgBox "TheRangeNonEmpty is non-empty"
End If

End Sub

Hope that helps. Ben.
 
Upvote 0
Sweet - Thanks Ben (sweater vests rock?). It was something as simple as saying ThisRange is Nothing rather than If ThisRange=Nothing. Never would have figured that out without your help, and I learned a few other things besides. Thanks!

Mike
 
Upvote 0

Forum statistics

Threads
1,202,911
Messages
6,052,517
Members
444,588
Latest member
ViJN

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