View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default If Sheet Exists Q

Sean,

You can't invent a function like SheetExists, you must write it or do
something different. Hers an alternative:-

For Each WS In Worksheets
If WS.Name = newShtName Then flg = True: Exit For
Next
If flg = True Then
Else
Sheets.Add.Name = newShtName
End If

If you include this you'll then find several other errors in your code
You try to rename a sheet to the same name as an existing sheet.
You try to select the 'Log' worksheets after you have just hidden it.
You can't do either of those things.

Mike

Mike

"Sean" wrote:

I have the following code, that is trying to copy the sheet Log, right
of the active sheet but only IF it does not exist already.

I am getting a Sub not defined on line "If SheetExists(newShtName)
Then" and don't know why.

The sheet who's value in A5 is 09/03/08 does not actually exist, so it
should copy.

Any help appreciated

Sub CreateYTD_PostLog()
Dim shtName As String
Dim newShtName As String
Dim WS As Worksheet
Application.ScreenUpdating = False

ActiveWindow.DisplayHeadings = False

For Each WS In ThisWorkbook.Worksheets
WS.Unprotect Password:="123"
Next WS

Sheets("Log").Activate
shtName = ActiveSheet.Name
newShtName = Format([a5], "dd-mm-yy")

If SheetExists(newShtName) Then
MsgBox "You have already created this week.", vbCritical
Exit Sub
End If
ActiveSheet.Copy after:=ActiveSheet
ActiveSheet.Name = newShtName
ActiveSheet.Tab.ColorIndex = -4142
On Error Resume Next
ActiveSheet.DrawingObjects.Visible = True
ActiveSheet.DrawingObjects.Delete
On Error GoTo 0
Sheets(shtName).Activate

Sheets("Log").Select
ActiveSheet.Unprotect Password:="123"
range("A1").Select

ActiveWindow.SelectedSheets.Visible = False

Sheets("Log").Select
range("A1").Select
End Sub