Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a workbook specifying its path

Tags:

excel

vba

Sub openwb()  
    ChDir "E:\sarath\PTMetrics\20131004\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 range on 3rd line. What should I do to open a workbook specifying its path?

like image 752
srt Avatar asked Oct 03 '13 10:10

srt


2 Answers

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
like image 143
Punith GP Avatar answered Oct 03 '22 13:10

Punith GP


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\PTMetrics\20131004\D8 L538-L550 16MY\"
    sFile = sPath & "D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm"

    Set wb = Workbooks.Open(sFile)
End Sub
like image 29
Siddharth Rout Avatar answered Oct 03 '22 13:10

Siddharth Rout