程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了检查 UDF大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决检查 UDF?

开发过程中遇到检查 UDF的问题如何解决?下面主要结合日常开发的经验,给出你关于检查 UDF的解决方法建议,希望对你解决检查 UDF有所启发或帮助;

给定以下格式的示例 Excel 数据:

文件路径 测试
C:\file.txt @H_673_17@=PERSONAl.XLSB!checkExists(A2)
C:\dir\file.txt 真的
C:\fake_dir 错误
C:\fake_dir\file.txt 错误
@H_673_17@真的
  • A1 和 A2 是标题
  • A2 和 A3 中的值是存在的文件路径
  • A4 是一个不存在的目录
  • A5 是一个@H_673_17@不能存在的文件,因为它的目录不存在
  • A6 为空白
  • B2 仅显示我如何调用公式(它保存在我的 PERSONAl.XLSB 项目容器东西)
  • @H_489_65@

    我想运行以下用户定义的公式,名为 checkExists;这使用 DIR 检查给定单元格中的值以查看该目录或文件是否存在,并返回一个布尔值。

    Public Function checkExists(path As String) As Boolean
    
    '   check if String is empty
        If (IsEmpty(path) = falsE) Then
    
    '       check if file/dir exists
            If ((Dir(path,vbnormal + vbDirectory)) <> vbNullString = falsE) Then
    '           String is not empty,and file/dir exists; return True
                checkExists = True
    
            Else
    '           file does not exist; return false
                checkExists = false
            
            End If
        
        Else
    '       String is empty; return false
            checkExists = false
        
        End If
    
    End Function
    

    我正在努力让它识别给定的单元格是空白的——比如这里的 A6。当我需要 TRUE 时,所写的公式会在此处返回 falSE

    我意识到我正在传递一个 String,并且可能还有另一种对象类型我需要传递它,例如 Range 或 Variant...但我不知道如何获得这些使用我的代码的对象类型。我也不知道同时使用 vbnormal(文件)和 vbDirectory(目录)是否会导致问题。我已经查找了诸如 ActiveCell{RangE}.Address 之类的东西,但我卡住了。

    任何帮助将不胜感激。谢谢。

    解决方法

    使用 Dir 检查文件或文件夹是否存在

    使用 Len

    Public Function checkExists(fPath As String) As Boolean
    
    '   check if the length of the String is greater than 0
        If Len(fPath) > 0 Then
    
    '       check if the length of 'Dir' is greater than 0
            If Len(Dir(fPath,vbNormal + vbDirectory)) > 0 Then
    
    '           File/dir exists; return True
                checkExists = True
    
            Else
    '           File does not exist; checkExists is false by default
                'checkExists = false
            
            End If
        
        Else
    '       The length of the String is 0; checkExists is false by default
            'checkExists = false
        
        End If
    
    End Function
    

    简短

    使用 Len

    Public Function checkExistsLen(fPath As String) As Boolean
        If Len(fPath) > 0 Then
            If Len(Dir(fPath,vbNormal + vbDirectory)) > 0 Then
                checkExists = True
            End If
        End If
    End Function
    

    使用 vbNullString""

    Public Function checkExists(fPath As String) As Boolean
        If fPath <> "" Then
            if Dir(fPath,vbNormal + vbDirectory) <> "" Then
                checkExists = True
            End If
        End If
    End Function
    

    大佬总结

    以上是大佬教程为你收集整理的检查 UDF全部内容,希望文章能够帮你解决检查 UDF所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:检查