Excel worksheets and VB .NET

  • Thread starter Thread starter Christopher Kain
  • Start date Start date
C

Christopher Kain

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?
 
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
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top