これは、Excel VBA で行うことができます。以前に Excel で VBA を使用したことがない場合は、以下のリンクを参照してアクセス方法を確認してください。
http://msdn.microsoft.com/en-us/library/ee814737.aspx
何か問題が発生した場合に備えて、これを試す前にスプレッドシートをバックアップしてください。
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.
Next, edit the ConsolidateWorksheets()
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 ProcessWorksheet
subroutine for the added sheet.
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.
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 <> 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" & 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
Hope that helps!