VBA根据文件名的第一部分打开excel工作簿? [英] VBA open excel workbook based on first part of file name?

查看:279
本文介绍了VBA根据文件名的第一部分打开excel工作簿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vba打开一个excel工作簿:

 '打开计划器
错误恢复下一个
设置WB =工作簿((文件名).xlsx)
打开错误GoTo 0
如果WB为Not Then然后打开工作簿如果未打开
设置WB = Workbooks.Open (G:\BUYING\Food Specials\6。Depot Memos\& ThisWorkbook.Worksheets(1).Range(T8)。value&\(File Name).xlsx密码:=samples,WriteResPassword = =samples,UpdateLinks = = False)
如果

我的问题是我的文件不时更改名称:

 仓库备注12  -  13 
仓库备注13 - 14
仓库备注15 - 16

我如何打开该文件基于其文件名Depot Memo的第一部分?
我是VBA全新的,所以我真的很感激任何帮助。



提前感谢

解决方案

根据 here (喊出@Alex K。):



只需替换您的代码部分,如下所示:

 '打开计划器
'这是你的例程
错误恢复下一个
设置WB =工作簿((FILE NAME).xlsx)
错误GoTo 0
如果WB是Nothing然后打开工作簿如果没有打开
设置WB = FindDepotMemo
结束如果
'和那里你需要另一个检查,如果WB是没有

并将此函数添加到您的项目中:

 函数FindDepotMemo()作为工作簿
Dim Path As String
Dim FindFirstFile As String

路径=G:\BUYING\Food Specials\6。仓库备忘录 &安培; ThisWorkbook.Worksheets(1).Range(T8)。Value& \

FindFirstFile = Dir $(Path&Depot Memo * .xlsm)

If(FindFirstFile<> vbNullString)然后
设置FindDepotMemo = Workbooks.Open(文件名:= Path& FindFirstFile,Password:=samples,WriteResPassword:=samples,UpdateLinks = = False)
End If
End Function


I am trying to open an excel workbook using vba:

'Open Planner
On Error Resume Next
Set WB = Workbooks("(FILE NAME).xlsx")
On Error GoTo 0
If WB Is Nothing Then 'open workbook if not open
    Set WB = Workbooks.Open("G:\BUYING\Food Specials\6. Depot Memos\" & ThisWorkbook.Worksheets(1).Range("T8").value & "\(File Name).xlsx", Password:="samples", WriteResPassword:="samples", UpdateLinks:=False)
End If

The problem i have is my file changes name from time to time like so:

Depot Memo 12 - 13
Depot Memo 13 - 14
Depot Memo 15 - 16

How can i open the file based on the first part of its filename 'Depot Memo'? I'm brand new to VBA so i'd really appreciate any help.

Thanks in advance

解决方案

Try this example based on answer from here (Shout-out to @Alex K.):

Just replace your code part like this:

'Open Planner
'This is your routine here
On Error Resume Next
Set WB = Workbooks("(FILE NAME).xlsx")
On Error GoTo 0
If WB Is Nothing Then 'open workbook if not open
    Set WB = FindDepotMemo
End If
'and there you need another check if WB is nothing

And add this function to your project:

Function FindDepotMemo() As Workbook
    Dim Path As String
    Dim FindFirstFile As String

    Path = "G:\BUYING\Food Specials\6. Depot Memos\" & ThisWorkbook.Worksheets(1).Range("T8").Value & "\"

    FindFirstFile = Dir$(Path & "Depot Memo*.xlsm")

    If (FindFirstFile <> vbNullString) Then
        Set FindDepotMemo = Workbooks.Open(Filename:=Path & FindFirstFile, Password:="samples", WriteResPassword:="samples", UpdateLinks:=False)
    End If
End Function

这篇关于VBA根据文件名的第一部分打开excel工作簿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆