Announcement

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

  • How to Create a Copy of an existing Excel file preserving the formatting, using Stata?

    Hi,

    I use Stata (putexcel and Mata) to create a heavily formatted Excel spreadsheet that I update automatically by running the same .do file. I'm interested in creating an identical copy of the spreadsheet in a different location, with a different name, automatically every time this .do file is run. I can see some ways of accomplishing the same thing using Excel itself or rewriting my Stata code to loop, but since I already have the spreadsheet I really would like to just create the copy, while preserving the formatting. Putexcel, it seems, can only open and close the file in the same place. I don't see a way to save a copy of the same Excel file opened in memory, or save it somewhere else with a different name. I want to preserve the formatting so not trying to use import/export excel.

    Does anybody know a way to do this specific thing? I understand this is probably not a super common way to do this. Thank you for any advice!

  • #2
    Stata has a command named copy that you can use to make copies of files, see [D] copy.

    Comment


    • #3
      If the formatting is working with putexcel already, you can use the copy command to make a copy of the file in a new place. As for the naming part, there are a few ways you can go about this. Probably the easiest way would to be just attach a timestamp (see below code). Let's say the original formatted file is saved as "formatted_original.xlsx".

      Code:
      local curr_excel = "run_" + string(clock(c(current_date) + " " + c(current_time), "DMYhms"), "%tcDD!_NN!_CCYY!_HH!_MM!_SS") + ".xlsx"
      copy "formatted_original.xlsx" "`curr_excel'", public
      That'll copy the original to a new file called "run_08_01_2026_15_31_26.xlsx".

      Comment


      • #4
        Thanks Jeff and Brian! Perfect answers and the copy command was exactly what I needed. Amazing.

        Comment

        Working...
        X