2013-12-05 2 views
0

거대한 데이터베이스를 Sheet1에 내 보냅니다 (11K 행). 레코드 ID는 CQ C 럼에 있습니다. 검색을 기반으로 행 데이터를 별도의 시트로 복사

는 난 단지 시트 2에서 기록 식별자, 나는 다음과 같은 매크로를 발견하고이 사이트를 검색 2 일 이후에 약간의 수정을 한

열 A의 작은 목록 (60-100)를 가지고있다. 이 솔루션은 부분적으로 작동합니다. Find Value on other sheet and copy entire row

첫 번째 행을 반환하지만 데이터 열을 계속 진행하지 않습니다. 단계별로 진행하면 매크로를 반복적으로 반복하는 것처럼 보입니다. 지금의 약자로

여기

Sub SearchForString() 

    Dim LCopyToRow As Integer 


    On Error GoTo Err_Execute 


    'Start copying data to row 2 in Sheet2 (row counter variable) 
    LCopyToRow = 1 

    Dim sheetTarget As String: sheetTarget = "sheet2" 
    Dim sheetToSearch As String: sheetToSearch = "sheet1" 
    Dim targetValue As String: targetValue = Sheets(sheetTarget).Range("A1").Value 'Value in sheet2!A1 to be searched in sheet1 
    Dim columnToSearch As String: columnToSearch = "CQ" 
    Dim iniRowToSearch As Integer: iniRowToSearch = 1 
    Dim LSearchRow As Long 'As far as it is not clear the number of rows you will be considering, better relying on the long type 
    Dim maxRowToSearch As Long: maxRowToSearch = 12000 'There are lots of rows, so better setting a max. limit 

    If (Not IsEmpty(targetValue)) Then 
     For LSearchRow = iniRowToSearch To Sheets(sheetToSearch).Rows.Count 

      'If value in the current row (in columnToSearch in sheetToSearch) equals targetValue, copy entire row to LCopyToRow in sheetTarget 
      If Sheets(sheetToSearch).Range(columnToSearch & CStr(LSearchRow)).Value = targetValue Then 

       'Select row in Sheet1 to copy 
       Sheets(sheetToSearch).Rows(LSearchRow).Copy 

       'Paste row into Sheet2 in next row 
       Sheets(sheetTarget).Rows(LCopyToRow).PasteSpecial Paste:=xlPasteValues 

       'Move counter to next row 
       LCopyToRow = LCopyToRow + 1 
      End If 

      If (LSearchRow >= maxRowToSearch) Then 
       Exit For 
      End If 

     Next LSearchRow 

     'Position on cell A3 
     Application.CutCopyMode = False 
     Range("A3").Select 

     MsgBox "All matching data has been copied." 
    End If 

    Exit Sub 

답변

0

당신은 항상 두 시트 1의 마지막 업데이트 된 행을 찾을뿐만 아니라 시트 2에 코드 아래에 주어진를 사용할 수 있습니다 ... 매크로입니다.

다음은 코드입니다.

Sub Testing() 
    'for getting the last row udpated, you have to enter the max range reference 
    'in our case it is A1048576. It starts from last and check what is our last 
    'row with data in specific to column A. 

    'Same can be used for colu 
    a = Sheet1.Range("A1048576").End(xlUp).Row 
End Sub 

위의 코드는 코드의 아래 줄에 제시되어 있습니다.

For LSearchRow = iniRowToSearch To Sheets(sheetToSearch).Rows.Count 
+0

어디에서 추가 할 수 있습니까? 몇 군데에 추가했는데 매크로의 결과와 함께 아무 것도 변경되지 않았습니다. – user3070911

+0

다시 연락 드리겠습니다. Sandesh :) – sandeshjadhav

+0

안녕하세요, 샘플 데이터를 게시 할 수 있습니까? 나는 당신의 코드를 실행하려고 시도했다. Sandesh :) – sandeshjadhav

관련 문제