Sub openwb()
ChDir "E:\sarath\PTMetrics\20131004\D8 L538-L550 16MY"
Workbooks("D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm").Open
End sub
在这里,我Subscript out of range
在第 3 行收到一条错误消息。我应该怎么做才能打开指定其路径的工作簿?
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
您还可以通过提示打开所需的文件,这有助于您从不同的路径和不同的文件中选择文件。
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