Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default "object-defined error" when Run but okay when Stepping

hello,

My VBA code sporadically hangs up with error "object-defined error" when I
try to add a new worksheet and draw shapes on it. Please see code below:

Option Explicit
'
Sub Sam()
Dim s As Worksheet

Set s = Sheets.Add

s.Select
ActiveSheet.Shapes.AddLine(10, 10, 250, 250).Select

End Sub
'
Sub bob()
Dim s As Worksheet

Set s = Sheets.Add

s.Shapes.AddLine(10, 10, 250, 250).Select

End Sub
'

When I open the workbook there is only a single worksheet. Routine "sam"
always runs fine, it always adds a new worksheet and draws the line. Routine
"bob" should do the same thing but it will give me the "object defined
error" when it is run the first time after file open; it does add the new
worksheet but then error results. After the error is dismissed "bob"
subsequently runs properly and without error...except that if all the newly
added sheets are manually deleted it again returns the error the first time
it is run.

The really weird thing is that "bob" will run fine if I step (F8) through it
under the same conditions that normally result in the error.

I don't understand why "bob" is inconsistent. I think I lifted the line
"s.Shapes.AddLine(10, 10, 250, 250).Select" from Excel VBA Help since it
seemed more efficient.

Anyone know why this line fails?

thanks!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default "object-defined error" when Run but okay when Stepping

Without testing, it looks like it's the .select that's giving you a problem.

You have to be on a sheet to select something on that sheet (range or object).
So if the sheet isn't the activesheet, you can't select stuff.

But "set s = sheets.add" should make that new worksheet the active sheet. (So
I'm confused here.)

But how about adding the line without selecting it:

Sub bob()
Dim s As Worksheet
Set s = Sheets.Add
s.Shapes.AddLine 10, 10, 250, 250
End Sub

Do you have the code in a general module or is it behind a worksheet or behind
ThisWorkbook?


Fred Smif wrote:

hello,

My VBA code sporadically hangs up with error "object-defined error" when I
try to add a new worksheet and draw shapes on it. Please see code below:

Option Explicit
'
Sub Sam()
Dim s As Worksheet

Set s = Sheets.Add

s.Select
ActiveSheet.Shapes.AddLine(10, 10, 250, 250).Select

End Sub
'
Sub bob()
Dim s As Worksheet

Set s = Sheets.Add

s.Shapes.AddLine(10, 10, 250, 250).Select

End Sub
'

When I open the workbook there is only a single worksheet. Routine "sam"
always runs fine, it always adds a new worksheet and draws the line. Routine
"bob" should do the same thing but it will give me the "object defined
error" when it is run the first time after file open; it does add the new
worksheet but then error results. After the error is dismissed "bob"
subsequently runs properly and without error...except that if all the newly
added sheets are manually deleted it again returns the error the first time
it is run.

The really weird thing is that "bob" will run fine if I step (F8) through it
under the same conditions that normally result in the error.

I don't understand why "bob" is inconsistent. I think I lifted the line
"s.Shapes.AddLine(10, 10, 250, 250).Select" from Excel VBA Help since it
seemed more efficient.

Anyone know why this line fails?

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
"Compile error: sub or function not defined" Joe Excel Discussion (Misc queries) 4 January 30th 06 08:19 PM
Formula syntax, "Object-defined error" [email protected] Excel Programming 2 December 29th 04 04:45 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 11:03 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"