Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default On Error GoTo 0

What does "On Error GoTo 0" mean? I see this on many of the posts.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default On Error GoTo 0

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default On Error GoTo 0

Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default On Error GoTo 0

Both Sub and Function are generally called procedures, and you cannot nest
one procedure within another. You must structure the code as

Sub A()
' blah blah blah
End Sub
Function X()
' blah blah
End Function

A Function or Sub statement may not occur within the body of another
procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default On Error GoTo 0

Chip, I guess I am missing the point. Can the Sub and Function interact? My
goal, based on the sample code where the "Sub InvoiceCheck" is, is to have
VBA, using a Function, evaluate for the existence of a worksheet named
"Invoice" then use the returned value, True or False, then pass the results
into a variable then have the Sub print the Invoice worksheet. Please advise
if you have another solution.

"Chip Pearson" wrote:

Both Sub and Function are generally called procedures, and you cannot nest
one procedure within another. You must structure the code as

Sub A()
' blah blah blah
End Sub
Function X()
' blah blah
End Function

A Function or Sub statement may not occur within the body of another
procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default On Error GoTo 0

You could do something like:

Sub InvoiceCheck()
'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
End If
End Sub

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

Freddy wrote:

Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''

"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default On Error GoTo 0

Chip, I think I moved ahead a bit. The following code executes but does not
print anything and there is a worksheet named "Invoice" in the active
workbook and there is a working network printer. Please review and advise.

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'Usage of Function named bExists
Sub InvoiceCheck()
If bExists("Invoice") Then
Worksheets("Invoice").PrintOut
Else
End If
End Sub

"Chip Pearson" wrote:

Both Sub and Function are generally called procedures, and you cannot nest
one procedure within another. You must structure the code as

Sub A()
' blah blah blah
End Sub
Function X()
' blah blah
End Function

A Function or Sub statement may not occur within the body of another
procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default On Error GoTo 0

I tried your code but nothing prints out and there is text in it. The
workbook I am using to test has two worksheets named "ECS" and "Invoice". I
also have a working network printer.

"Dave Peterson" wrote:

You could do something like:

Sub InvoiceCheck()
'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
End If
End Sub

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

Freddy wrote:

Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''

"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.


--

Dave Peterson

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default On Error GoTo 0

If you didn't change the code, it's looking for "sheet1" and printing that it if
it finds it.

You could use a line like this in the Subroutine:

if bexists("ecs") then
worksheets("ecs").printout
end if

But I'm not sure what you're really doing.

Freddy wrote:

I tried your code but nothing prints out and there is text in it. The
workbook I am using to test has two worksheets named "ECS" and "Invoice". I
also have a working network printer.

"Dave Peterson" wrote:

You could do something like:

Sub InvoiceCheck()
'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
End If
End Sub

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

Freddy wrote:

Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''

"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.


--

Dave Peterson


--

Dave Peterson
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default On Error GoTo 0

No, you call the function from within a sub

Sub InvoiceCheck()
'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
End If
End Sub


Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.





  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default On Error GoTo 0



demo'd from the immediate window:

? bExists("Invoice")
True
? bExists("iNvOiCe ")
True
? bExists("House")
False

the workbook had a sheet named invoice. It didn't have a sheet named house.

--
Regards,
Tom Ogilvy


"Freddy" wrote in message
...
Chip, I think I moved ahead a bit. The following code executes but does
not
print anything and there is a worksheet named "Invoice" in the active
workbook and there is a working network printer. Please review and advise.

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'Usage of Function named bExists
Sub InvoiceCheck()
If bExists("Invoice") Then
Worksheets("Invoice").PrintOut
Else
End If
End Sub

"Chip Pearson" wrote:

Both Sub and Function are generally called procedures, and you cannot
nest
one procedure within another. You must structure the code as

Sub A()
' blah blah blah
End Sub
Function X()
' blah blah
End Function

A Function or Sub statement may not occur within the body of another
procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public
Function
code and the usage portion of your sample code. Can I not use the
Function
within a sub procedure? If I attempt to run the sub below I get an
error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you
see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want
the
system to start catching errors again so you turn error handling back
on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.






  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default On Error GoTo 0

Others have focused on helping you resolve your problem as you framed it.
Here's a different take on how you could (should, IMO) approach it. It also
connects to your initial question about the On Error clause.

To test a worksheet exists, with error handling all you need is
Option Explicit

Function ActiveWBSheetExists(aSheetName As String) As Boolean
On Error Resume Next
SheetExists = Not (ActiveWorkbook.Sheets(aSheetName) Is Nothing)
End Function

or the more general purpose routine
Function SheetExists(aSheetName As String, _
Optional aWB As Workbook) As Boolean
If aWB Is Nothing Then Set aWB = ActiveWorkbook
On Error Resume Next
SheetExists = Not (aWB.Sheets(aSheetName) Is Nothing)
End Function

So, to print a sheet if it exists, one would add the procedu

Sub printIfPresent()
If SheetExists("sheet 1") Then ActiveWorkbook.Sheets("sheet 1").PrintOut
End Sub

But, that leads to the next improvement. Why bother testing if a sheet
exists? Why not simply print it if it's possible with

Sub PrintIfPossible()
On Error Resume Next
ActiveWorkbook.Sheets("sheet 1").PrintOut
End Sub

--

Tushar Mehta
http://www.tushar-mehta.com
Custom business solutions leveraging a multi-disciplinary approach
In Excel 2007 double-click to format may not work; right click and select
from the menu


"Freddy" wrote:

Chip, I think I moved ahead a bit. The following code executes but does not
print anything and there is a worksheet named "Invoice" in the active
workbook and there is a working network printer. Please review and advise.

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'Usage of Function named bExists
Sub InvoiceCheck()
If bExists("Invoice") Then
Worksheets("Invoice").PrintOut
Else
End If
End Sub

"Chip Pearson" wrote:

Both Sub and Function are generally called procedures, and you cannot nest
one procedure within another. You must structure the code as

Sub A()
' blah blah blah
End Sub
Function X()
' blah blah
End Function

A Function or Sub statement may not occur within the body of another
procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.




  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default On Error GoTo 0

Thanks to all who have assisted me. Good show!

"Tushar Mehta" wrote:

Others have focused on helping you resolve your problem as you framed it.
Here's a different take on how you could (should, IMO) approach it. It also
connects to your initial question about the On Error clause.

To test a worksheet exists, with error handling all you need is
Option Explicit

Function ActiveWBSheetExists(aSheetName As String) As Boolean
On Error Resume Next
SheetExists = Not (ActiveWorkbook.Sheets(aSheetName) Is Nothing)
End Function

or the more general purpose routine
Function SheetExists(aSheetName As String, _
Optional aWB As Workbook) As Boolean
If aWB Is Nothing Then Set aWB = ActiveWorkbook
On Error Resume Next
SheetExists = Not (aWB.Sheets(aSheetName) Is Nothing)
End Function

So, to print a sheet if it exists, one would add the procedu

Sub printIfPresent()
If SheetExists("sheet 1") Then ActiveWorkbook.Sheets("sheet 1").PrintOut
End Sub

But, that leads to the next improvement. Why bother testing if a sheet
exists? Why not simply print it if it's possible with

Sub PrintIfPossible()
On Error Resume Next
ActiveWorkbook.Sheets("sheet 1").PrintOut
End Sub

--

Tushar Mehta
http://www.tushar-mehta.com
Custom business solutions leveraging a multi-disciplinary approach
In Excel 2007 double-click to format may not work; right click and select
from the menu


"Freddy" wrote:

Chip, I think I moved ahead a bit. The following code executes but does not
print anything and there is a worksheet named "Invoice" in the active
workbook and there is a working network printer. Please review and advise.

Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'Usage of Function named bExists
Sub InvoiceCheck()
If bExists("Invoice") Then
Worksheets("Invoice").PrintOut
Else
End If
End Sub

"Chip Pearson" wrote:

Both Sub and Function are generally called procedures, and you cannot nest
one procedure within another. You must structure the code as

Sub A()
' blah blah blah
End Sub
Function X()
' blah blah
End Function

A Function or Sub statement may not occur within the body of another
procedure.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Freddy" wrote in message
...
Thank you for that explanation. By the way, can you respond to another
question that apparently Tom Ogilvy has not had time to review?

I do not understand how the relationship works between the Public Function
code and the usage portion of your sample code. Can I not use the Function
within a sub procedure? If I attempt to run the sub below I get an error
which reads: Compile error: Expected End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
Sub InvoiceCheck()
Public Function bExists(s As String)
For Each sh In ActiveWorkbook.Worksheets
If LCase(s) = LCase(sh.Name) Then
bExists = True
Exit Function
End If
Next
bExists = False
End Function

'usage
If bExists("sheet1") Then
Worksheets("sheet1").PrintOut
Else

End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''






"Jim Thomlinson" wrote:

On Error GoTo 0

Return to the default error handling in VBA. Most instances when you see
this are the result of setting the system to ignor errors while a
potentially
problematic line of code executes. After that line is passed you want the
system to start catching errors again so you turn error handling back on
to
the default settings.
--
HTH...

Jim Thomlinson


"Freddy" wrote:

What does "On Error GoTo 0" mean? I see this on many of the posts.



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
On Error Goto 0 Chip Pearson Excel Programming 0 January 6th 07 12:04 AM
On Error Goto 0 Bob Phillips Excel Programming 0 January 4th 07 09:46 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
On Error Goto doesn't goto Paul Excel Programming 1 October 15th 04 03:51 PM
On Error Goto doesn't goto Paul Excel Programming 0 October 15th 04 03:05 PM


All times are GMT +1. The time now is 04:48 AM.

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"