【发布时间】:2017-12-12 08:11:20
【问题描述】:
我有一个 excel vba sub 来检查文件是否在上述目录中。问题是它总是在 for 循环的第一次迭代之后转到 File Saved。如果文件确实被找到,ActualValue 的值在其中有一些,但如果它丢失,则值为“”。问题是它总是进入 = 0 或 = "" 条件。
Dim ActualValue As String
For i = 17 To 32
On Error Resume Next
DateFormat = Format("2017-12-11", "yyyy-mm-dd")
Sheet4.Cells(i, 5).Select
Selection.ClearContents
SearchValue = Sheet4.Cells(i, 1).Value
If SearchValue = "" Then
MsgBox "No A/C indicated. Please check.", vbOKOnly
Exit Sub
End If
ActualValue = Dir("A:\123 456\789\abc efg\Sample Folder\SAMPLE FOLDER\" & DateFormat & "\" & SearchValue & "" & "*.xls")
If Len(Dir(ActualValue)) = 0 Then
Sheet4.Cells(i, 10).Value = "File Saved"
Else
Sheet4.Cells(i, 10).Value = "File Missing"
GoTo FileMissing
End If
FileMissing:
Next i
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
你为什么要放 LEN(DIR?不应该只是 Len(ActualValue)
-
@Joshua --
i的值是多少,因此,Sheet4.Cells(i, 1)的值是多少? -
只需将 Len(Dir(ActualValue)) 放入 msgbox 并查看您得到的内容,或者使用本地窗口查看其中的内容
-
这里有广泛的介绍:VBA Check if file exists
-
@Joshua -- 如果您正在检查单个文件是否存在,您应该只使用
DIR函数之一。另外,我假设您的日期实际上并没有像那样硬编码?那i有价值吗?更多代码(特定于您的问题)会有所帮助。查看minimal reproducible example 以及tour 和How to Ask。您是否搜索过有关检查文件是否存在的现有问题?