我需要使用具有更改标题的VBA打开文件。文件名称为Membership_Sales_Figures_for_14082017-20082017_104942.xls。 我的代码产生了一个无法找到文件的错误。它将文件称为Weekly Control \ Membership.xls
Sub Test()
Const fpath As String = "Weekly Control\Membership\"
Dim fname As String
Dim lname As String
' Below gives you the Monday of the week
fname = Format(Date - (Weekday(Date) + 6) + 1, "ddmmyyyy")
lname = Format(Date - (Weekday(Date)) + 1, "ddmmyyyy")
fname = "Membership_Sales_Figures_for_" & fname & "-" & lname & "*" & ".xls"
fname = fpath & fname
fname = fpath & Dir(fname)
Dim wb As Workbook
Set wb = Workbooks.Open(fname)
If wb Is Nothing Then MsgBox "File does not exist": Exit Sub
End Sub
答案 0 :(得分:3)
只需使用dir(fname)
查找与您的表达式匹配的文件即可。请注意,它返回第一个找到的文件。
fname = "Membership_Sales_Figures_for_" & fname & "-" & lname & "*" & ".xls"
fname = fpath & fname
fname = fpath & dir(fname)
答案 1 :(得分:0)
您正在寻找此文件。 " Membership_Sales_Figures_for_14082017-20082017_104942.xls"
但是你的代码添加了一个" *"在文件的末尾。
fname = "Membership_Sales_Figures_for_" & fname & "-" & lname & "*" & ".xls"
删除& "*"