宏匹配来自sheet1的字符串值,如果匹配将下一个单元格值复制到sheet2

时间:2018-06-15 21:47:14

标签: excel vba excel-vba

我需要找到sheet2(C:C)中sheet1的每个单元格(C:C)值的匹配,如果值匹配,则复制相应的下一个单元格,即D:D并替换为表格2.如果是如果不匹配,则将范围A复制并粘贴到工作表2中下一个空单元格中的D

    Sub Method1()
    Dim strSearch As String
    Dim strOut As String
    Dim bFailed As Boolean
    Dim i As Integer

    strSearch = Sheet1.Range("C2")
    i = 1
    Do Until ActiveCell.Value = Empty
    ActiveCell.Offset(1, 0).Select 'move down 1 row
    i = i + 1 'keep a count of the ID for later use
    Loop
    'ActiveCell.Value = i

    On Error Resume Next
    strOut = Application.WorksheetFunction.VLookup(strSearch, Sheet2.Range("C:C"), 2, False) 
            If Err.Number <> 0 Then bFailed = True
    On Error GoTo 0

    If Not bFailed Then
    MsgBox "corresponding value is " & vbNewLine & strOut
    Else
    MsgBox strSearch & " not found"
    End If
    End Sub

Sheet1:`在这里输入代码 enter image description here

Sheet 2中: enter image description here

2 个答案:

答案 0 :(得分:0)

尝试一下:

Sub Method1()

    Dim cSearch As Range, m

    Set cSearch = Sheet1.Range("C2")

    Do While Len(cSearch.Value) > 0
        'omit the "WorksheetFunction" or this will throw a run-time error
        '   if there's no match. Instead we check the return value for an error
        m = Application.Match(cSearch.Value, Sheet2.Range("C:C"), 0)

        If Not IsError(m) Then
            'got a match - update ColD on sheet2
            Sheet2.Cells(m, "D").Value = cSearch.Offset(0, 1).Value
        Else
            'no match - add row to sheet2 (edit)
            cSearch.Offset(0, -2).Resize(1, 4).Copy _
                     Sheet2.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        End If

        Set cSearch = cSearch.Offset(1, 0) 'next value to look up
    Loop

End Sub

答案 1 :(得分:0)

但是,我更改了代码并完成了工作,但是我想为C:C中的每个单元重复该功能,看看

                        Sub Method1()
                            Dim strSearch As String
                            Dim strOut As String
                            Dim bFailed As Boolean
                            Dim i As Integer

                            strSearch = Sheet1.Range("C2")
                            i = 1
                            'Do Until ActiveCell.Value = Empty
                                     ActiveCell.Offset(1, 0).Select 'move down 1 row
                                     i = i + 1 'keep a count of the ID for later use
                                ' Loop

                             'ActiveCell.Value = i

                            On Error Resume Next
                            strOut = Application.WorksheetFunction.VLookup(strSearch, Sheet2.Range("C:C"), 1, False)
                            If Err.Number <> 0 Then bFailed = True
                            On Error GoTo 0

                            If Not bFailed Then
                            Worksheets("Sheet1").Range("e2").Copy
                            Worksheets("Sheet2").Range("e2").PasteSpecial Paste:=xlPasteFormulas
                            Application.CutCopyMode = False
                            ActiveCell.Interior.ColorIndex = 6
                            MsgBox "corresponding value been copied " & vbNewLine & strOut
                            Else

                            MsgBox strSearch & " not found"
                            End If
                            End Sub