.FIND和Offset().FIND函数

时间:2013-09-04 15:11:25

标签: vba excel-vba excel-2010 excel

我在Excel中为3张代码编写代码,工作表1将显示Sheet3中未出现在Sheet2中的数据,为了完成此代码,代码将如下所示;

Dim r As Excel.Range
Dim cell As Excel.Range
Set r = Sheet3.Range(Sheet3.Cells(1, 1), Sheet3.Cells(Rows.Count, 1).End(xlUp))
Dim curRowSheet1 As Long

curRowSheet1 = 1

For Each cell In r
    Set rFind = Sheet2.Range("A:A").Find(cell.Value)



    If (rFind Is Nothing) Then
        cell.EntireRow.Copy Sheet1.Cells(curRowSheet1, 1)
        curRowSheet1 = curRowSheet1 + 1
    End If
Next cell

注意:我试图在第9行“SET rFind”下包含第二个.FIND,其中if在列中查找单元格值(“A:Ä”)然后它还验证列中的值是否相同(表2和表3中的“B:B”),我想我可以使用Offset()函数来比较这些数据,对此的任何建议都会受到赞赏!!!!

感谢。

1 个答案:

答案 0 :(得分:0)

我建议使用Range.Find

而不是WorksheetFunction.CountIfs
Sub tgr()

    Dim ACell As Range

    For Each ACell In Sheet3.Range("A1", Sheet3.Cells(Rows.Count, "A").End(xlUp)).Cells
        If WorksheetFunction.CountIfs(Sheet2.Columns("A"), ACell.Value, Sheet2.Columns("B"), ACell.Offset(, 1).Value) = 0 Then
            ACell.EntireRow.Copy Sheet1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
        End If
    Next ACell

End Sub