View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default If Sheet Exists Q

Probably because the sub SheetExists does not exist!

Add this to your code

'-----------------------------------------------------------------
Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Function


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Sean" wrote in message
...
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