Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel vba是否尝试使用字符串的许多变体查找目录?_Excel_Vba - Fatal编程技术网

Excel vba是否尝试使用字符串的许多变体查找目录?

Excel vba是否尝试使用字符串的许多变体查找目录?,excel,vba,Excel,Vba,我正在使用vba尝试定位一个文件,该文件的名称每隔几天就会发生轻微的更改。此文件存储在本地intranet系统上 文件名始终为: Food Specials Rolling Depot Memo xx - xx.xlsm 其中xx可以表示不同的周数,例如 03 - 21 22 - 52 etc.. 因此,有一天,该文件可能会如下所示: Food Specials Rolling Depot Memo 03 - 21.xlsm Food Specials Rolling Depot Memo

我正在使用vba尝试定位一个文件,该文件的名称每隔几天就会发生轻微的更改。此文件存储在本地intranet系统上

文件名始终为:

Food Specials Rolling Depot Memo xx - xx.xlsm
其中xx可以表示不同的周数,例如

03 - 21
22 - 52
etc..
因此,有一天,该文件可能会如下所示:

Food Specials Rolling Depot Memo 03 - 21.xlsm
Food Specials Rolling Depot Memo 22 - 52.xlsm
第二天它可能看起来像:

Food Specials Rolling Depot Memo 03 - 21.xlsm
Food Specials Rolling Depot Memo 22 - 52.xlsm
有没有办法创建两个字符串,其中包含我所有的周数(即01-52),并在文件路径中随机测试每个字符串,如下所示:

     WeekNumber1 = 1,2,3,4,5,6,7,8,9,10 etc.
    WeekNumber2 = 1,2,3,4,5,6,7,8,9,10 etc.



    Set wb = Workbooks.Open(sURL & "Food%20Specials%20Rolling%20Depot%20Memo%20" & WeekNumber1 & "%20-%20" & WeekNumber2 & ".xlsm")

MsgBox wb

On Error GoTo 0

谢谢你的建议,因为我不确定dir()函数在sharepoint/internet站点上是否有效

Sub test()
    On Error Resume Next
    For i = 1 To 52
        For j = i To 52

            Set wb = Workbooks.Open(sURL & "Food%20Specials%20Rolling%20Depot%20Memo%20" & format(i,"00") & "%20-%20" & format(j,"00" & ".xlsm")
            If Not wb Is Empty Then Exit For
        Next j
        If Not wb Is Empty Then Exit For
    Next i
    If wb Is Empty Then
        MsgBox "could not find file"
    Else
        MsgBox "found " & wb.Name
    End If
End Sub
您可以使用该函数迭代与通配符匹配的所有文件:

Dim wildcard as String
Dim fileName as String

' Prepend a path to look in a directory different from the current directory
wildcard = "Food Specials Rolling Depot Memo ?? - ??.xlsm"
fileName = Dir(wildcard)
Do While fileName <> ""
    ' Process fileName
    Set wb = Workbooks.Open(fileName)

    fileName = Dir()
Loop
Dim通配符作为字符串
将文件名设置为字符串
'前置一个路径,以便在不同于当前目录的目录中查找
通配符=“食品特价滚动仓库备忘录???.xlsm”
fileName=Dir(通配符)
文件名“”时执行此操作
'进程文件名
设置wb=工作簿。打开(文件名)
fileName=Dir()
环

下面是一个使用类似
的简单方法:

Sub GetTheName()
    Dim s As String, FileName As String

    s = "C:\TestFolder\*.xlsm"

    FileName = Dir(s)

    Do Until FileName = ""
        If FileName Like "Food Specials Rolling Depot Memo*" Then MsgBox FileName
        FileName = Dir()
    Loop
End Sub

感谢您的帮助,但是,它总是返回未找到的文件:(我认为问题可能是I和j都返回1。数字必须是两位数,即01Edit of last comment:我认为问题可能是I和j都返回1。数字必须是两位数,即01,因为文件名将被写入食品特价滚动仓库备忘录01-19。xlsm etcI编辑了code包含两位数格式。@Bing.Wong我很确定我的代码是按照描述的那样工作的,所以也许您可以详细说明为什么它不适合您?如果
Dir(通配符)
返回空字符串,则没有包含该通配符的文件。问题可能很简单,没有在通配符中指定正确的路径。它似乎一直在返回“Food Specials Rolling Depot Memo???-??.xlsm”一次又一次。@Bing.Wong:这没有任何意义。如果您阅读了链接文档,该函数将按照我的解释清楚地执行。此外,当我尝试运行我提供的确切代码时(没有任何与通配符匹配的文件)按预期返回空字符串。我的代码和环境(Excel 2016)之间必须存在一些差异还有你的。谢谢你的建议。我得到了一个没有错误的循环?有什么想法吗?实际排序。如果在循环之前,则缺少一个结尾。虽然现在代码似乎找不到文件:/not get Anymessage@Bing.Wong请确保包括所有空格在内的文件名拼写正确。这是否在SharePoint网站上?我注意到您<在
.Open
调用中,code>sUrl
,文件名为url编码。