我正在尝试将名称与已存在的工作表进行比较,以查看它是否已创建。
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
基本上我想检查工作表是否已创建,是否已经退出代码,如果没有,则复制并粘贴工作表中的模板。
答案 0 :(得分:0)
您只需添加一个功能即可检查。
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