Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default How to use IfError in Excel 2007 VBA ?

Hello,

I am trying to figure out how to use the IfError function with Excel 2007
VBA. I have built a simple spreadsheet to test division by zero error
handling. The following line either performs the division or throws an error
11 (division by 0) instead of returning 99.

ActiveSheet.Cells(1, 3) = WorksheetFunction.IfError((ActiveSheet.Cells(1, 1)
/ ActiveSheet.Cells(1, 2)), 99)

Any help would be appreciated.

Thanks,

Xavier

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default How to use IfError in Excel 2007 VBA ?

It would make more sense to me to just check the denominator cell for 0.

dim myVal as double
myVal = 99
With activesheet
if application.isnumber(.cells(1,1).value) then
if application.isnumber(.cells(1,2).value) then
if .cells(1,2).value < 0 then
myval = .cells(1,1).value / .cells(1,2).value
end if
end if
end if
.cells(1,3).value = myval
end with




Xavier wrote:

Hello,

I am trying to figure out how to use the IfError function with Excel 2007
VBA. I have built a simple spreadsheet to test division by zero error
handling. The following line either performs the division or throws an error
11 (division by 0) instead of returning 99.

ActiveSheet.Cells(1, 3) = WorksheetFunction.IfError((ActiveSheet.Cells(1, 1)
/ ActiveSheet.Cells(1, 2)), 99)

Any help would be appreciated.

Thanks,

Xavier


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default How to use IfError in Excel 2007 VBA ?

The division by zero that I mention is just a way for me to quickly test the
IfError function. I need to use it in a different context to trap other
errors. This is the simplest example I have been able to produce but still
can't get it to work. :-(


"Dave Peterson" wrote in message
...
It would make more sense to me to just check the denominator cell for 0.

dim myVal as double
myVal = 99
With activesheet
if application.isnumber(.cells(1,1).value) then
if application.isnumber(.cells(1,2).value) then
if .cells(1,2).value < 0 then
myval = .cells(1,1).value / .cells(1,2).value
end if
end if
end if
.cells(1,3).value = myval
end with




Xavier wrote:

Hello,

I am trying to figure out how to use the IfError function with Excel 2007
VBA. I have built a simple spreadsheet to test division by zero error
handling. The following line either performs the division or throws an
error
11 (division by 0) instead of returning 99.

ActiveSheet.Cells(1, 3) = WorksheetFunction.IfError((ActiveSheet.Cells(1,
1)
/ ActiveSheet.Cells(1, 2)), 99)

Any help would be appreciated.

Thanks,

Xavier


--

Dave Peterson


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default How to use IfError in Excel 2007 VBA ?

When VBA tries to evaluate the div/0 an error is raised before getting to
the IFError worksheet function. Here's a couple of examples

Sub test()
Dim s As String
Dim x As Double, v

s = "=" & ActiveSheet.Cells(1, 1).Address & "/" & ActiveSheet.Cells(1,
2).Address
ActiveSheet.Cells(1, 3).Formula = s
x = WorksheetFunction.IfError(ActiveSheet.Cells(1, 3), 99)
ActiveSheet.Cells(1, 4) = x

v = CVErr(xlErrDiv0)
x = WorksheetFunction.IfError(v, 123)
ActiveSheet.Cells(1, 5) = x

End Sub

Wouldn't normally use the IFError function in VBA
On error resume next
v = 1/0
if err.number = 9 then
' code
elseif etc
End if
On error goto 0

Regards,
Peter T

"Xavier" wrote in message
...
The division by zero that I mention is just a way for me to quickly test
the IfError function. I need to use it in a different context to trap
other errors. This is the simplest example I have been able to produce but
still can't get it to work. :-(


"Dave Peterson" wrote in message
...
It would make more sense to me to just check the denominator cell for 0.

dim myVal as double
myVal = 99
With activesheet
if application.isnumber(.cells(1,1).value) then
if application.isnumber(.cells(1,2).value) then
if .cells(1,2).value < 0 then
myval = .cells(1,1).value / .cells(1,2).value
end if
end if
end if
.cells(1,3).value = myval
end with




Xavier wrote:

Hello,

I am trying to figure out how to use the IfError function with Excel
2007
VBA. I have built a simple spreadsheet to test division by zero error
handling. The following line either performs the division or throws an
error
11 (division by 0) instead of returning 99.

ActiveSheet.Cells(1, 3) =
WorksheetFunction.IfError((ActiveSheet.Cells(1, 1)
/ ActiveSheet.Cells(1, 2)), 99)

Any help would be appreciated.

Thanks,

Xavier


--

Dave Peterson




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default How to use IfError in Excel 2007 VBA ?

OK, if I understand you correctly, I picked up the wrong example to test
IFERROR under VBA. Thanks for the code sample you provided. I usually use
some similar case, but in the current situation, I am working with
GetPivotData and need to catch some #REF errors and IfError is the easiest
way to achieve what I am trying to do.

Thanks,

Xavier



"Peter T" <peter_t@discussions wrote in message
...
When VBA tries to evaluate the div/0 an error is raised before getting to
the IFError worksheet function. Here's a couple of examples

Sub test()
Dim s As String
Dim x As Double, v

s = "=" & ActiveSheet.Cells(1, 1).Address & "/" & ActiveSheet.Cells(1,
2).Address
ActiveSheet.Cells(1, 3).Formula = s
x = WorksheetFunction.IfError(ActiveSheet.Cells(1, 3), 99)
ActiveSheet.Cells(1, 4) = x

v = CVErr(xlErrDiv0)
x = WorksheetFunction.IfError(v, 123)
ActiveSheet.Cells(1, 5) = x

End Sub

Wouldn't normally use the IFError function in VBA
On error resume next
v = 1/0
if err.number = 9 then
' code
elseif etc
End if
On error goto 0

Regards,
Peter T

"Xavier" wrote in message
...
The division by zero that I mention is just a way for me to quickly test
the IfError function. I need to use it in a different context to trap
other errors. This is the simplest example I have been able to produce
but still can't get it to work. :-(


"Dave Peterson" wrote in message
...
It would make more sense to me to just check the denominator cell for 0.

dim myVal as double
myVal = 99
With activesheet
if application.isnumber(.cells(1,1).value) then
if application.isnumber(.cells(1,2).value) then
if .cells(1,2).value < 0 then
myval = .cells(1,1).value / .cells(1,2).value
end if
end if
end if
.cells(1,3).value = myval
end with




Xavier wrote:

Hello,

I am trying to figure out how to use the IfError function with Excel
2007
VBA. I have built a simple spreadsheet to test division by zero error
handling. The following line either performs the division or throws an
error
11 (division by 0) instead of returning 99.

ActiveSheet.Cells(1, 3) =
WorksheetFunction.IfError((ActiveSheet.Cells(1, 1)
/ ActiveSheet.Cells(1, 2)), 99)

Any help would be appreciated.

Thanks,

Xavier

--

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
IFERROR in Excel 2007 Dirrk Excel Worksheet Functions 5 April 25th 10 10:08 PM
Can IFERROR be used in Excel 2003 Compatibility View? Fabian Excel Discussion (Misc queries) 3 February 1st 10 04:56 PM
function =IFERROR LOOKUP works in excel 2007 not in excel 2003 David Ryan Excel Worksheet Functions 4 April 15th 09 03:25 PM
IFERROR MMCBRIDE Excel Worksheet Functions 3 September 13th 08 12:15 AM
iferror Roy Gudgeon[_2_] Excel Worksheet Functions 13 July 31st 08 05:24 PM


All times are GMT +1. The time now is 04:45 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"