我有以下代码:
If Sheets("EstimateTemplate").Visible Then
Sheets("EstimateTemplate").Select
ActiveWindow.SelectedSheets.Visible = False
Sheets("Navigation").Select
Else
Sheets("EstimateTemplate").Visible = True
Sheets("Navigation").Select
End If
我怎样才能用“IF SHEET EXISTS”子句包装它
我尝试了以下方法,但没有效果
IF not Sheets("EstimateTemplate") = "" then
If Sheets("EstimateTemplate").Visible Then
Sheets("EstimateTemplate").Select
ActiveWindow.SelectedSheets.Visible = False
Sheets("Navigation").Select
Else
Sheets("EstimateTemplate").Visible = True
Sheets("Navigation").Select
End If
end if
答案1
我从这里找到解决方案作为一个好的:
没有内置函数来实现这个功能。
Function SheetExists(SheetName As String, Optional wb As Excel.Workbook)
Dim s As Excel.Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set s = wb.Sheets(SheetName)
On Error GoTo 0
SheetExists = Not s Is Nothing
End Function
因此你最终会得到:
IF SheetExists("EstimateTemplate") then
If Sheets("EstimateTemplate").Visible Then
Sheets("EstimateTemplate").Select
ActiveWindow.SelectedSheets.Visible = False
Sheets("Navigation").Select
Else
Sheets("EstimateTemplate").Visible = True
Sheets("Navigation").Select
End If
End if