+ Reply to Thread
Results 1 to 5 of 5

If Special Cells count is zero

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    If Special Cells count is zero

    Should be simple, but for me not so much. Here's what I'm trying to do: IF there are any formula cells in my selection shut it down, otherwise do my stuff. My problem is when there are no formula cells, excel throws up an error saying no cells are found. Any help is appreciated.
    If Selection.Cells.SpecialCells(xlCellTypeFormulas).Count > 0 Then
            MsgBox "Macro is exiting because there are" & vbCrLf & _
                   "formulas in the range to be worked.", vbOKOnly + vbCritical, "CAN'T PROCESS FORMULA CELLS"
            Exit Sub
        Else
            MsgBox "No Formulas"
            'do the rest of my stuff
        End If

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: If Special Cells count is zero

    Hi, jomili,

    one way around raising the run-time error is to do it like this:
    Dim varFormulas As Variant
    
    On Error Resume Next
    varFormulas = Selection.Cells.SpecialCells(xlCellTypeFormulas).Count
    On Error GoTo 0
    If varFormulas > 0 Then
        MsgBox "Macro is exiting because there are" & vbCrLf & _
               "formulas in the range to be worked.", vbOKOnly + vbCritical, "CAN'T PROCESS FORMULA CELLS"
        Exit Sub
    Else
        MsgBox "No Formulas"
        'do the rest of my stuff
    End If
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: If Special Cells count is zero

    Thanks Holger, that seems to work well. I appreciate the help.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: If Special Cells count is zero

    Hi jomili,

    You need to 'trap' the Runtime error, so you can continue processing with syntax like the following:
    Sub SpecialCellsRuntimeError()
    
      Dim iCount As Long
    
      On Error Resume Next
      iCount = 0
      iCount = Selection.Cells.SpecialCells(xlCellTypeFormulas).Count
      Err.Clear
      On Error GoTo 0
      
      
      If iCount > 0 Then
        MsgBox "The Selection has cells that have formulas."
      Else
        MsgBox "The Selection has NO cells with formulas."
      End If
    
    End Sub
    Lewis

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: If Special Cells count is zero

    Sorry Lewis, guess I was posting the same time you were. Thanks for the help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Special count issue
    By JO505 in forum Excel General
    Replies: 0
    Last Post: 03-06-2015, 04:09 PM
  2. Count Special Situation
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-12-2014, 12:45 PM
  3. Count - Special case
    By shiyaschennattu in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-03-2013, 12:46 PM
  4. Edit for special filter and count...
    By superspurs in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2009, 10:13 AM
  5. How do you count (not sum) cells containing special formatting?
    By tbank in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-12-2005, 06:05 PM
  6. [SOLVED] How do I count If for these special distinctions
    By Desperate Novice in forum Excel General
    Replies: 2
    Last Post: 01-24-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1