ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how error-trap "no cells were found error" (https://www.excelbanter.com/excel-programming/332638-how-error-trap-no-cells-were-found-error.html)

Ian Elliott

how error-trap "no cells were found error"
 
Thanks for any help.
I have some code in my macro:
dSum = Application.Sum(Selection.SpecialCells(xlVisible))
but if there is nothing selected (at least visibly selected), I get an error
"Run-time error '1004': No cells selected."
Is there some kind of IF, like IF (Application.Sum...)=NULL or something so
my program doesn't terminate?
Thanks

Jim Thomlinson[_4_]

how error-trap "no cells were found error"
 
Try this..

Dim rngToSum As Range
Dim dSum As Double

Set rngToSum = Selection.SpecialCells(xlVisible)

If rngToSum Is Nothing Then
dSum = 0
Else
dSum = Application.Sum(rngToSum)
End If

--
HTH...

Jim Thomlinson


"Ian Elliott" wrote:

Thanks for any help.
I have some code in my macro:
dSum = Application.Sum(Selection.SpecialCells(xlVisible))
but if there is nothing selected (at least visibly selected), I get an error
"Run-time error '1004': No cells selected."
Is there some kind of IF, like IF (Application.Sum...)=NULL or something so
my program doesn't terminate?
Thanks


GB

how error-trap "no cells were found error"
 
One way is to test the Selection.

If TypeName(Application.Selection) = "Nothing" Then
'No selection was made
Else
'Perform work on selection
End If

"Ian Elliott" wrote:

Thanks for any help.
I have some code in my macro:
dSum = Application.Sum(Selection.SpecialCells(xlVisible))
but if there is nothing selected (at least visibly selected), I get an error
"Run-time error '1004': No cells selected."
Is there some kind of IF, like IF (Application.Sum...)=NULL or something so
my program doesn't terminate?
Thanks


Dave Peterson[_5_]

how error-trap "no cells were found error"
 
I think you'll have to watch out for the error:

Option Explicit
Sub testm()

Dim rngToSum As Range
Dim dSum As Double

Set rngToSum = Nothing
On Error Resume Next
Set rngToSum = Selection.SpecialCells(xlVisible)
On Error GoTo 0

If rngToSum Is Nothing Then
dSum = 0
Else
dSum = Application.Sum(rngToSum)
End If
End Sub

======

Another option (I like Jim's better):

dSum = 0
on error resume next
dSum = Application.Sum(Selection.SpecialCells(xlVisible))
on error goto 0



Jim Thomlinson wrote:

Try this..

Dim rngToSum As Range
Dim dSum As Double

Set rngToSum = Selection.SpecialCells(xlVisible)

If rngToSum Is Nothing Then
dSum = 0
Else
dSum = Application.Sum(rngToSum)
End If

--
HTH...

Jim Thomlinson

"Ian Elliott" wrote:

Thanks for any help.
I have some code in my macro:
dSum = Application.Sum(Selection.SpecialCells(xlVisible))
but if there is nothing selected (at least visibly selected), I get an error
"Run-time error '1004': No cells selected."
Is there some kind of IF, like IF (Application.Sum...)=NULL or something so
my program doesn't terminate?
Thanks


--

Dave Peterson


All times are GMT +1. The time now is 09:04 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com