【问题标题】:"*variable* = Dir" causes error: "Invalid Procedure Call Or Argument" when looping through files“*variable* = Dir”导致错误:循环文件时出现“无效的过程调用或参数”
【发布时间】:2021-06-12 05:57:16
【问题描述】:

我正在尝试制作一个循环遍历文件并将名称查找到 Excel 工作表中的脚本。

Option Compare Text
Option Explicit
Sub SortVerkoops()
Dim verkoop As String, verkoopnoex As String, gbref As String, orderstatusname As String, gbyear As String, dealer As String, rootorderpath As String, orderpath As String, foldername As String
Dim gbrefv As String
Dim orders As Range
Dim orderstatus As String
Dim wb As Workbook

orderstatusname = Dir("D:\reece\Documents\Sort sales orders into folders\Staff\Order status\Order status 2021.xlsx")

orderstatus = "D:\reece\Documents\Sort sales orders into folders\Staff\Order status\Order status 2021.xlsx"
verkoop = Dir("D:\reece\Documents\Sort sales orders into folders\Staff\Verkoops\*")

Workbooks.Open Filename:=orderstatus, ReadOnly:=True

Set wb = Workbooks(orderstatusname)
Set orders = wb.Sheets("UK Orders").Range("$A:$B")

Do While Len(verkoop) > 0
    Debug.Print "##########"
    Debug.Print verkoop
    verkoopnoex = Replace(verkoop, ".docx", "")
    gbref = Trim(Replace(verkoopnoex, "Sales Order", ""))
    gbyear = Left(gbref, InStr(gbref, "-") - 1)
    gbyear = (gbyear & "-####")
    ThisWorkbook.Sheets(1).Range("_gbref") = gbref
    Debug.Print gbrefv
    Debug.Print gbref
    Debug.Print gbyear
    dealer = ThisWorkbook.Sheets(1).Range("_dealer").Value
    Debug.Print dealer
    rootorderpath = ("D:\reece\Documents\Sort sales orders into folders\Staff\Customer orders\" & dealer & "\" & gbyear)
    'foldername = Dir(rootorderpath & "\" & gbref & "*")
    orderpath = (rootorderpath & "\" & foldername)
    Debug.Print foldername
    Debug.Print orderpath
    verkoop = Dir
    Debug.Print verkoop
    Debug.Print "##########"
Loop


End Sub

我唯一遇到的麻烦是当我将变量重新定义为 = Dir 以重置文件时,我收到此错误:

该错误仅在这段代码处于活动状态时显示:

我在尝试排除故障时遇到了麻烦。感谢您的帮助,谢谢!

编辑 11/06/2021:非常感谢 Tim Williams 的解决方案。如果有人想要我的工作代码,请告诉我,我可以清理并提供它。

【问题讨论】:

  • @BigBen 我正在查看所有文件,检查 gbref 对应的经销商,找到确切的路径,然后将文件复制到该文件夹​​并继续下一个。我知道如何进行复制等。只是那一点代码

标签: excel vba


【解决方案1】:

您不能嵌套调用Dir() - 您必须在开始另一个循环之前完成一个循环。

您可以改为这样做:

Sub Tester()

    Dim colFiles As Collection, fName
    
    Set colFiles = DirMatches("C:\Tester\", "*.xls*")
    For Each fName In colFiles
        Debug.Print fName
    Next fName
    
End Sub

'Run a Dir loop on `FolderPath` and return all matches in a collection
'   of filenames (or full path) where filename matches `pattern` 
Function DirMatches(ByVal folderPath As String, pattern As String)
    Dim col As New Collection, f
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
    f = Dir(folderPath & pattern)
    Do While Len(f) > 0
        col.Add f                  'filename only
        'col.Add folderPath & f    'full path
        f = Dir()
    Loop
    Set DirMatches = col
End Function

【讨论】:

  • 我刚刚测试了你的函数并使用了你的循环,它绝对是一种享受。谢谢你,我的好心先生!
猜你喜欢
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多