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

I have a list of projects and each has a button with it. When the user
clicks the button the macro copies a hidden "form" worksheet and names the
new worksheet the name of the project.

The Macro works fine unless the button has already been used. Can't have two
worksheets with the same name.

I've tried the On Error command and a Err.Description with no luck.

Below is the Macro. If you have projects in the "list" worksheet and the
first project in Cell A3 and a hidden worksheet, "form", the Macro will work
the first time. The second time it's run is returns a run time error at the
Active.Sheet = mSheetName line. If it receives an error here I would like to
delete the new sheet and exit the sub.

Thanks for any help.

Sub Button4_Click()

Dim mSheetName As String

mSheetName = Cells(3, 1)
Sheets("Form").Visible = True
Sheets("Form").Select
Sheets("Form").Copy Befo=Sheets("Form")
Sheets("Form (2)").Select
Cells(4, 2) = mSheetName
ActiveSheet.Name = mSheetName
Sheets("Form").Visible = False
Sheets("List").Select

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Renaming Worksheet Error

Hi,
You may wantr to check if the sheet already exists at the begining of the
sub (before even copying the new sheet)

Dim mSheetName As String
Dim Wsh as Worksheet

mSheetName = Cells(3, 1)
on error resume next
set wsh=worksheets(mSheetName)
if err=0 then 'no error ie exist
Msgbox "Sheet name already exist. Please choose another name"
Exit sub
End if
on error goto 0

Sheets("Form").Visible = True
Sheets("Form").Select
Sheets("Form").Copy Befo=Sheets("Form")
Sheets("Form (2)").Select
Cells(4, 2) = mSheetName
ActiveSheet.Name = mSheetName
Sheets("Form").Visible = False
Sheets("List").Select

End Sub

Regards,
Sebastien

"bry32" wrote:

I have a list of projects and each has a button with it. When the user
clicks the button the macro copies a hidden "form" worksheet and names the
new worksheet the name of the project.

The Macro works fine unless the button has already been used. Can't have two
worksheets with the same name.

I've tried the On Error command and a Err.Description with no luck.

Below is the Macro. If you have projects in the "list" worksheet and the
first project in Cell A3 and a hidden worksheet, "form", the Macro will work
the first time. The second time it's run is returns a run time error at the
Active.Sheet = mSheetName line. If it receives an error here I would like to
delete the new sheet and exit the sub.

Thanks for any help.

Sub Button4_Click()

Dim mSheetName As String

mSheetName = Cells(3, 1)
Sheets("Form").Visible = True
Sheets("Form").Select
Sheets("Form").Copy Befo=Sheets("Form")
Sheets("Form (2)").Select
Cells(4, 2) = mSheetName
ActiveSheet.Name = mSheetName
Sheets("Form").Visible = False
Sheets("List").Select

End Sub

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
Renaming cells tedious and error prone LurfysMa New Users to Excel 2 December 24th 06 03:58 PM
Renaming Worksheet: Error message: "Reserved Name" thank you! Excel Discussion (Misc queries) 1 September 29th 05 05:01 PM
UDF error when renaming Module1 RonaldF Excel Programming 4 November 27th 04 05:21 PM
Error trap the renaming of a sheet to an existing one Newbie Excel Programming 4 April 14th 04 07:46 AM
Renaming the Worksheet through VBA abxy[_24_] Excel Programming 4 February 9th 04 07:32 PM


All times are GMT +1. The time now is 01:20 AM.

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"