合并来自两张不同工作表的文字

时间:2017-02-04 14:42:15

标签: excel vba excel-vba merge

如果这两张表中的某些值匹配,我正在研究如何合并工作簿中两张不同工作表的文本。

我有两张纸,Latency和DRG。我想将“DRG”Col E中的文本合并到“Latency”表格Col P中(行可能已经有文本,但“DRG”Col E中的文本应该用分号合并。)

示例:(下面的代码执行匹配并更新COl O中的某个文本,我想添加合并部分以及此代码) 如果在“延迟”的col A和“DRG”的Col B中匹配,那么“DRG”的Col 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

End Sub

2 个答案:

答案 0 :(得分:0)

我确信这个问题会吸引一些很棒的VBA编码答案。但是,可以使用数据库引擎技术来解决这些问题。见初学者https://support.microsoft.com/en-gb/help/278973/excelado-demonstrates-how-to-use-ado-to-read-and-write-data-in-excel-workbooks

答案 1 :(得分: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