Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 87
  • Last Modified:

Test for a range that evaluates to nothing

Hi

I am using excel in office 2013.   The following code fails when there are no constants in the selected range.  It works fine if the range does not evaluate to nothing.

I need the following logic

If rStoreConstants  <> nothing then do something


Dim RStoreConstants As Range
Set RStoreConstants = TheWorkSheet.Range(Cells(FirstRow, 1), Cells(Lastrow, LastColumn)).SpecialCells(xlCellTypeConstants)

Open in new window

0
peispud
Asked:
peispud
  • 3
  • 3
  • 2
1 Solution
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

You will need to ignore errors (with an On Error Resume Next statement above your existing code), but you may use this:

If Not (RStoreConstants Is Nothing) Then
rem Do Something here
End If
0
 
Martin LissRetired ProgrammerCommented:
If Not rStoreConstants Is Nothing Then
0
 
peispudAuthor Commented:
I've used this code.  A pop up comes up saying "No cells were found."

If Not TheWorkSheet.Range(Cells(FirstRow, 1), Cells(Lastrow, LastColumn)).SpecialCells(xlCellTypeConstants) Is Nothing Then
        Set RStoreConstants = TheWorkSheet.Range(Cells(FirstRow, 1), Cells(Lastrow, LastColumn)).SpecialCells(xlCellTypeConstants)
End If
    

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Martin LissRetired ProgrammerCommented:
Are the cell values in the range the result of formulas?
0
 
peispudAuthor Commented:
mixed.   There are always formulas in the selected range.   There may be constants.
0
 
[ fanpages ]IT Services ConsultantCommented:
This code performs as you expected (because of the On Error Resume Next statement, as I mentioned above):

  Dim LastColumn                                        As Integer
  Dim FirstRow                                          As Long
  Dim LastRow                                           As Long
  Dim RStoreConstants                                   As Range
  Dim TheWorkSheet                                      As Worksheet
  
  LastColumn = 20
  FirstRow = 1&
  LastRow = 10&
  
  Set TheWorkSheet = ActiveSheet
  Set RStoreConstants = Nothing
  
  On Error Resume Next
  Set RStoreConstants = TheWorkSheet.Range(Cells(FirstRow, 1), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeConstants)
  On Error GoTo 0
  
  If Not (RStoreConstants Is Nothing) Then
     Rem Do Something
  End If

Open in new window

0
 
peispudAuthor Commented:
Thank you all.
0
 
[ fanpages ]IT Services ConsultantCommented:
You're welcome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now