使用特定的通配符打开此目录中的所有文件

标题所说的很简单。 我需要能够打开具有“ x .xlsx”而不是“* xy.xlsx”的文件夹。

只是不知道该怎么做。 我可以得到文件目录,然后用星号select所有带“x”的文件。 但我有文件,我不想打开唯一的变化是在文件名的末尾他们有less量的文本(“Y”)。

这是我迄今为止。 我会补充什么。

Workbooks.Open (Dir & FileNameStart & "*") 

希望这是明确的。

 Dim f f = Dir(SrcPath & "*x*.xlsx", vbNormal) Do While Len(f)>0 If not f like "*y.xlsx" Then Workbooks.open SrcPath & f '... end if f = Dir() Loop 

你的问题有一些不清楚的地方。 不过请看下面的解决办法:

 Dim tmp 'this will run inside loop- so, start your loop somewhere here tmp = Dir() 'if file name has "y" and has no "yyx" then... If InStr(1, tmp, "y", vbTextCompare) <> 0 And _ InStr(1, tmp, "yyx", vbTextCompare) = 0 Then '...you will open it Workbooks.Open tmp End If 

所以,把它放在你的循环,它应该工作。