2017-02-04 102 views
0

如果在这两张工作表中的某些值之间存在匹配,我正在关注如何合并工作簿中两张不同工作表的文本。合并来自两张不同工作表的文本

我有两张表,延迟和DRG。我想将文本从“DRG”列E合并到“延迟”列表列P(行可能已经有文本,但来自“DRG”列E的文本应与分号合并)。下面的代码进行匹配并更新CO10中的某个文本,我想添加合并部分以及此代码) 如果在“等待时间”的列A和“DRG”的列B中存在匹配,则文本在“DRG”的E栏中应该合并到“Latency”表的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

你有一些测试数据? –

+0

如果需要,我可以创建一个并上传任何第三方网站。 –

回答

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 

如下

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

只是辉煌! –

相关问题