title: Execute .bat file from Excel VBA Macro
body: <p>Im having a problem with my excel vba macro. I need it to execute a batch file which is in the same folder as the excel workbook. The code works well sometimes. I don't know whats causing the error. Here's the code:</p>
  
  <pre><code>Sub writebatch()
    Sheets("code").Select
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=ThisWorkbook.path &amp; "\code.bat",
    FileFormat:=xlTextPrinter, CreateBackup:=False
    Application.DisplayAlerts = True
    ThisWorkbook.Saved = True
    Shell "cmd.exe /k cd " &amp; ThisWorkbook.path &amp; "&amp;&amp;code.bat"
    Application.Quit
  End Sub
  </code></pre>
  
  <p>It writes the batch file, but then doesn't execute it. Only once I got the command window to not close and it said the code.bat file could not be found. So the changedir command worked. Is it possible to run cmd.exe and run the code.bat with the relative path without having to changedir?</p>