View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Sheets changing name

The code you have will always copy the Master worksheet because of the
following line.

Set ShtToCopy = Sheets("Master")

If you want it to copy the worksheet you are working on then you need to
copy the ActiveSheet. If you do the following it will fix the problem.

Copy the following code into a standard module. (To create a standard module
when in the VBA editor select Insert - Module.) Standard modules are named
Module1, Module2 etc.

The code is amended to copy the ActiveSheet plus automatically find the next
alpha character for the worksheet name if the name already exists. (No need
for the user to have to enter it.)

Sub CopyWorkSheet()

Dim wsShtToCopy As Worksheet
Dim strNewShtDate As String
Dim strNewShtName As String
Dim wsNewSht As Worksheet
Dim intChr As Integer


Set wsShtToCopy = ActiveSheet

'Assign proposed new worksheet name to variable
strNewShtDate = Format(Date, "mm-dd-yy")

'Copy the date name to another variable
strNewShtName = strNewShtDate

'Test for new sheet name already existing
'by attempting to assign to a variable
intChr = 65 'Initialize Chr for "A"
Do
On Error Resume Next
Set wsNewSht = Sheets(strNewShtName)

'If error is zero then worksheet exists
'therefore append an alpha character and test again
If Err.Number = 0 Then 'No Error
strNewShtName = strNewShtDate & Chr(intChr)
intChr = intChr + 1
Else
'Worksheet does not already exist so exit loop
Exit Do
End If
On Error GoTo 0 'Resume error trapping
Loop

On Error GoTo 0 'Resume normal error trapping

wsShtToCopy.Copy After:=Sheets(1)
ActiveSheet.Name = strNewShtName

End Sub

Now copy the following code into the Master Worksheet code area. Note that
the sub name must match the button name. To find the button name click Design
Mode button on the worksheet, right click the button and select properties.
The button name is the first line of the properties. (Don't forget to turn
off design mode after exiting from the properties.)

Private Sub CommandButton7_Click()
Call CopyWorkSheet
End Sub

--
Regards,

OssieMac