VBA,判断文件夹内是否是所要文件,如果是则打开,否则不打开

  • file系统的 file 返回的就是绝对路径
  • 只有file.name 才是所谓的文件名
Sub dd1()

Dim fso As Object
Dim fd1 As Object
Dim f1 As Object

Set fso = CreateObject("scripting.filesystemobject")

path1 = ThisWorkbook.Path
Debug.Print path1

fdn1 = fso.getparentfoldername(path1)
Debug.Print fdn1


Set fd1 = fso.getfolder(path1)
For Each f1 In fd1.Files
    If f1.Name Like "*.xlsm" Then
       Debug.Print "f1=" & f1
       Debug.Print "f1.Name=" & f1.Name
    End If
Next

Application.Wait (Now + TimeValue("00:00:05"))

For Each f1 In fd1.Files
    If f1.Name = "test1.xlsm" Then
       Workbooks.Open (f1)
    End If
Next

End Sub

发布了370 篇原创文章 · 获赞 45 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/xuemanqianshan/article/details/103811975