Open all files in this directory with a specific w

2019-08-07 03:30发布

Its is simple as the title puts it. I need to be able to open files with a folder that have "x.xlsx" but not "*xy.xlsx".

Just not sure how to do it. I can get the the file directory and then use a asterisk to select all files with "x". but i have files that i don't want to open with the only change being that at the end of the file name they have a small amount of text ("y").

this is what i have so far. what would i add.

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

hope this is clear.

2条回答
等我变得足够好
2楼-- · 2019-08-07 03:45

There is something unclear in your question. However look into possible solution below:

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

So, put it inside your loop and it should work.

查看更多
▲ chillily
3楼-- · 2019-08-07 03:59
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
查看更多
登录 后发表回答