2017-01-25 1 views
0

다음 VBA 코드를 사용하여 동일한 통합 문서 내에있는 두 시트의 데이터를 연결하고 싶습니다. 두 시트의 각 항목에는 고유 식별자가 있습니다. 그 식별자를 사용하고 sheet2의 전체 행을 복사 한 다음 sheet1의 마지막 열의 오른쪽에 복사하기를 바라고 있습니다.복사 고유 식별자를 사용하여 데이터 붙여 넣기

이 코드를 수정하는 방법은 무엇입니까?

Sub link_data() 
    Dim i, lastrow 
    im i2, lastrow2 

    Dim A As Double 
    Dim D As Double 
    lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row 
    lastrow2 = Sheet2.Range("A" & Rows.Count).End(xlUp).Row 
    For i = 2 To lastrow 
    For i2 = 2 To lastrow2 
    Set D = Sheet1.Cells(i, "AW") 
    Set A = Sheet2.Cells(i2, "AI") 
    If Sheet1.Cells(D).Value = Sheet2.Cells(A) Then 
    Sheet2.Cells(A).EntireRow.Copy Destination:=Sheet1.Cells(i, "AX").end(xlRight).Offset(1) 
End Sub 
+0

닫기 'Loops)를 호출하고'If'를'End If'로 닫습니다. –

+0

형식이 개선되고 쓸데없는 텍스트가 삭제되었습니다. –

답변

0

어떻게되는지 알려 :

Next`가 (당신이이`이`와`For`의 각
Sub link_data() 
    Dim i As Long, lastrow As Long, i2 As Long, lastrow2 As Long 
    Dim A As Range, D As Range 

    lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row 
    lastrow2 = Sheet2.Range("A" & Rows.Count).End(xlUp).Row 
    For i = 2 To lastrow 
     Set D = Sheet1.Cells(i, "AW") 
     For i2 = 2 To lastrow2 
      Set A = Sheet2.Cells(i2, "AI") 
      If D.Value = A.Value Then 
       Intersect(A.Parent.UsedRange, A.EntireRow).Copy Destination:=Sheet1.Cells(i, Columns.Count).End(xlToLeft).Offset(, 1) 
      End If 
     Next 
    Next 
End Sub 
0

실제 힘든 것을 재현 할 더미 데이터가없는 것을 볼 수 있습니다. 하지만 당신의 데이터로 아래를 시도하고 나에게 어쩌면 당신이 후있어

Option Explicit 
Sub link_data() 
Dim i, lastrow 
Dim i2, lastrow2 
Dim A 
Dim D 

lastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row 
lastrow2 = Sheet2.Range("A" & Rows.Count).End(xlUp).Row 

For i = 2 To lastrow 
    For i2 = 2 To lastrow2 
    Set D = Sheets("Sheet1").Cells(i, "AW") 
    Set A = Sheets("Sheet2").Cells(i2, "AI") 
     If Sheet1.Cells(D).Value = Sheet2.Cells(A) Then 
      Sheet2.Cells(i2, "AI").EntireRow.Copy Destination:=Sheet1.Cells(i, "AX") 
     End If 
    Next i2 
Next i 

End Sub