【发布时间】:2015-06-08 12:33:34
【问题描述】:
我对 VBA 没有任何了解。我有包含文件路径的 excel 文件,我想在该位置找到文件的存在。
我尝试了以下方法,但需要比这更好的方法
Sub Test_File_Exist_With_Dir()
Dim FilePath As String
Dim TestStr As String
FilePath = ActiveSheet.Range("A7").Value
TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
ActiveSheet.Range("B7").Value = 0
Else
ActiveSheet.Range("B7").Value = 1
End If
End Sub
预期输出
File path Existence
C:\Users\Desktop\Excel\Jan15.txt 1
C:\Users\Desktop\Excel\Feb15.txt 1
C:\Users\Desktop\Excel\Mar15.txt 1
C:\Users\Desktop\Excel\Apr15.txt 0
C:\Users\Desktop\Excel\May15.txt 0
如果我向数据添加新行,那么它的存在应该会自动填充。
【问题讨论】: