布尔函数拧紧

时间:2017-12-18 18:51:37

标签: excel vba excel-vba

我有一个布尔函数,用于检查是否存在与复选框关联的工作表名称。虽然我有编码可以在错误中阻止复选框,但如果为false,则布尔值会给出错误13。

Function WorksheetExists(sheetName As String) As Boolean
    WorksheetExists = Evaluate("ISREF('" & sheetName & "'!A1)")
End Function

我想要做的是设置它,如果sheetName中没有值,该函数就会结束。这可能吗?

2 个答案:

答案 0 :(得分:2)

您可以测试作为参数传递的值。如果作为sheetName传递的字符串长度为0个字符,则下面将函数设置为False(默认值)

Option Explicit

Sub count()
    Debug.Print getFileCount("\\hhhhh\ttttttt\kkkkkk")
End Sub


Function getFileCount(localRoot, Optional fld, Optional count As Long) As Long
    Dim FileInFolder, x, sh, fileInZip, ZipFile, aFile, fso, f, baseFolder, subFolder, ftpFile, i

    Set fso = CreateObject("Scripting.Filesystemobject")
    Set sh = CreateObject("Shell.Application")

    If IsMissing(fld) Then
        Set baseFolder = fso.GetFolder(localRoot)
    Else
        Set baseFolder = fld
    End If

    count = count + baseFolder.Files.count

    For Each subFolder In baseFolder.SubFolders
        'Count files inside the zip files
        For Each FileInFolder In subFolder.Files
            If Right(FileInFolder.Name, 4) = ".zip" Then

                Set ZipFile = sh.Namespace(FileInFolder.Path)
                count = count + ZipFile.items.count
            End If
        Next FileInFolder

        'Count all files
        getFileCount localRoot, subFolder, count
    Next

    getFileCount = count
End Function

答案 1 :(得分:0)

如果您对性能感兴趣,则运行速度提高约30倍

Function WorksheetExists(sheetName As String) As Boolean
    Dim ws As Worksheet
    On Error GoTo EH
    Set ws = ActiveWorkbook.Worksheets(sheetName)
    WorksheetExists = True
Exit Function
EH:
    WorksheetExists = False
End Function