Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 21 of 21

Thread: Copy Paste from multiple worksheets into one based on multiple criteria

  1. #21
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    first bit adds two worksheets "master" and "uid", if they dont exist; clears their existing data if they exist. (master is for consolidation and uid is for controlling the number of same persons's records in all 4 sheets (x, y, z, p))
    writes column headers from worksheet "data fields" to worksheet "master".
    adds headers ("UID", "x", "y", "z", "p") to worksheet "uid".
        On Error Resume Next
        Set wsM = Worksheets("master")
        ...
        ...
        ...
            .Range("A1:E1") = Array("UID", "x", "y", "z", "p")
        End With

    second bit creates an id by concatenating cell values in columns C and D (format: FirstName MiddleName LastName_BOD). inserts a blank column as their first column, writes these id's in the corresponding cells of column A of all 4 sheets (x, y, z, p). writes these id's to column A of worksheet "uid" as well. then sorts worksheets on uid's, ie Column A.
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
        ...
        ...
        ...
            Next e
        End With
    the third bit counts the number of records of the same person in all 4 worksheets. this is necessary for covering the all records of the same person. and writes the sorted uid's for the same person maximum (of 4 ws) number of times) in col A of master.
        With wsU
        'determine the number of same uid in all wss
        ...
        ...
        ...
        End With
    now we have 6 worksheets having uid's in their column A; with one difference, worksheet "uid" has unique list of uid's.

    the last bit finally consolidates the data from 4 sheets (x, y, z, p) in master.
        For Each e In Array("x", "y", "z", "p")
            With Worksheets(e)
        ...
        ...
        ...
            End With
        Next e
    remember uid's in worksheet uid are sorted (so are in master)?

    loops row 2 to LastRow in 4 wss.
    looks for uid in col A of "master" up-down, finds the first occurence of uid, returns its row number as FRuid.
    looks for uid in col A of "master" bottom-up, finds the first (last, from up-down perspective) occurence of uid, returns its row number as LRuid.
    loops column 2 to LastCol (.Cells(1, .Columns.Count).End(xlToLeft).Column) in 4 wss.
    looks for headers in Row 1 of "master" left-right, finds the header, returns its column number as ColNumMaster.
    writes the cell value (.Cells(i, j)) from 4 worksheets to master, in the intersect cell (FRuid To LRuid, ColNumMaster), if that cell is not previously inserted a value, according to outer and inner loops.
    Last edited by mancubus; 04-08-2014 at 06:20 AM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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