Sub openwb()
ChDir "E:\sarath\PTMetrics\20131004\D8 L538-L550 16MY"
Workbooks("D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm").Open
End sub
在这里,我在第3行收到错误Subscript out of range
。如何打开指定其路径的工作簿?
答案 0 :(得分:17)
Workbooks.open("E:\sarath\PTMetrics\20131004\D8 L538-L550 16MY\D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm")
或者,以更有条理的方式......
Sub openwb()
Dim sPath As String, sFile As String
Dim wb As Workbook
sPath = "E:\sarath\PTMetrics\20131004\D8 L538-L550 16MY\"
sFile = sPath & "D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm"
Set wb = Workbooks.Open(sFile)
End Sub
答案 1 :(得分:2)
您还可以通过提示打开所需文件。当您要从不同路径和不同文件中选择文件时,这会有所帮助。
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