时间:2016-09-22 10:58:35
Function SheetIsExist(strExcleName As String, strSheetName As String) As Boolean
'//判断名称的工作表是否已经在指定的Excel文件中存在
Dim shtSheet As Worksheet
SheetIsExist = False
On Error GoTo lab1
Set shtSheet = Workbooks(strExcleName).Sheets(strSheetName)
If shtSheet Is Nothing Then
SheetIsExist = False
Else
SheetIsExist = True
End If
Set shtSheet = Nothing
Exit Function
lab1:
SheetIsExist = False
End Function
精简后:
Function SheetIsExist(strExcleName As String, strSheetName As String)
'判断名称的工作表是否已经在指定的Excel文件中存在
On Error Resume Next
Dim shtSheet As Worksheet
Set shtSheet = Workbooks(strExcleName).Worksheets(strSheetName)
SheetIsExist = (Err = 0)
End Function
引用
Set N1 = New 类1 '实例化类
MsgBox N1.SheetIsExist(文件名, SheetName) '为真就是存在喽
Workbooks 用于当前所有打开的文件
1 |
|
明白Workbooks 是如何使用了.