I'm trying to compare a name with worksheets that already exist to see if it has been created or not yet.
Private Sub Workbook_Open()
Dim Ldate As String
Dim Lweekday As Integer
Dim Newweek As String
Dim Sname As String
Ldate = Date
Lweekday = Weekday(Ldate)
If Lweekday = 1 Then
Newweek = DateAdd("d", 3, Ldate)
Sname = Month(Newweek) & "-" & Day(Newweek) & "-" & Year(Newweek)
Sheets.Add.Name = Sname
Application.CutCopyMode = False
Worksheets("Template").Select
Range("A1:A2").Copy
Worksheets(Sname).Select
Range("A1").PasteSpecial
Application.CutCopyMode = False
End If
End Sub
Basically I want to check if the worksheet has been created and if it has then exit the code, if not then copy and paste a template in the worksheet.
You can simply add a function to check that.
Private Function IsWorksheetExist(sName As String) As Boolean
Dim oWS As Worksheet
On Error Resume Next
Set oWS = ThisWorkbook.Worksheets(sName)
If oWS Is Nothing Then
IsWorksheetExist = False
Else
IsWorksheetExist = True
End If
On Error GoTo 0
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.