Solved

Test for a range that evaluates to nothing

Posted on 2014-10-19
8
70 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 ]
ID: 40390701
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 46

Expert Comment

by:Martin Liss
ID: 40390702
If Not rStoreConstants Is Nothing Then
0
 

Author Comment

by:peispud
ID: 40390708
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 46

Expert Comment

by:Martin Liss
ID: 40390715
Are the cell values in the range the result of formulas?
0
 

Author Comment

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

Accepted Solution

by:
[ fanpages ] earned 500 total points
ID: 40390720
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
ID: 40390727
Thank you all.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40390730
You're welcome.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

810 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