HI Govert...The following function compiles and works GREAT when COUNTING comments correctly in a range providing there EXIST comments in that range but if there are NO comments in the specified range then
up pops the ComException "No cells were found." error...
I have tried every preCheck I can think of to test for NOTHING exists before running the count code... and 'nothing' works... (isnothing, isDBnull, etc, etc, etc.)
I have even handled it (trap) in the Catch and it does Catch it and return the correct value of 0 to the calling Sub... but i have read it is not good code to pass return values in the catch area... and it feels wrong ?
There
must be more elegant code to pre-vent the com ex ? Public Shared Function CountCommentsInRange(rangeName As Range) As Integer
Dim CellCount As Integer = 0
Try
If IsNothing(rangeName.SpecialCells(XlCellType.xlCellTypeComments).Count) = False Then
'COM EXCEPTION occurs on this line if there are NO comments in the range... ditto next line if no pre IF test CellCount = rangeName.SpecialCells(XlCellType.xlCellTypeComments).Count
CountCommentsInRange = CellCount
End If
Catch ex As System.Runtime.InteropServices.COMException
If ex.Message = "No cells were found." Then
CountCommentsInRange = 0
End If
End Try
End Function