Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this in Excel VBA. If you haven't used VBA in Excel before, see the link below to see how to access it.</p> <p><a href="http://msdn.microsoft.com/en-us/library/ee814737.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ee814737.aspx</a></p> <p><strong>Make sure you backup your spreadsheet before trying any of this in case something goes wrong!</strong></p> <p>I wrote something up that will copy data from your secondary worksheets into your main worksheet. Just open the VBA editor, and paste in the code.</p> <p>Next, edit the <code>ConsolidateWorksheets()</code> function so that it has the right names for your sheets. If you have additional sheets, declare them and add another line that calls the <code>ProcessWorksheet</code> subroutine for the added sheet.</p> <p>This code will copy data from your tickets and donations worksheet into your main worksheet when it finds a matching id. If there isn't a matching id, it doesn't copy anything for that row.</p> <pre><code>Option Explicit Sub ConsolidateWorksheets() 'declare the worksheets you are using Dim mainWks As Worksheet Dim ticketsWks As Worksheet Dim donationsWks As Worksheet 'set the worksheet names Set mainWks = ThisWorkbook.Worksheets("Sheet1") Set ticketsWks = ThisWorkbook.Worksheets("Sheet2") Set donationsWks = ThisWorkbook.Worksheets("Sheet3") Call ProcessWorksheet(mainWks, ticketsWks) Call ProcessWorksheet(mainWks, donationsWks) End Sub ' copies data from the otherWks to the mainWks Sub ProcessWorksheet(mainWks As Worksheet, otherWks As Worksheet) Dim i As Integer Dim rowId As Integer Dim otherRowIndex As Integer Dim otherLastColIndex As Integer Dim lastRowIndex As Integer Dim pasteColStart As Integer Dim pasteColEnd As Integer ' figure out the last row in the main sheet lastRowIndex = mainWks.UsedRange.Rows.count otherLastColIndex = otherWks.UsedRange.Columns.count ' figure out where to copy and paste from ' this assumes that the id row is always the first row in every sheet pasteColStart = mainWks.UsedRange.Columns.count + 1 pasteColEnd = pasteColStart + (otherLastColIndex - 2) ' copy column headers otherWks.Activate otherWks.Range(Cells(1, 2), Cells(1, otherLastColIndex)).Copy mainWks.Activate mainWks.Range(Cells(1, pasteColStart), Cells(1, pasteColEnd)).PasteSpecial ' loop through all the rows of the main sheet For i = 2 To lastRowIndex ' get row id from first cell in current row rowId = Cells(i, 1).Value 'lookup row id in other worksheets otherRowIndex = FindIdRowInWks(otherWks, rowId) If otherRowIndex &lt;&gt; 0 Then otherWks.Activate otherWks.Range(Cells(otherRowIndex, 2), Cells(otherRowIndex, otherLastColIndex)).Copy mainWks.Activate mainWks.Range(Cells(i, pasteColStart), Cells(i, pasteColEnd)).PasteSpecial End If Next i End Sub ' loops through the given worksheet, looks for a given id in the first column ' and returns the row index where the id was found. returns 0 if nothing found. Public Function FindIdRowInWks(wks As Worksheet, idToFind As Integer) As Integer Dim lastRow As Integer lastRow = wks.Range("A" &amp; Rows.count).End(xlUp).Row Dim rowNumber As Integer rowNumber = 0 Dim i As Integer For i = 2 To lastRow If (Cells(i, 1).Value = idToFind) Then rowNumber = i End If Next i FindIdRowInWks = rowNumber End Function </code></pre> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload