Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
GB GB is offline
external usenet poster
 
Posts: 230
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to error trap "... cannot find the data..." message? [email protected] Excel Discussion (Misc queries) 4 August 29th 08 02:57 AM
Best way to trap error to MsgBox "Too many cell formats" [email protected] Excel Discussion (Misc queries) 0 August 22nd 06 09:52 PM
"trap" a compile time error Eric Barber Excel Programming 0 November 12th 04 11:24 AM
Getting "compile error" "method or data member not found" on reinstall Bp Excel Programming 1 April 23rd 04 04:42 PM
How to trap the error message "Remote Data not accessible"? rghpvf Excel Programming 0 January 7th 04 05:40 PM


All times are GMT +1. The time now is 11:22 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"