繁体   English   中英

此工作簿文件夹中的VBA打开文件,知道名称的一部分

[英]VBA Open File From This Workbook's Folder Knowing Part of The Name

我正在尝试从与主工作簿相同的文件夹中打开文件。 问题在于名称不是永久性的,名称中只有一个词始终存在-“ NAME”。

我想对Thisworkbook.Path使用特定的方法来打开xlsx文件,但找不到包含代码的工作簿。

那是代码的相关部分:

Sub RemoveDuplicats()

Dim Harel As Workbook
Dim SAP As Workbook
Dim Path As String
Dim Found As String

Path = ThisWorkbook.Path
Found = Dir(Path & "*NAME*.xlsx")   'open SAP report
If Found <> "" Then
    Set SAP = Workbooks.Open(Path & Found)
End If

End Sub

ThisWorkbook.Path返回不带反斜杠的路径,请尝试

Found = Dir ( Path & "\" & "*NAME*.xlsx")

您需要遍历此文件夹中的所有文件,并像这样比较文件名:

昏暗的StrFile作为字符串

    StrFile = Dir(ThisWorkbook.Path & "\*" & ".xlsm")
    Do While Len(StrFile) > 0
        If StrFile Like "*Name*" Then
            MsgBox StrFile 'This will be your File

        End If
        StrFile = Dir
    Loop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM