Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 1004 "Application-defined or object-defined error"

Guys,

The following code gives the 1004 "Application-defined or
object-defined error" error when is inside a function:

Function myFunction() As Integer
myFunction = 0
On Error GoTo Error
Worksheets(1).Range("B2").Value = 3.14159
Exit Function
Error:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error
in myFunction"
myFunction = -1
End Function

But the same code works inside a sub:

Sub mySub()
On Error GoTo Error
Worksheets(1).Range("B2").Value = 3.14159
Exit Sub
Error:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error
in mySub"
End Sub

I can't figure out why and can't work around it. Does anyone have any
ideias? Any help would be deeply appreciated.

Thanks in advance!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default 1004 "Application-defined or object-defined error"

What version of Excel.?

Function works in Excel 2003.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default 1004 "Application-defined or object-defined error"

Error is a reserved word. Change it to something else like Errorhandler

On Error goto ErrorHandler

--
HTH...

Jim Thomlinson


"guiu" wrote:

Guys,

The following code gives the 1004 "Application-defined or
object-defined error" error when is inside a function:

Function myFunction() As Integer
myFunction = 0
On Error GoTo Error
Worksheets(1).Range("B2").Value = 3.14159
Exit Function
Error:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error
in myFunction"
myFunction = -1
End Function

But the same code works inside a sub:

Sub mySub()
On Error GoTo Error
Worksheets(1).Range("B2").Value = 3.14159
Exit Sub
Error:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error
in mySub"
End Sub

I can't figure out why and can't work around it. Does anyone have any
ideias? Any help would be deeply appreciated.

Thanks in advance!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 1004 "Application-defined or object-defined error"

If you're calling =myFunction() from a worksheet cell, then you can't change
other cells in this function. Functions can return values to the cell that
holds them.

guiu wrote:

Guys,

The following code gives the 1004 "Application-defined or
object-defined error" error when is inside a function:

Function myFunction() As Integer
myFunction = 0
On Error GoTo Error
Worksheets(1).Range("B2").Value = 3.14159
Exit Function
Error:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error
in myFunction"
myFunction = -1
End Function

But the same code works inside a sub:

Sub mySub()
On Error GoTo Error
Worksheets(1).Range("B2").Value = 3.14159
Exit Sub
Error:
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Error
in mySub"
End Sub

I can't figure out why and can't work around it. Does anyone have any
ideias? Any help would be deeply appreciated.

Thanks in advance!


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default 1004 "Application-defined or object-defined error"

Howard, it's Excel 2003.
Jim, the On Error works fine, that's how I know what error is
occurring.
Dave, what you say makes sense.

Thank you, all! I'll see what I can do here.
Regards



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 1004 "Application-defined or object-defined error"


on which line does the error occur?

and how is the function being called (from code or from a worksheet)?

there are several ways to check the line but the simplest is to comment
out (or remove) the error trap


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=500401

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default 1004 "Application-defined or object-defined error"

"guiu" wrote in message
oups.com...
Howard, it's Excel 2003.


I had called the function from within code.

If you use the function in a cell, the error 1004 does occur.

Use

' Worksheets(1).Range("B2").Value = 3.14159
myFunction = 3.14159



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
Macro Run-time Error 1004 Application Defined or Object Defined Error Anddmx Excel Programming 6 June 9th 04 03:40 PM
What does "Application-defined or object-defined error" mean? Shatin Excel Programming 4 February 3rd 04 10:15 AM
"Application-defined or object-defined error" while printing excel report chemburkar Excel Programming 0 February 2nd 04 08:33 PM
"Run Time Error 1004 Application Defined or Object Defined Error." BJC Excel Programming 4 October 26th 03 03:09 AM


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