大佬教程收集整理的这篇文章主要介绍了检查 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@真的 |
我想运行以下用户定义的公式,名为 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,请注明来意。