匹配2个工作表中的列并粘贴到第三个工作表

时间:2017-06-29 15:48:52

标签: excel vba excel-vba

我在2个工作表(wb1和wb2)上没有特定顺序的数字。如果匹配,我需要wb2上b列中的电子邮件地址粘贴到A列中的工作表wb3。下面的代码只匹配两个工作表中相同单元格中的数字。非常感谢任何帮助。

Private Sub CommandButton2_Click()
'Merge Report button

    Dim foundCell As Range
    Dim strFind As String
    Dim fRow, fCol As Integer
    Dim wb1 As Worksheet
    Dim wb2 As Worksheet
    Dim wb3 As Worksheet

    'Set sheets - wb1 is email list and wb2 is missed punch report, and wb3 is home page
    Set wb1 = Sheets("Sheet1")
    Set wb2 = Sheets("Sheet1 (2)")
    Set wb3 = Sheets("Home")

    'Get find string
    strFind = wb1.Range("A1").Value

    'Find string in column C of Sheet2
    Set foundCell = wb2.Range("A1").Find(strFind, LookIn:=xlValues)

    'If match cell is found
    If Not foundCell Is Nothing Then

        'Get the row and column
        fRow = foundCell.Row
        fCol = foundCell.Column

        'copy email from column b
        wb1.Range("B1").Copy

        'paste in column a on home page
        wb3.Range("A1").PasteSpecial xlPasteValues

        'Clear cache
        Application.CutCopyMode = False

    'If not found, show message.
    Else

        Call MsgBox("No match was found. Clear form and start over with step 1.")

    End If

End Sub

0 个答案:

没有答案