2014-08-27 2 views
0

A 열의 값이 sheet2의 같은 열의 값과 일치하면 sheet1을 확인할 매크로가 있습니다. 그렇다면 각 셀에 대해 인접 셀을 복사합니다 sheet1에서 sheet2로 일치하는 값. 아래는 내가 지금까지 가지고 있지만 lastrowadd 라인에 'run error 9'가 계속 나오고 이유를 모르겠습니다. 어떤 도움을 주시면 감사하겠습니다 :) 작은 변화의 몇 lastrowAddlastrowRemove 모두가 정의 된 방식으로 포함되었습니다Excel VBA는 일치하는 기준에 따라 다른 시트의 인접한 열을 복사합니다.

Sub CopyAdjacent() 
    Dim i As Long, j As Long, colStatus As Long, lastrowAdd As Long, lastrowRemove As Long 

    colStatus = 2 'your status column number 
    lastrowAdd = Sheets(“Sheet1”).Cells(Sheets(“Sheet1”).Rows.Count, 1).End(xlUp).Row 
    lastrowRemove = Sheets(“Sheet2”).Cells(Sheets(“Sheet2”).Rows.Count, 1).End(xlUp).Row 

    For i = 1 To lastrowAdd 
     For j = 1 To lastrowRemove 
      If Sheets(“Sheet1”).Cells(i, 1).Value = Sheets(“Sheet2”).Cells(j, 1).Value Then 
       Sheets(“Sheet2”).Cells(j, colStatus).Value = Sheets(“Sheet1”).Cells(i, colStatus).Value 
      End If 
     Next j 
    Next i 
End Sub 
+0

'장 ("시트 1"). 세포 (장 ("시트 1"). Rows.Count 1) .END (xlUp)은 스프레드 시트 ("시트 1') '이 변경 시도 .Row'. 행 .Count-1' 그리고 무슨 일이 일어나는 지 알아보기 – Saechel

+0

여전히 같은 결과를 준다. – Steven

답변

0

. 정의에서 ij도 삭제했습니다.

Sub CopyAdjacent() 
Dim colStatus As Long, lastrowAdd As Integer, lastrowRemove As Integer 

colStatus = 2 
lastrowAdd = Sheets(“Sheet1”).Cells(Rows.Count, 1).End(xlUp).Row 
lastrowRemove = Sheets(“Sheet2”).Cells(Rows.Count, 1).End(xlUp).Row 

For i = 1 To lastrowAdd 
    For j = 1 To lastrowRemove 
     If Sheets(“Sheet1”).Cells(i, 1).Value = Sheets(“Sheet2”).Cells(j, 1).Value Then 
      Sheets(“Sheet2”).Cells(j, colStatus).Value = Sheets(“Sheet1”).Cells(i, colStatus).Value 
     End If 
    Next 
Next 
End Sub 

또한 동일한 열에서 두 항목이 일치하는지 확인하지 않습니다. Sheet2의 모든 열을 각각 Sheet1에서 확인합니다. 나는 아래 코드가 당신이 찾고있는 것이라고 생각한다.

Sub CopyAdjacent() 
' The below line has been changed, you may still omit lastrowRemove 
Dim colStatus, lastrowAdd, lastrowRemove As Integer 

colStatus = 2 
lastrowAdd = Sheets(“Sheet1”).Cells(Rows.Count, 1).End(xlUp).Row 
' The below line is now redundant in the new code 
'lastrowRemove = Sheets(“Sheet2”).Cells(Rows.Count, 1).End(xlUp).Row 

For i = 1 To lastrowAdd 
     If Sheets(“Sheet1”).Cells(i, 1).Value = Sheets(“Sheet2”).Cells(i, 1).Value Then 
      Sheets(“Sheet2”).Cells(i, colStatus).Value = Sheets(“Sheet1”).Cells(i, colStatus).Value 
     End If  
Next 
End Sub 
+0

나는 당신이 한 일에서 당신의 요지를보고 당신의 변화를 이해했다고 생각하지만, 슬프게도 여전히 같은 오류가 난다./ – Steven

+0

오류가 어디서 발생했는지 구체적으로 알려주 는가? 런타임 오류 9 범위를 벗어나는 것입니다, 당신의 시트는 확실히 'Sheet1'과'Sheet2'라고하거나 당신에게 여분의 아무것도주지 않는거야? 또한'colStatus As Long'을'colStatus As Integer'로 변경해보십시오. 그 이유는 셀 A1.5를 가질 수 있기 때문에 정수로 셀 참조를 호출해야한다고 생각하기 때문일 수도 있습니다. –

+0

colStatus를 Long에서 Integer로 변경하는 경우에도 동일한 행의 lastrowAdd와 동일한 디버깅 하이라이트를 제공합니다. 또한, 내 시트는 Sheet1 및 Sheet2라고합니다. – Steven

관련 문제