本文介绍了如何检查Excel-VBA中是否存在某些表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人知道如何使用Excel VBA检查Excel文档中是否存在某些表单?
Does anyone know how to check whether certain sheets exist or not in an Excel document using Excel VBA?
推荐答案
尽管(不幸的是)这种方法不可用,我们可以创建自己的函数来检查这个..
Although (unfortunately) such method is not available, we can create our own function to check this..
希望下面的代码适合您的需要。
Hope the code below fits your needs.
Edit1:还添加了delete语句...
Added also delete statement...
Sub test()
If CheckSheet(Sheets(3).Name) then
Application.DisplayAlerts = False
Sheets(Sheets(3).Name).Delete
Application.DisplayAlerts = True
End If
End Sub
我要去的解决方案...
The solution I'd go for...
Function CheckSheet(ByVal sSheetName As String) As Boolean
Dim oSheet As Excel.Worksheet
Dim bReturn As Boolean
For Each oSheet In ActiveWorkbook.Sheets
If oSheet.Name = sSheetName Then
bReturn = True
Exit For
End If
Next oSheet
CheckSheet = bReturn
End Function
或者,如果您不介意使用积极引发错误的代码(通常的编码最佳做法不推荐),您可以使用这个 wannabe'code below ...
Alternatively, if you don't mind to use code that actively raise errors (which is not recommended by common coding best practices) you could use this 'Spartan Programming wannabe' code below...
Function CheckSheet(ByVal sSheetName As String) As Boolean
Dim oSheet As Excel.Worksheet
Dim bReturn As Boolean
For Each oSheet In ActiveWorkbook.Sheets
If oSheet.Name = sSheetName Then
bReturn = True
Exit For
End If
Next oSheet
CheckSheet = bReturn
End Function
Function CheckSheet(ByVal sSheetName As String) As Boolean
On Error Resume Next
Dim oSheet As Excel.Worksheet
Set oSheet = ActiveWorkbook.Sheets(sSheetName)
CheckSheet = IIf(oSheet Is Nothing, False, True)
End Function
这篇关于如何检查Excel-VBA中是否存在某些表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!