Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel worksheets and VB .NET

Hi, I need to conditionally rename a worksheet in an excel 2000 workbook.
I have the following piece of code:
application.excel.worksheets("SheetName").Name = "NewSheetName"

which works to rename it. However, if SheetName doesn't exist or has been
renamed the program throws and exception. How can I check to make sure that
a sheet exists in the sheets collection?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Excel worksheets and VB .NET

Christopher,

I think you should take a look at Exception Handling (Try, Catch and Finally
or something like that) in VB.Net and catch the exception and deal with it
in VB.Net.

hth,

Doug Glancy

"Christopher Kain" wrote in message
...
Hi, I need to conditionally rename a worksheet in an excel 2000 workbook.
I have the following piece of code:
application.excel.worksheets("SheetName").Name = "NewSheetName"

which works to rename it. However, if SheetName doesn't exist or has been
renamed the program throws and exception. How can I check to make sure

that
a sheet exists in the sheets collection?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel worksheets and VB .NET

it probably takes more code to check than to ignore the error

on error resume next
ActiveWorkbook.worksheets("SheetName").Name = "NewSheetName"
on error goto 0

dim sh as Worksheet
set sh = nothing
if you must check
on error resume next
set sh = ActiveWorkbook.worksheets("Sheetname")
On error goto 0
if not sh is nothing then
' sheet exists
Activeworkbook.worksheets("SheetName").Name = "NewSheetName"
End if

another approach is to loop through all the sheets to see if any has that
name.

--
Regards,
Tom Ogilvy

"Christopher Kain" wrote in message
...
Hi, I need to conditionally rename a worksheet in an excel 2000 workbook.
I have the following piece of code:
application.excel.worksheets("SheetName").Name = "NewSheetName"

which works to rename it. However, if SheetName doesn't exist or has been
renamed the program throws and exception. How can I check to make sure

that
a sheet exists in the sheets collection?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel worksheets and VB .NET

didn't see the vb .Net in your subject - just read your posting - best to
put all pertinent information in the body of the email.

Nonetheless, perhaps you can incorporate the error handling into your code.

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
it probably takes more code to check than to ignore the error

on error resume next
ActiveWorkbook.worksheets("SheetName").Name = "NewSheetName"
on error goto 0

dim sh as Worksheet
set sh = nothing
if you must check
on error resume next
set sh = ActiveWorkbook.worksheets("Sheetname")
On error goto 0
if not sh is nothing then
' sheet exists
Activeworkbook.worksheets("SheetName").Name = "NewSheetName"
End if

another approach is to loop through all the sheets to see if any has that
name.

--
Regards,
Tom Ogilvy

"Christopher Kain" wrote in message
...
Hi, I need to conditionally rename a worksheet in an excel 2000

workbook.
I have the following piece of code:
application.excel.worksheets("SheetName").Name = "NewSheetName"

which works to rename it. However, if SheetName doesn't exist or has

been
renamed the program throws and exception. How can I check to make sure

that
a sheet exists in the sheets collection?






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
Unable to save modified Excel 2003 worksheets using Excel 2007 vkeller Excel Discussion (Misc queries) 0 May 9th 08 10:38 PM
How use info in Excel shared worksheets to create new worksheets dkc Excel Worksheet Functions 0 June 28th 07 08:36 PM
Weird problem with Excel 2000...Worksheets disappearing in a shared Excel file BrianL_SF Excel Discussion (Misc queries) 2 October 10th 06 08:27 PM
How to send Excel 2000 worksheets to Excel 2003? SecretarybyBarry Excel Discussion (Misc queries) 2 February 21st 05 05:28 PM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM


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