Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Can't seem to find the error with this code

Hi,
I don't know why I can't pass thru this code. I know it's fairly straight
forward but everytime I run it's always giving the error "Object required"
even if my active workbook contains the sheetname "mydata". Can you tell me
what's wrong with this code. All I am trying to do is to check if a sheetname
exists.

On Error Resume Next
Set x = ActiveWorbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err)
MsgBox "Sheet does not exist"
End If

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Can't seem to find the error with this code

It works fine on my machine...
Make sure you are using the line
Dim x

....and that Worbook is a typo (should be spelt Workbook)

Rgds
J



"matelot" wrote in message
...
Hi,
I don't know why I can't pass thru this code. I know it's fairly straight
forward but everytime I run it's always giving the error "Object required"
even if my active workbook contains the sheetname "mydata". Can you tell
me
what's wrong with this code. All I am trying to do is to check if a
sheetname
exists.

On Error Resume Next
Set x = ActiveWorbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err)
MsgBox "Sheet does not exist"
End If

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 115
Default Can't seem to find the error with this code

Hi matelot,

Line 2 - you spell ActiveWorkbook incorrectly.

This works - do you really need two MsgBoxes?;

Sub tst()
Dim x As Worksheet

On Error Resume Next
Set x = ActiveWorkbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err) _
&"Sheet does not exist"
End If
On Error Goto 0
End Sub

I like this better;

Sub SheetExists()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
If sht.Name = "mydata" Then
MsgBox "Passed "
Exit Sub
End If
Next sht
MsgBox "Sheet does not exist"
End Sub

In general;

- it helps if you declare all variables
- use the debug tool to pick up on spelling errors
- try not to use code that relies on generating errors
(it can be elegant, as in your example, but you forego the ability
to use error trapping for its intended purpose)

Ed Ferrero
http://www.edferrero.com


Hi,
I don't know why I can't pass thru this code. I know it's fairly straight
forward but everytime I run it's always giving the error "Object required"
even if my active workbook contains the sheetname "mydata". Can you tell
me
what's wrong with this code. All I am trying to do is to check if a
sheetname
exists.

On Error Resume Next
Set x = ActiveWorbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err)
MsgBox "Sheet does not exist"
End If

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Can't seem to find the error with this code

Guys,
Thanks so much. It's my typo.

M
"Ed Ferrero" wrote:

Hi matelot,

Line 2 - you spell ActiveWorkbook incorrectly.

This works - do you really need two MsgBoxes?;

Sub tst()
Dim x As Worksheet

On Error Resume Next
Set x = ActiveWorkbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err) _
&"Sheet does not exist"
End If
On Error Goto 0
End Sub

I like this better;

Sub SheetExists()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
If sht.Name = "mydata" Then
MsgBox "Passed "
Exit Sub
End If
Next sht
MsgBox "Sheet does not exist"
End Sub

In general;

- it helps if you declare all variables
- use the debug tool to pick up on spelling errors
- try not to use code that relies on generating errors
(it can be elegant, as in your example, but you forego the ability
to use error trapping for its intended purpose)

Ed Ferrero
http://www.edferrero.com


Hi,
I don't know why I can't pass thru this code. I know it's fairly straight
forward but everytime I run it's always giving the error "Object required"
even if my active workbook contains the sheetname "mydata". Can you tell
me
what's wrong with this code. All I am trying to do is to check if a
sheetname
exists.

On Error Resume Next
Set x = ActiveWorbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err)
MsgBox "Sheet does not exist"
End If

Thanks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default Can't seem to find the error with this code

Is this a just typo?
Set x = ActiveWorbook.Sheets("mydata") should be
Set x = ActiveWorkbook.Sheets("mydata")

keizi

"matelot" wrote in message
...
Hi,
I don't know why I can't pass thru this code. I know it's fairly straight
forward but everytime I run it's always giving the error "Object required"
even if my active workbook contains the sheetname "mydata". Can you tell me
what's wrong with this code. All I am trying to do is to check if a sheetname
exists.

On Error Resume Next
Set x = ActiveWorbook.Sheets("mydata")
If Err = 0 Then
MsgBox "Passed "
Else
MsgBox ActiveWorkbook.Name & " - " & Error(Err)
MsgBox "Sheet does not exist"
End If

Thanks


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
Error dialog box meaning and how to find error Jeanne Excel Worksheet Functions 2 September 4th 08 04:59 AM
Code to find code D. Excel Discussion (Misc queries) 2 August 12th 07 06:16 PM
help with this error-Compile error: cant find project or library JackR Excel Discussion (Misc queries) 2 June 10th 06 09:09 PM
How can I still go to the error-code after a On Error Goto? Michel[_3_] Excel Programming 2 May 4th 04 04:21 AM
Code Error - Run Time Error 5 (Disable Cut, Copy & Paste) Tim[_36_] Excel Programming 4 April 23rd 04 02:53 AM


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