Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

excel - How to open a workbook specifying its path

Sub openwb()  
    ChDir "E:sarathPTMetrics20131004D8 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Workbooks.open("E:sarathPTMetrics20131004D8 L538-L550 16MYD8 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:sarathPTMetrics20131004D8 L538-L550 16MY"
    sFile = sPath & "D8 L538-L550_16MY_Powertrain Metrics_20131002.xlsm"

    Set wb = Workbooks.Open(sFile)
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...