Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / January 2008

Tip: Looking for answers? Try searching our database.

If statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 14 Jan 2008 01:57 GMT
I am trying to write a macro that renames worksheets. My problem is I don't
always have the same worksheets in each workbook. I would appreciate it if
someone could give me some direction on how to write the if statement so
that if the called out worksheet doesn't exist then it is skipped and the
macro doesn't error out. Thank you in advance.

Joe
Bernie Deitrick - 14 Jan 2008 16:04 GMT
Joe,

See the macro below.

HTH,
Bernie
MS Excel MVP

Sub ProperWayToUseMulitpleErrorHandlers()

Dim strName As String

Check1:
On Error GoTo ErrCheck1
strName = Worksheets("Sheet Doesn't Exist").Name
MsgBox """Sheet Doesn't Exist"" exists and will be renamed"
Worksheets("Sheet Doesn't Exist").Name = "New Name 1"
'Code to work with sheet "Sheet Doesn't Exist"

Check2:
On Error GoTo ErrCheck2
strName = Worksheets("Sheet Does Exist").Name
MsgBox """Sheet Does Exist"" exists and will be renamed"
Worksheets("Sheet Does Exist").Name = "New Name 2"
'Code to work with sheet "Sheet Does Exist"

Check3:
On Error GoTo ErrCheck3
strName = Worksheets("Sheet May Exist").Name
MsgBox """Sheet May Exist"" exists and will be renamed"
Worksheets("Sheet May Exist").Name = "New Name 3"
'Code to work with sheet "Sheet May Exist"

Stopp:
MsgBox "All Done"

Exit Sub

ErrCheck1:
MsgBox """Sheet Doesn't Exist"" does not exist and cannot be renamed"
Resume Check2
ErrCheck2:
MsgBox """Sheet Does Exist"" does not exist and cannot be renamed"
Resume Check3
ErrCheck3:
MsgBox """Sheet May Exist"" does not exist and cannot be renamed"
Resume Stopp

End Sub

>I am trying to write a macro that renames worksheets. My problem is I don't always have the same
>worksheets in each workbook. I would appreciate it if someone could give me some direction on how
>to write the if statement so that if the called out worksheet doesn't exist then it is skipped and
>the macro doesn't error out. Thank you in advance.
>
> Joe
Joe - 15 Jan 2008 01:05 GMT
Thank you very much for your assistance.

Joe

> Joe,
>
[quoted text clipped - 53 lines]
>>
>> Joe
Joe - 19 Jan 2008 18:07 GMT
Sorry for the delay in my reply but this post worked beautifully. Thank you
for the help.

Joe

> Joe,
>
[quoted text clipped - 53 lines]
>>
>> Joe
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.