VBA打开文件从这个工作簿的文件夹知道名称的一部分
我正在尝试从主工作簿的相同文件夹中打开文件。 问题是这个名字不是永久性的,只有一个单词永远留在名字里面 – “NAME”。
我想用Thisworkbook.Path
来使用特定的方法来打开xlsx文件,但它没有find带有代码的工作簿。
这是代码的相关部分:
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返回没有尾部反斜杠的path,尝试
Found = Dir ( Path & "\" & "*NAME*.xlsx")
你将需要循环这个文件夹中的所有Fiels,并像这样比较文件名:
Dim StrFile As String
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