【问题标题】:If Len(Dir(ActualValue)) = 0 Then code excel vba always return true inside for loopIf Len(Dir(ActualValue)) = 0 Then code excel vba always return true inside for loop
【发布时间】: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 以及tourHow to Ask。您是否搜索过有关检查文件是否存在的现有问题?

标签: vba excel


【解决方案1】:

我认为这是DateFormat = Format("2017-12-11", "yyyy-mm-dd") 这一行,您正在尝试将今天的日期转换为String,在这种情况下使用DateFormat = Format(Date, "yyyy-mm-dd")

此外,获取所有 Excel 文件类型(包括 2003 格式)扩展名(例如 .xls.xlsx.xlsm)的更安全方法是使用 .xl??

最后,在您的代码中无需使用On Error Resume NextGoTo

代码

Option Explicit

Sub CheckFilesinDirDate()

Dim ActualValue As String, SearchValue As String, DateFormat As String
Dim i As Long

For i = 17 To 32           
    DateFormat = Format(Date, "yyyy-mm-dd") ' I think you are trying to get today's date int to a String

    Sheet4.Cells(i, 5).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 & "" & "*.xl??")               
    If Dir(ActualValue) <> "" Then
        Sheet4.Cells(i, 10).Value = "File Saved"
    Else
        Sheet4.Cells(i, 10).Value = "File Missing"
    End If
Next i

Application.ScreenUpdating = True

End Sub

【讨论】:

  • 感谢您的提示!但它仍然不起作用,因为所有迭代都为我转到 else 语句
  • @JoshuaAaron 你需要仔细检查ActualValue 代表路径和文件名与你的文件匹配(我在测试时也遇到了一个错误)
【解决方案2】:

DateFormat = Format("2017-12-11", "yyyy-mm-dd") 毫无意义。
Format 将格式字符串应用于数字或日期。因此,如果我们谈论 dec-11,它应该是:
DateFormat = Format(#12-11-2017#, "yyyy-mm-dd")

此外,您可以在 ActualValue 计算之后添加 Debug.Print ActualValue,以查看它是否返回了您期望的结果。 (一定要按 ctrl+G 显示调试窗口)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多