2017-02-04 3 views
0

통합 문서에서 두 시트의 특정 값이 일치하는 경우 두 개의 다른 시트의 텍스트를 병합하는 방법을 알아 보겠습니다.두 개의 다른 시트의 텍스트 병합

지연 및 DRG 두 장이 있습니다. "DRG"Col E의 텍스트를 "지연 시간"시트 Col P로 병합하려고합니다 (행에 텍스트가 이미있을 수 있지만 "DRG"Col E의 텍스트는 세미콜론과 병합해야합니다).

예 : 아래의 코드는 일치 작업을 수행하고 COL O의 특정 텍스트를 업데이트합니다.이 코드와 함께 병합 부분을 추가하려고합니다. "대기 시간"의 col A와 "DRG"의 Col B가 일치하는 경우 텍스트 "DRG"의 Col E는 "대기 시간"시트의 Col P (해당 행)에 병합되어야합니다.

더 좋은 방법은 환영합니다.

Sub PassFailValidation() 

Dim Rng As Range, cl As Range 
Dim LastRow As Long, MatchRow As Variant 

With Sheets("DRG") 
    LastRow = .Cells(.Rows.count, "C").End(xlUp).Row '<-- find last row with data in column C 
    Set Rng = .Range("C2:C" & LastRow) '<-- set range in Column C 
End With 

With Sheets("Latency") 
    For Each cl In .Range("B2:B" & .Cells(.Rows.count, "B").End(xlUp).Row) ' loop through all cells in Column B 
     MatchRow = Application.Match(cl.Value, Rng, 0) ' find match with values in Colummn C as in "DRG" sheet 
     If Not IsError(MatchRow) Then ' <-- successful match 

      Select Case Sheets("DRG").Range("D" & MatchRow + 1).Value 'Set D as the cell whch has the value 
       Case "Approved" 
        .Range("O" & cl.Row).Value = "Pass" 

       Case "Pended" 
        .Range("O" & cl.Row).Value = "Fail" 

       Case "In progress" 
        .Range("O" & cl.Row).Value = "In progress" 
      End Select 
     End If 
    Next cl 
End With 

최종 하위

+0

다음

If Not Sheets("DRG").Range("E" & MatchRow + 1).Value = vbNullString Then .Range("P" & cl.row).Value = Range("P" & cl.row).Value & IIf(Not Range("P" & cl.row).Value = vbNullString, ";", "") & Sheets("DRG").Range("E" & MatchRow + 1).Value 

로? –

+0

필요할 경우 제 3 자 사이트를 만들고 업로드 할 수 있습니다. –

답변

0

이 단지 추가 참조 : 당신은 몇 가지 테스트 데이터를 가지고 있습니까

Sub PassFailValidation() 

    Dim Rng As Range, cl As Range 
    Dim LastRow As Long, MatchRow As Variant 

    With Sheets("DRG") 
     LastRow = .Cells(.Rows.Count, "C").End(xlUp).row '<-- find last row with data in column C 
     Set Rng = .Range("C2:C" & LastRow) '<-- set range in Column C 
    End With 

    With Sheets("Latency") 
     For Each cl In .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).row) ' loop through all cells in Column B 
      MatchRow = Application.Match(cl.Value, Rng, 0) ' find match with values in Colummn C as in "DRG" sheet 
      If Not IsError(MatchRow) Then ' <-- successful match 
       Select Case Sheets("DRG").Range("D" & MatchRow + 1).Value 'Set D as the cell whch has the value 
        Case "Approved" 
         .Range("O" & cl.row).Value = "Pass" 

        Case "Pended" 
         .Range("O" & cl.row).Value = "Fail" 

        Case "In progress" 
         .Range("O" & cl.row).Value = "In progress" 
       End Select 
       If Not Sheets("DRG").Range("E" & MatchRow + 1).Value = vbNullString Then .Range("P" & cl.row).Value = .Range("P" & cl.row).Value & IIf(Not .Range("P" & cl.row).Value = vbNullString, ";", "") & Sheets("DRG").Range("E" & MatchRow + 1).Value 
      End If 
     Next cl 
    End With 

End Sub 
+0

이것은 훌륭합니다! 한 가지 더, ";" 빈 셀에 추가되는 반면 기존 텍스트가있는 경우에만 추가되어야합니다 ... 어떻게 변경합니까? –

+0

수정 된 코드보기 – user3598756

+0

그냥 똑똑! –

관련 문제