Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72764

Importing many worksheets from workbooks; blank sheets

$
0
0
Hi all,

I'm working on importing the individual sheets from excel workbooks, but some of the sheets have names and are blank, which is breaking my code. I'd like to be able to go around this without using cap and obfuscating any other import errors. Any ideas? I pasted my entire code below (working directories omitted).

Thanks,
Lucia


Code:
local allfiles: dir "$directory" files "*.xlsx", respectcase

* counts number of workbooks
loc n: word count `allfiles'

cd "$directory"

forvalues i = 1/`n' {
    local workbook : word `i' of `allfiles'
    
    qui import excel using "`workbook'", describe 
    gl nwork = r(N_worksheet)
    
    forvalues j = 1/$nwork {
        qui import excel using "`workbook'", describe 
        
        loc sheet = "`r(worksheet_`j')'"                                             
        
        if "`sheet'" != "First_Sheet" {                                            
            import excel using "`workbook'", sheet("`sheet'") firstrow allstring
            gen sheetname = "`sheet'"                                                                 
            gen workbookname = "`workbook'"
            
            if "`i'_`j'" == "1_2" {
                save "$seconddirectory\mastersheet", replace
                }
            
            else {                                                         
                save `i'_`j'.dta
                clear
                use "$seconddirectory\mastersheet"
                append using `i'_`j' 
                save "$seconddirectory\mastersheet", replace
                erase `i'_`j'.dta
            }
        }
    clear
    }
    
    
}

Viewing all articles
Browse latest Browse all 72764

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>