1

I am looking to write an efficient macro to ultimately save a lot of time when working with research data. Goal is to create one Master workbook with consolidated data.

Here is the situation:

  • I have 40 research subjects that all have an output file titled with their subject number in the format "Subject 1_OUTPUT.txt" (where "1" in the example ranges from 1 to 40).

  • Within each of these output text files, there are ~30 columns of subject-specific data... and the column headers are the same, and in the same order, between all 40 subject files (ex: Column A is titled "OutputDataObject1" in Subject 1's file, Subject 2's file, etc.).

Ultimate Goal: Create one Master Excel file that has a tab named for each column header from the subject files (ex: OutputDataObject1, OutputDataObject2, etc.) and within each tab, a column for each Subject with their data listed within the column. Therefore, each tab would have apples-to-apples data for all 40 subjects on one tab.

Logic for the Macro:

  • Open each Subject txt file (Subject #1 through Subject #40)
  • Copy each column from the subject txt file to tab on the Master Workbook with the same name for the data object... and into the appropriate Master column on that tab, depending on the subject number (Subject 1 through 40)

I know this is do-able but I am struggling with how to create nested loops to open, match the column to the tab-name, and then find the subject's specific column to paste into the Master workbook.

UPDATED 10-16-15: I RESOLVED THE ISSUE WITH MY LOOP - BELOW WITH WORKING CODE :)

Dim MasterWB As Workbook
Dim OpenWB As Workbook
Dim cur_subjectname As String
Dim cur_filename As String
Dim cur_source_column As Integer 
Dim cur_subject_number As Integer 
Dim MasterWB_sheetcount As Integer 'to cycle through MasterWB
Dim cur_sheetnumber As Integer 'to cycle through MasterWB
Dim S As Integer
Dim I As Integer 'to cycle through MasterWB
Dim cur_subjectoffset As Integer
Dim Cell As Range

Application.DisplayAlerts = False
Application.ScreenUpdating = False
cur_source_column = 0
cur_subject_number = 1 'tied to master WB column #
cur_sheetnumber = 3 'first tab to 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 with Subject #1
For S = 1 To 45 'Change for Total # of Subjects
    cur_filename = ThisWorkbook.Path & "/" & cur_subjectname & "_OUTPIV.xlsx" 'Change for different data types
    If Len(Dir(cur_filename)) = 0 Then

    Else 'load data to MasterWB (ThisWorkbook)
        Set OpenWB = Workbooks.Open(cur_filename)
        For I = 1 To 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 I
        OpenWB.Close
    End If
    'Reset values and Move to 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
pnuts
  • 58,317
  • 11
  • 87
  • 139
ABay
  • 11
  • 2
  • I figured it out! I'll update my code in the main post in case it helps someone in the future... – ABay Oct 16 '15 at 14:37
  • Thanks for contributing with what you've solved. If you get 15 rep, it would be good practice to post the solution as an answer to your question. Hence, if someone with the same problem finds this post, he'll find the answer as well. http://stackoverflow.com/help/self-answer – Mariano Oct 17 '15 at 02:56

0 Answers0