+ Reply to Thread
Results 1 to 4 of 4

loop through files find columns and copy/paste them as columns in master workbook

  1. #1
    Registered User
    Join Date
    07-16-2018
    Location
    Wroclaw,Poland
    MS-Off Ver
    2016
    Posts
    7

    loop through files find columns and copy/paste them as columns in master workbook

    Hi everyone!

    Here is what im attempting to do in vba:

    select folder with files
    for each workbook in the folder
    for each worksheet
    if column = "Field" or "Table" with title background filled with orange
    copy and paste into master workbook as column
    for each copied column paste with header containing column name + workbook name + worksheet name
    close all workbooks except master
    save master

    I've found something similar to what I need, but it will not work properly - the code will only copy first instance of column from first worksheet, so only one column named "Field" will be copied isntead of every instance. I need to get all columns named Field and Table from each worksheet of each workbook and store them in one worksheet as separate columns side by side with headers containing info of their origin:

    Sub Main()
    On Error Resume Next
    Application.ScreenUpdating = False
    'THE MASTER FILE MUST BE SAVED IN THE FILE FOLDER
    'ALONG WITH THE WORKBOOKS OF INTEREST
    Dim MyTempWB As Workbook
    Dim WS As Worksheet
    'INFORMATION ABOUT YOUR FILE AND FOLDER
    Dim MyWB As Workbook
    Set MyWB = ActiveWorkbook
    ThePath = MyWB.Path
    MyWorkBookName = MyWB.Name
    Sheet1.Cells(1, 1).Value = "Field"
    'LOOP THROUGH ALL FILES EXCEPT THE MASTER
    vPath = ThePath & "\*.xls"
    Filename = Dir(vPath)
    Do While Filename <> ""
    If Filename = MyWorkBookName Then GoTo SkipThisFile
    'OPEN NEXT FILE
    Workbooks.Open (CStr(ThePath & "" & Filename))
    Set MyTempWB = ActiveWorkbook
    'STEP THROUGH EACH SHEET IN THE FILE
    With MyTempWB
    For I = 1 To CInt(MyTempWB.Sheets.Count)
    'SEARCH THE SHEET FOR VALUE ItemID AND XItemID
    Set ItemIDColumn = MyTempWB.Sheets(I).Cells.Find("ItemID", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns)
    If Not ItemIDColumn Is Nothing Then
    FirstRow = MyTempWB.Sheets(I).Cells(ItemIDColumn.Row + 1, ItemIDColumn.Column).Row
    LastRow = MyTempWB.Sheets(I).Cells(Rows.Count, ItemIDColumn.Column).End(xlUp).Row
    Range(MyTempWB.Sheets(I).Cells(FirstRow, ItemIDColumn.Column), MyTempWB.Sheets(I).Cells(LastRow, ItemIDColumn.Column)).Copy
    MyWB.Activate
    Sheet1.Cells(Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Select
    ActiveSheet.Paste
    MyTempWB.Activate
    End If
    Set XItemIDColumn = MyTempWB.Sheets(I).Cells.Find("XItemID", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns)
    If Not XItemIDColumn Is Nothing Then
    FirstRow = MyTempWB.Sheets(I).Cells(XItemIDColumn.Row + 1, XItemIDColumn.Column).Row
    LastRow = MyTempWB.Sheets(I).Cells(Rows.Count, XItemIDColumn.Column).End(xlUp).Row
    Range(MyTempWB.Sheets(I).Cells(FirstRow, XItemIDColumn.Column), MyTempWB.Sheets(I).Cells(LastRow, XItemIDColumn.Column)).Copy
    MyWB.Activate
    Sheet1.Cells(Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Select
    ActiveSheet.Paste
    MyTempWB.Activate
    End If
    Next I
    End With
    'CLOSE THE FILE
    MyTempWB.Close
    SkipThisFile:
    Count = Count + 1
    Filename = Dir()
    Loop
    'AT THIS POINT EVERYTHING HAS BEEN MOVED
    'NOW LETS LOOP BACK THROUGH AND REMOVE YOUR N/A & BLANK VALUES
    MyWB.Activate
    For I = 1 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    If IsError(Sheet1.Cells(I, 1).Value) Then Sheet1.Cells(I, 1).EntireRow.Delete
    If Sheet1.Cells(I, 1).Value = "" Then Sheet1.Cells(I, 1).EntireRow.Delete
    If Sheet1.Cells(I, 1).Value = "#N/A" Then Sheet1.Cells(I, 1).EntireRow.Delete
    Next I
    Application.ScreenUpdating = True
    On Error GoTo 0
    MyWB.Save
    End Sub

    Im frustrated beyond belief, as it seems to be so close to a solution and yet so far away.

    Thank you in advance.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: loop through files find columns and copy/paste them as columns in master workbook

    Re: if column = "Field" or "Table" with title background filled with orange
    Is the Column named"Field" or "Table"? Or has the Header cell (top Row) for that Column the text "Field" or "Table"?

    Re: copy and paste into master workbook as column
    Which Sheet?
    Last edited by jolivanes; 07-16-2018 at 05:52 PM. Reason: more info requested

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: loop through files find columns and copy/paste them as columns in master workbook

    If the "Field" and "Table" Values are Column Headers, this should work.

    All Workbooks, Workbook with this code in it and the Workbooks you'll be copying from, have to be in one and the same Folder.
    Ths code will copy said Columns into a Sheet named "Master"
    This code will add a Sheet named "Master" into this Workbook.
    Color Index for the Color Orange is 45 in this code. Change/adapt as required.
    Change references if and where required.

    Please Login or Register  to view this content.
    Please use code tags when you put code in your Post. Peruse the Forum Rules. That's why we have them.
    Last edited by jolivanes; 07-16-2018 at 11:48 PM. Reason: Request using code tags

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: loop through files find columns and copy/paste them as columns in master workbook

    Just wondering, did it work?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy data from multiple files, then paste to master workbook.
    By TonyPepper in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-06-2016, 06:35 AM
  2. [SOLVED] Problem looping through multiple files to copy/paste into master workbook
    By shoes1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-07-2015, 02:46 PM
  3. [SOLVED] Copy certain columns from all the files and paste in a Summary workbook
    By vignesh rocks in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-19-2012, 09:33 AM
  4. Update master file columns with columns from slave files
    By underskinned in forum Excel General
    Replies: 1
    Last Post: 09-11-2012, 12:21 PM
  5. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  6. Loop Through and Copy CSV Files to Master Workbook
    By sweetnumb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 06:25 AM
  7. Loop workbook find and copy data columns
    By Zaeguzah in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2010, 06:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1