Alt+F11插入下面的代码到模块
'查找某个文件是否存在
Function IsExistFile(ByRef strDir As String, ByRef fileName As String)
Dim s As String
If (Right(strDir, 1) <> "\") Then
strDir = strDir & "\"
End If
s = dir(strDir & fileName, vbArchive + vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
If (s <> "") Then
IsExistFile = fileName
Else
IsExistFile = "无"
End If
End Function
参数说明strDir为文件夹路径,fileName为文件名
使用示例:某个单元格输入=IsExistFile("E:\doc",B2) 即可,若不存在返回无,存在返回B2中的文件名称。路径和文件名可以直接用字符串或引用某个单元格内容。
追问直接复制这代码到宏里面就行了吗,在这代码需要加入文件夹名字不!我加入宏后,在A1输入文件名,在B1输入=IsExistFile("D:\TEST",A1),不行 显示#name?
追答右击VBA Project ,插入模块。代码拷贝到模块中。
本回答被提问者采纳