Home > Programming > Retaining an Excel cell’s format when using putexcel

Retaining an Excel cell’s format when using putexcel

In a previous blog entry, I talked about the new Stata 13 command putexcel and how we could use putexcel with a Stata command’s stored results to create tables in an Excel file.

After the entry was posted, a few users pointed out two features they wanted added to putexcel:

  1. Retain a cell’s format after writing numeric data to it.
  2. Allow putexcel to format a cell.

In Stata 13.1, we added the new option keepcellformat to putexcel. This option retains a cell’s format after writing numeric data to it. keepcellformat is useful for people who want to automate the updating of a report or paper.

To review, the basic syntax of putexcel is as follows:

putexcel excel_cell=(expression) … using filename[, options]

If you are working with matrices, the syntax is

putexcel excel_cell=matrix(expression) … using filename[, options]

In the previous blog post, we exported a simple table created by the correlate command by using the commands below.

. sysuse auto
(1978 Automobile Data)

. correlate foreign mpg

             |  foreign      mpg
     foreign |   1.0000
         mpg |   0.3934   1.0000

. putexcel A1=matrix(r(C), names) using corr

These commands created the file corr.xlsx, which contained the table below in the first worksheet.


As you can see, this table is not formatted. So, I formatted the table by hand in Excel so that the correlations were rounded to two digits and the column and row headers were bold with a blue background.


putexcel‘s default behavior is to remove the formatting of cells. Thus, if we want to change the correlated variables in our command from foreign and mpg to foreign and weight using the below commands, the new correlations shown in Excel will revert to the default format:

. sysuse auto, clear
(1978 Automobile Data)

. correlate foreign weight

             |  foreign   weight
     foreign |   1.0000
      weight |  -0.5928   1.0000

. putexcel A1=matrix(r(C), names) using corr, modify


As of Stata 13.1, you can now use the keepcellformat option to preserve a numeric cell’s format when writing to it. For example, the command

. putexcel A1=matrix(r(C), names) using corr, modify keepcellformat

will produce


Let’s look at a real-world problem and really see how the keepcellformat option can help us. Suppose we need to export the following tabulate table to a report we wrote in Word.

. webuse auto2, clear
(1978 Automobile Data)

. label variable rep78 "Repair Record"

. tabulate rep78

     Repair |
     Record |      Freq.     Percent        Cum.
       Poor |          2        2.90        2.90
       Fair |          8       11.59       14.49
    Average |         30       43.48       57.97
       Good |         18       26.09       84.06
  Excellent |         11       15.94      100.00
      Total |         69      100.00

In the previous putexcel blog post, I mentioned my user-written command tab2xl, which exports a one-way tabulation to an Excel file. I have since updated the command so that it uses the new keepcellformat option to preserve cell formatting. You can download the updated tab2xl command by typing the following:

. net install http://www.stata.com/users/kcrow/tab2xl, replace

Using this command, I can now export my tabulate table to Excel by typing

. tab2xl rep78 using tables, row(1) col(1)

Once the table is in Excel, I format it by hand so that it looks like this:


I then link this Excel table to a Word document. When you link an Excel table to a Word document, it

  1. preserves the formatting of the table and
  2. automatically updates the Word document when you update the Excel table.

It is fairly easy to link an Excel table to a Word document or PowerPoint presentation. In Excel/Word 2010, you would do as follows:

  1. Highlight the table/data in Excel.
  2. On the Home tab, click on the Copy button.
  3. Open the Word document and scroll to where you want the table pasted.
  4. On the Home tab of Word, click on the Paste button.
  5. Select Link & Keep Source Formatting, Link, from the Paste icon menu.

My report now looks like this:


With the Excel table linked into Word, any time we update our Excel table using putexcel, we also update our table in Word.

Suppose that after a few weeks, we get more repair record data. We now need to update our report, and our new tabulate table looks like this:

. tabulate rep78

     Repair |
     Record |      Freq.     Percent        Cum.
       Poor |          4        2.90        2.90
       Fair |          8        5.80        8.70
    Average |         60       43.48       52.17
       Good |         44       31.88       84.06
  Excellent |         22       15.94      100.00
      Total |        138      100.00

To update the report, we simply need to reissue the putexcel command after tabulate.

. tabulate rep78
. tab2xl rep78 using tables, row(1) col(1)

The linked Word report will automatically reflect the changes:


Categories: Programming Tags: , , , , ,