vba - How to aggregate/compile multiple Excel spreasheets (.csv) into separate sheets in one workbook? -
i not programmer - need/want write command in excel aggregate multiple .csv files separate sheets in 1 workbook... runs once, , copy/paste contents of 1 .csv file errors out error:
runtime error '438': object not support property or method. i've narrowed down line:
'paste thisworkbook.worksheets(sheets.count).range("a1").paste but, being new of this, unsure do. far have been grabbing seemed applicable code snippets around web.
private sub commandbutton1_click() dim strfile string, strpath string dim wkb workbook 'change path own file location: strpath = "c:[file path here]" 'this returns empty string "" if file cannot found , error if folder incorrect strfile = dir(strpath & "*.csv") while strfile <> "" 'open csv file , assign variable can reference later set wkb = workbooks.open(strpath & strfile) 'add new worksheet @ end of macro workbook paste thisworkbook.sheets.add after:=sheets(sheets.count) 'get range , copy wkb.sheets(1).usedrange.copy debug.print (sheets.count) 'paste thisworkbook.worksheets(sheets.count).range("a1").paste 'close csv file wkb.close 'find next file - dir without parameters next file in folder matches first dir call strfile = dir loop end sub
sub dougsloop() dim wbk workbook dim filename string dim path string dim wso workbook dim starttime double dim secondselapsed double dim arr variant dim rowc long dim colc long application.screenupdating = false application.displayalerts = false application.calculation = xlcalculationmanual starttime = timer path = "path folder of files" & "\" filename = dir(path & "*.csv??") set wso = thisworkbook wso.sheets(1).select while len(filename) > 0 doevents set wbk = workbooks.open(path & filename, true, true) arr = wbk.sheets(1).usedrange rowc = wbk.sheets(1).usedrange.rows.count colc = wbk.sheets(1).usedrange.columns.count wso.activesheet.range(wso.activesheet.cells(1, 1), wso.activesheet.cells(rowc, colc)).value2 = arr wbk.close false filename = dir wso.sheets.add after:=worksheets(worksheets.count) loop application.screenupdating = true application.displayalerts = true application.calculation = xlcalculationautomatic secondselapsed = round(timer - starttime, 2) msgbox "this code ran in " & secondselapsed & " seconds", vbinformation end sub
Comments
Post a Comment