Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible to run a Stata do-file from within MS Excel?

    Dear all,

    My do-file imports sample data from an MS Excel spreadsheet, then prepares and analyzes the sample data, and finally exports the results to MS Excel by aid of the
    Code:
    putexcel
    command.

    The respective analysis needs to be updated once a month and it would be great if this could be done right from within the Excel spreadsheet, i.e. without having to manually open Stata and then running the do-file there. Is it somehow possible to run a Stata do-file from within MS Excel (i.e. by aid of some VBA code)? Thanks a lot for your thoughts on this.

    Best,
    Ingo




  • #2
    I've never used it, but apparently you can access Stata from within Microsoft applications. At Stata's command line, type
    Code:
    help automation
    and follow the hyperlinks that come up. Again, I don't have any personal experience with it and so I don't know whether what you'll find there can be made to achieve your objective, but it's worth looking into.

    Comment


    • #3
      https://www.stata.com/support/faqs/windows/batch-mode/ describe how to run Stata for Windows in batch mode which seems to be an alternative.

      Comment


      • #4
        Joseph and Bjarte,

        Thanks a lot for your help. On my Windows machine, I finally managed to run a Stata do-file from within Excel by aid of the following VBA script (which I placed in an Excel module):

        Code:
        Sub runStataDoFile()
        
            Dim StataExec, DoFile As String
            Dim wsh As Object
        
            ' Execution of the do-file
            StataExec = """C:\Program Files\Stata17\StataMP-64.exe"""
            DoFile = """<folder of the Stata do-file>\ExcelStata_Test.do"""
            Set wsh = VBA.CreateObject("WScript.Shell")
            wsh.Run StataExec & " /e do " & DoFile
        
        End Sub
        In the above code, <folder of the Stata do-file> refers to the actual folder where the do-file is located.

        Best,
        Ingo

        Comment

        Working...
        X