Changing working directory from Excel vba shell

前端 未结 4 1718
广开言路
广开言路 2020-12-11 19:43

For work, I\'m trying to run a Python script from an Excel VBA macro.

The macro is as follows -

Sub Plot()

Shell \"C:\\Users\\maheshda\\AppData\\Lo         


        
4条回答
  •  有刺的猬
    2020-12-11 20:09

    If your behavior is to open an excel window and then open your recent file, please note that you should not forget to add change Drive and then change Directory into your VBA code.

    Cause the Excel always start with the default Directory even it's just open your recent file !

    Dim ThisWorkbookPath As String
    Dim ThisWorkbookPathParts As Variant
    
    ThisWorkbookPath = ThisWorkbook.Path
    ThisWorkbookPathParts = Split(ThisWorkbookPath, _
                            Application.PathSeparator)
    
    ChDrive ThisWorkbookPathParts(LBound(ThisWorkbookPathParts))
    ChDir ThisWorkbookPath
    

提交回复
热议问题