VBA里,workbook.path不是全名,而workbook.fullname才是。而file.path就是全名,没有file.fullname

1 VBA 和 Application--Excel下的命名系统 

2.1 workbooks 工作簿有fullname属性

fullname= path+ "\" + name

  • ThisWorkbook.FullName     文件全名
  • ThisWorkbook.Path             文件路径
  • ThisWorkbook.Name           文件名
  • ThisWorkbook.FullName  =   ThisWorkbook.Path  + "\"  +  ThisWorkbook.Name

2 但是 fso下的file 没有fullname 属性,file.path 就是完整名字了

  • file.FullName     错误,没这个fullname属性
  • file.Path             文件路径,就是文件全名
  • file.Name           文件名

Sub test_wb11()
'比较wb的名字  和 一般file的名字


Dim fso1 As Object
Dim fd1 As Object

Set fso1 = CreateObject("scripting.filesystemobject")
Set fd1 = fso1.GetFolder(ThisWorkbook.Path)

For Each i In fd1.Files
    If i Like "*.xlsm" Then
         Debug.Print i.Name
         Debug.Print i.Path    'workbook工作簿的名字不一样
         Debug.Print i.Path & "\" & i.Name    '这样做重复而多余
'         Debug.Print i.FullName   '会报错
    End If
Next
Debug.Print ""

For Each j In Workbooks
    Debug.Print j.Name
    Debug.Print j.Path
    Debug.Print j.FullName             'wb有fullname属性
    Debug.Print j.Path & "\" & j.Name  'wb工作簿的fullname=path+ "" + name 是有意义的
    
Next

End Sub
发布了388 篇原创文章 · 获赞 45 · 访问量 10万+

猜你喜欢

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