2013-10-31 2 views
0

첫 번째 워크 시트의 모든 데이터 항목에 대해 두 번째 워크 시트의 모든 데이터 행 사본이 추가되도록 다른 워크 시트에 결합하려는 2 Excel 워크 시트에 정보가 있습니다. 예 :어떻게 데이터를 병합하는 두 개의 워크 시트를 결합 할 수 있습니까?

 
Sheet One 
    A 
Department 1 
Department 2 
Department 3 

---------------------------------------------- 

Sheet 2 
    F   G   H 
ItemCode1, ItemDesc1, ItemCost1 
ItemCode2, ItemDesc2, ItemCost2 
ItemCode3, ItemDesc3, ItemCost3 
ItemCode4, ItemDesc4, ItemCost4 
ItemCode5, ItemDesc5, ItemCost5 

---------------------------------------------- 

Resultant Sheet 3 
     A   F   G   H 
Department 1, ItemCode1, ItemDesc1, ItemCost1 
Department 1, ItemCode2, ItemDesc2, ItemCost2 
Department 1, ItemCode3, ItemDesc3, ItemCost3 
Department 1, ItemCode4, ItemDesc4, ItemCost4 
Department 1, ItemCode5, ItemDesc5, ItemCost5 
Department 2, ItemCode1, ItemDesc1, ItemCost1 
Department 2, ItemCode2, ItemDesc2, ItemCost2 
Department 2, ItemCode3, ItemDesc3, ItemCost3 
Department 2, ItemCode4, ItemDesc4, ItemCost4 
Department 2, ItemCode5, ItemDesc5, ItemCost5 
Department 3, ItemCode1, ItemDesc1, ItemCost1 
Department 3, ItemCode2, ItemDesc2, ItemCost2 
Department 3, ItemCode3, ItemDesc3, ItemCost3 
Department 3, ItemCode4, ItemDesc4, ItemCost4 
Department 3, ItemCode5, ItemDesc5, ItemCost5 

누구든지 나를 도와 줄 수 있습니까? 지금까지 새 시트를 작성하는 데이터를 반복하려고 시도했지만 쉽게 이동하는 방법이 있다고 생각합니다.

+1

여기서 Sheet1 및 Sheet2의 데이터와 일치하는 'Key'열은 어디에 있습니까? 어느 항목이 어떤 부서에 속해 있는지 알기 위해서는'Key' 칼럼이 필요합니다. 아니면 'Department'를 가리키는'ItemCode'에 뭔가가 있습니까? – L42

+0

이러한 키를 사용하면 INDEX & MATCH를 VBA 대신 사용할 수 있습니다. – pnuts

+0

키가 없습니다. 단지 sheet1의 각 행에 대해 한 번에 복사/붙여 넣기를 수행하는 것입니다. –

답변

0

아래의 VBA 코드는 코드 분석 및 더 나은 이해를위한 추적입니다.
meachanical 한 종류의 방법 (그냥 복사하고 붙여 넣기).
이것은 더 잘했을 수도 있지만 제 생각에는 꽤 큰 코드입니다.

Sub Macro1() 

Dim wkbk As Workbook 
Dim i As Integer 

Dim lastrow As Long 
Dim lastrow3 As Long 
Dim lastrowref As Long 

i = 1 

Set wkbk = ActiveWorkbook 

    Do 
     ' to find the range(used to paste values in sheet 3(Column A-Department1 
     'and cloumn B(for Values in sheet2) 
     lastrowref = lastrow3 + 1 

     With wkbk.Sheets(2).Select 
     Range("f1:H1").Select 
     Range(Selection, Selection.End(xlDown)).Select 

     Selection.Copy 
     End With 

     With wkbk.Sheets(3).Select 
     Cells(lastrowref, 6).Select 
     ActiveSheet.Paste 
     End With 

        With ActiveWorkbook.Sheets(3) 
' to find the cells where data needs to be pasted 
        lastrow3 = .Range("f1").End(xlDown).Row 
        End With 


        Sheets("Sheet1").Select 
        With ActiveWorkbook.Sheets(1) 
'to find the number of records in sheet1 
        lastrow = .Range("a1").End(xlDown).Row 
        End With 

        With ActiveWorkbook.Sheets(1) 
        .Cells(i, 1).Select 
        Selection.Copy 
        End With 

     With wkbk.Sheets(3).Select 
     Range(Cells(lastrow3, 1), Cells(lastrowref, 1)).Select 
     ActiveSheet.Paste 
     End With 
' loops till the Number of departments in sheet1 
       i = i + 1 
    Loop While i <= lastrow 


End Sub 
+0

루틴은 내가 필요한 것을 수행합니다. 고맙습니다. "Cells (lastrowref, 6) .Select"를 3으로 변경하려고 시도했습니다. 그러나이 값을 변경하면 아무리 " 런타임 오류 '1004': 응용 프로그램 정의 또는 개체 정의 오류 ". 오류가 발생하면 결과 시트 A 열은 첫 번째 부서 정보로 마지막 Excel 행 1048576까지 채워지고 항목 중 하나는 C1 열에서부터 배치됩니다. – Enthusiast

관련 문제