试图find一个使用string的许多变种的目录?
我正在使用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
有什么办法可以创build2个包含我所有星期数字的string(即01 – 52),并在文件path中随机testing每个字符,如下所示:
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
谢谢
这是一个简单的方法使用Like
:
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
您可以使用Dir
函数遍历与通配符匹配的所有文件:
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
一个命题,因为我不确定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