Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA Error Handling

In VBA I have code that accesses an Excel Workbook. The code has multiple Private Subs which is looking for various worksheets in the Excel Workbook. Should a Private Sub be looking for an worksheet that does not exist VBA returns an error and stops going through the remaining private subs. I would like to create an Error Handler that upon a Private Sub not locating a particular worksheet it exits that private sub and continues on to the next private sub without stopping the processing. Is this possible?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA Error Handling

Hi Robin,

Am Fri, 18 Oct 2013 13:17:00 -0700 (PDT) schrieb Robin Coe:

In VBA I have code that accesses an Excel Workbook. The code has multiple Private Subs which is looking for various worksheets in the Excel Workbook. Should a Private Sub be looking for an worksheet that does not exist VBA returns an error and stops going through the remaining private subs. I would like to create an Error Handler that upon a Private Sub not locating a particular worksheet it exits that private sub and continues on to the next private sub without stopping the processing. Is this possible?


write as first code line
On Error Resume Next


Regards
Claus B.
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default VBA Error Handling

Optional approach is to use this function in an If...Then construct!

Function bSheetExists(WksName As String) As Boolean
Dim x As Worksheet
On Error Resume Next
Set x = ActiveWorkbook.Sheets(WksName)
bSheetExists = (Err = 0)
End Function

Example:

If bSheetExists("Sheet1") Then
'do stuff with it
End If

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default VBA Error Handling

Here's another that lets you specify the workbook...

Function SheetExists(Sheetname$, Optional wb As Excel.Workbook =
Nothing) As Boolean
'Modified from a Chip Pearson example
On Error Resume Next
SheetExists = CBool(Len(IIf(wb Is Nothing, ActiveWorkbook,
wb).Sheets(Sheetname).name))
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA Error Handling

On Friday, October 18, 2013 4:17:00 PM UTC-4, Robin Coe wrote:
In VBA I have code that accesses an Excel Workbook. The code has multiple Private Subs which is looking for various worksheets in the Excel Workbook.. Should a Private Sub be looking for an worksheet that does not exist VBA returns an error and stops going through the remaining private subs. I would like to create an Error Handler that upon a Private Sub not locating a particular worksheet it exits that private sub and continues on to the next private sub without stopping the processing. Is this possible?



This works great....thank you


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
Err.Description as error proc name trail, error handling Neal Zimm Excel Programming 4 February 25th 10 08:07 AM
First Error works but Subsequent Error Handling Does Not Edwin Kelly Excel Programming 2 May 20th 09 02:40 PM
Error handling error # 1004 Run-time error [email protected] Excel Programming 3 May 20th 08 02:23 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM


All times are GMT +1. The time now is 06:09 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"