vba 如何打开指定其路径的工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19157385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:48:01  来源:igfitidea点击:

How to open a workbook specifying its path

excelvba

提问by srt

Sub openwb()  
    ChDir "E:\sarath\PTMetrics131004\D8 L538-L550 16MY"
    Workbooks("D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm").Open    
End sub

Here, I am getting an error saying Subscript out of rangeon 3rd line. What should I do to open a workbook specifying its path?

在这里,我Subscript out of range在第 3 行收到一条错误消息。我应该怎么做才能打开指定其路径的工作簿?

回答by Siddharth Rout

Workbooks.open("E:\sarath\PTMetrics\20131004\D8 L538-L550 16MY\D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm")

Workbooks.open("E:\sarath\PTMetrics\20131004\D8 L538-L550 16MY\D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm")

Or, in a more structured way...

或者,以更结构化的方式......

Sub openwb()
    Dim sPath As String, sFile As String
    Dim wb As Workbook

    sPath = "E:\sarath\PTMetrics131004\D8 L538-L550 16MY\"
    sFile = sPath & "D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm"

    Set wb = Workbooks.Open(sFile)
End Sub

回答by Punith GP

You can also open a required file through a prompt, This helps when you want to select file from different path and different file.

您还可以通过提示打开所需的文件,这有助于从不同路径和不同文件中选择文件。

Sub openwb()
Dim wkbk As Workbook
Dim NewFile As Variant

NewFile = Application.GetOpenFilename("microsoft excel files (*.xlsm*), *.xlsm*")

If NewFile <> False Then
Set wkbk = Workbooks.Open(NewFile)
End If
End Sub