Solved

Test for a range that evaluates to nothing

Posted on 2014-10-19
8
64 Views
Last Modified: 2014-10-19
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
Comment
Question by:peispud
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
If Not rStoreConstants Is Nothing Then
0
 

Author Comment

by:peispud
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Are the cell values in the range the result of formulas?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:peispud
Comment Utility
mixed.   There are always formulas in the selected range.   There may be constants.
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:peispud
Comment Utility
Thank you all.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
Comment Utility
You're welcome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now