2017-01-23 2 views
0

아래 에서처럼 한 시트에서 다른 시트로 데이터를 복사하는 코드가 있지만 조회 부분이 작동하지 않습니다. 내가이 검색 기능을 사용하지 않는 경우 다음 코드는 게시물에 따르면 좋은vlookup 및 if 조건 복사 데이터 vba

Sub CopyRows() 

Dim Rng As Range 
Dim Rng2 As Range 
Dim Cl As Range 
Dim str As String 
Dim RowUpdCrnt As Long 

Set UsedRange = Sheets("Jan").Range("b5:bk81") 
Set Rng = Sheets("Jan").UsedRange 'the range to search ie the used range 
Set Rng2 = Sheets("Feb").Range("I5:AK5") 

str = "WRK." 'string to look for 
Sheets("Feb").Range("B5:B81").Value = "" 
RowUpdCrnt = 5 

' In my test data, the "WRK."s are in column AN. This For-Each only selects column AN. 
' I assume all my "WRK."s are in a single column. Replace "B" by the appropriate 
' column letter for your data. 

For Each Cl In Rng.Columns("AN").Rows 
    If Cl.Text = str Then 
     'if the cell contains the correct value copy it to next empty row on sheet 2 & delete the row 
     VLookup(Cl.EntireRow.Range("b1"), Sheets("Master").Range("H7:H200"), 1, 0).Copy 
     Sheets("Feb").Cells(RowUpdCrnt, 2).PasteSpecial xlPasteValues 
     RowUpdCrnt = RowUpdCrnt + 1 
    End If 
Next Cl 
Application.CutCopyMode = False 

End Sub 
+0

시도'대신 Application.VLookup' –

답변

0

을 작동하고, 복사 할 수있는 유일한 것은 당신이 방금 복사 >> 붙여 넣기를 사용하지 않고 (세포를받을 수 있도록하는 값입니다), Sheets("Feb").Range("B" & RowUpdCrnt).Value = Application.VLookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0)

를 사용하여 아래의 코드 시도 :

With Sheets("Jan") 
    ' loop until last row with data in Column AN (and not the entire column) to save time 
    For Each Cl In .Range("AN1:AN" & .Cells(.Rows.Count, "AN").End(xlUp).Row) 
     If Cl.Value Like str Then 
      'if the cell contains the correct value copy it to next empty row on sheet 2 & delete the row 
      If Not IsError(Application.VLookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0)) Then ' <-- verify the VLookup was successful 
       Sheets("Feb").Range("B" & RowUpdCrnt).Value = Application.VLookup(.Range("B" & Cl.Row).Value, Sheets("Master").Range("H7:H200"), 1, 0) 
       RowUpdCrnt = RowUpdCrnt + 1 
      End If 
     End If 
    Next Cl 
End With