Announcement

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

  • Error opening Excel after using merge option on -putexcel- command

    I'm trying to use the -putexcel- command to create tables with a header at the top. I found the -tab2xl- program on the Stata website and that suits what I need for this project nicely. However, when I run the code below and then open I get an error telling me that there is an error (which Excel is kind enough to help me recover from) and then further that the error is related to the merge command below. Ideally I would like the grey box to extend over the top of the table and have the variable label displayed nicely. However, when I run the code, it still seems to only be the A column cell, rather than in the merged A:D cell. I'm using Excel 2019 and Stata 16.

    Click image for larger version

Name:	Error 1.png
Views:	1
Size:	7.1 KB
ID:	1655102



    Click image for larger version

Name:	Error 2.png
Views:	1
Size:	14.6 KB
ID:	1655103


    Code:
    *net install http://www.stata.com/users/kcrow/tab2xl, replace
    sysuse auto, clear
    replace turn = round(turn,5)
    
    local row = 2
    local empty = `row' + 1
    putexcel set "AutoData.xlsx", modify sheet(Tabs)
    putexcel describe
    foreach var of varlist rep78 headroom turn {
        quietly: tab2xl `var' foreign using "AutoData.xlsx", row(`row') col(1) sheet(Tabs)
        putexcel describe
        quietly: tab `var' foreign, m
        local Varlab : variable label `var'
        display in red "Varlab: `Varlab'"
        quietly: putexcel B`row'="" 
        quietly: putexcel (A`row':D`row'), merge 
        quietly: putexcel (A`row'), hcenter border(all) fpattern(solid, gray)
        quietly: putexcel A`row'="`Varlab'", font(Calibri, 12, white)
        quietly: putexcel A`empty'="" 
        
        local row = `row' + `r(r)' + 5
        local empty = `row' + 1
    }
    putexcel save
    Click image for larger version

Name:	Office.png
Views:	1
Size:	7.0 KB
ID:	1655101



  • #2
    The problem seems to center on the fact that tab2xls has already merged cells B and C in the row in question. The following produces presentable results without generating an error.
    Code:
    *net install http://www.stata.com/users/kcrow/tab2xl, replace
    sysuse auto, clear
    replace turn = round(turn,5)
    
    local row = 2
    local empty = `row' + 1
    capture noisily erase AutoData.xlsx
    putexcel set "AutoData.xlsx", modify sheet(Tabs)
    putexcel describe
    foreach var of varlist rep78 headroom turn {
        quietly: tab2xl `var' foreign using "AutoData.xlsx", row(`row') col(1) sheet(Tabs)
        putexcel describe
        quietly: tab `var' foreign, m
        local Varlab : variable label `var'
        display in red "Varlab: `Varlab'"
        // quietly: putexcel B`row'="" 
        // quietly: putexcel (A`row':D`row'), merge 
        quietly: putexcel (B`row'), hcenter border(all) fpattern(solid, gray)
        quietly: putexcel B`row'="`Varlab'", font(Calibri, 12, white)
        quietly: putexcel B`empty'="" 
        
        local row = `row' + `r(r)' + 5
        local empty = `row' + 1
    }
    putexcel save

    Comment

    Working...
    X