Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在单元格a中循环,在列b中查找匹配项_Excel_Concatenation_Match_Offset_Vba - Fatal编程技术网

Excel 在单元格a中循环,在列b中查找匹配项

Excel 在单元格a中循环,在列b中查找匹配项,excel,concatenation,match,offset,vba,Excel,Concatenation,Match,Offset,Vba,循环遍历列工作表1 a2:a,如果工作表2列a2:a中存在匹配项,则在工作表1 b2:b中找到偏移量(如果未找到其他项)。我已经拼凑了一些代码,但可能把自己弄糊涂了。我希望我的答案清晰明了 Dim r1 As Range Dim r2 As Range Dim i As Integer Dim lookupArray As Variant Dim lookupVal As Variant Dim matchResult As Variant Dim rowIndex As Long Dim

循环遍历列工作表1 a2:a,如果工作表2列a2:a中存在匹配项,则在工作表1 b2:b中找到偏移量(如果未找到其他项)。我已经拼凑了一些代码,但可能把自己弄糊涂了。我希望我的答案清晰明了

  Dim r1 As Range
Dim r2 As Range
Dim i As Integer
Dim lookupArray As Variant
Dim lookupVal As Variant
Dim matchResult As Variant
Dim rowIndex As Long
Dim e1 As Integer
Dim e2 As Integer

    r1 = r1.Range("A2:A").Cells
    r2 = r2.Range("B2:B").Cells

    e1 = Cells(Rows.Count, "A").End(xlUp).Row 'Range("A" & Cells.Rows.Count).End(xlUp).Offset(1,0).Select
    e2 = Cells(Rows.Count, "B").End(xlUp).Row

            For rowIndex = r1 To e1
            Set lookupVal = Range(r2 & rowIndex)

            matchResult = Application.match(lookupVal, r1, 0)

            If r1.cell(i, 1).Value = r2.cell(i, 1).Value And Not IsEmpty(Cells(i, 1).Value) Then
                  r1(i, 1).Offset(0, -1).Value "Found"
                  Else
                  r1(i, 1).Offset(0, -1).Value "NotFound"
                  End If
                'copy found cells in sheet 3
            Next rowIndex

第一种方法(更快、更简单、更短):

第二种方法:

Sub test2()
    Dim r1 As Range
    Dim r2 As Range
    Dim cell As Range
    Dim lastrow As Long

    'change Sheet1 to suit
    With ThisWorkbook.Worksheets("Sheet1")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set r1 = .Range("A2:A" & lastrow)
    End With

    'change Sheet2 to suit
    With ThisWorkbook.Worksheets("Sheet2")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set r2 = .Range("A2:A" & lastrow)
    End With

    For Each cell In r1
        If IsError(Application.Match(cell, r2, 0)) Then
            cell.Offset(, 1) = "NotFound"
        Else
            cell.Offset(, 1) = "Found"
        End If
    Next cell

End Sub
Sub test2()
    Dim r1 As Range
    Dim r2 As Range
    Dim cell As Range
    Dim lastrow As Long

    'change Sheet1 to suit
    With ThisWorkbook.Worksheets("Sheet1")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set r1 = .Range("A2:A" & lastrow)
    End With

    'change Sheet2 to suit
    With ThisWorkbook.Worksheets("Sheet2")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set r2 = .Range("A2:A" & lastrow)
    End With

    For Each cell In r1
        If IsError(Application.Match(cell, r2, 0)) Then
            cell.Offset(, 1) = "NotFound"
        Else
            cell.Offset(, 1) = "Found"
        End If
    Next cell

End Sub