do while loop based on a value available in cell (find) in vba

断了今生、忘了曾经 提交于 2019-12-06 07:50:26

Try below code :

Sub test()

    Dim lastRow As Long
    Dim rng As Range
    Dim firstCell As String

    lastRow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row

    For i = 1 To lastRow
        Set rng = Sheets("sheet2").Range("A:A").Find(What:=Cells(i, 1), LookIn:=xlValues, lookAt:=xlPart, SearchOrder:=xlByRows)

        If Not rng Is Nothing Then firstCell = rng.Address

        Do While Not rng Is Nothing

            rng.Offset(0, 1) = "found"

            Set rng = Sheets("sheet2").Range("A:A").FindNext(rng)


            If Not rng Is Nothing Then
                If rng.Address = firstCell Then Exit Do
            End If

        Loop
    Next

End Sub

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!