excel - Open various text files and copy columns into master workbook -
i looking write efficient macro save lot of time when working research data. goal create 1 master workbook consolidated data.
here situation:
i have 40 research subjects have output file titled subject number in format "subject 1_output.txt" (where "1" in example ranges 1 40).
within each of these output text files, there ~30 columns of subject-specific data... , column headers same, , in same order, between 40 subject files (ex: column titled "outputdataobject1" in subject 1's file, subject 2's file, etc.).
ultimate goal: create 1 master excel file has tab named each column header subject files (ex: outputdataobject1, outputdataobject2, etc.) , within each tab, column each subject data listed within column. therefore, each tab have apples-to-apples data 40 subjects on 1 tab.
logic macro:
- open each subject txt file (subject #1 through subject #40)
- copy each column subject txt file tab on master workbook same name data object... , appropriate master column on tab, depending on subject number (subject 1 through 40)
i know do-able struggling how create nested loops open, match column tab-name, , find subject's specific column paste master workbook.
updated 10-16-15: resolved issue loop - below working code :)
dim masterwb workbook dim openwb workbook dim cur_subjectname string dim cur_filename string dim cur_source_column integer dim cur_subject_number integer dim masterwb_sheetcount integer 'to cycle through masterwb dim cur_sheetnumber integer 'to cycle through masterwb dim s integer dim integer 'to cycle through masterwb dim cur_subjectoffset integer dim cell range application.displayalerts = false application.screenupdating = false cur_source_column = 0 cur_subject_number = 1 'tied master wb column # cur_sheetnumber = 3 'first tab populate subject data cur_subjectoffset = 0 'to move down "subject_list" tab of subjects set masterwb = thisworkbook masterwb_sheetcount = masterwb.worksheets.count - 3 masterwb.worksheets("subject_list").activate 'first subject number in a2 cur_subjectname = sheets("subject_list").range("a1").offset(cur_subject_number, 0).value 'outside loop - open each workbook, starting subject #1 s = 1 45 'change total # of subjects cur_filename = thisworkbook.path & "/" & cur_subjectname & "_outpiv.xlsx" 'change different data types if len(dir(cur_filename)) = 0 else 'load data masterwb (thisworkbook) set openwb = workbooks.open(cur_filename) = 1 masterwb_sheetcount openwb.sheets(1).range("b6:b110").offset(, cur_source_column).copy masterwb.sheets(cur_sheetnumber).range("a2").offset(, cur_subject_number).pastespecial cur_source_column = cur_source_column + 1 cur_sheetnumber = cur_sheetnumber + 1 next openwb.close end if 'reset values , move next subject cur_subject_number = cur_subject_number + 1 cur_source_column = 0 cur_sheetnumber = 3 masterwb.worksheets("subject_list").activate 'first subject number in a2 cur_subjectname = sheets("subject_list").range("a1").offset(cur_subject_number, 0).value next s
Comments
Post a Comment