简体   繁体   中英

What is wrong with this code? .Find , VBA

I have two subs, First sub "Find" finds all the values in column G , range (G1:G10) , there i have three values so it loops 3 times and gives me the value.

Now i am calling a second sub "Find2" to find that value in column A, range (A1:A10). the problem is it runs only once.. it is not looping three times to get 3 values. Any idea ?. why it is not working.

Sub Find()

Set shtSheet1 = Sheets("Sheet1")
With shtSheet1.Range("G1:G10")
    Set V = .find("*", LookIn:=xlValues)
    If Not V Is Nothing Then
        FirstAddress = V.Address

        Do
        X = V
        Call Find2
        Set V = .FindNext(V)

        Loop While Not V Is Nothing And V.Address <> FirstAddress

    End If
End With

End Sub

Second sub

Public Sub Find2()

Set shtSheet1 = Sheets("Sheet1")
Set shtSheet2 = Sheets("Sheet2")

    With shtSheet1.Range("A1:A10")
        Set C = .find(X, LookIn:=xlValues)
        If Not C Is Nothing Then
        MsgBox X
        End If
    End With

End Sub

我认为在子例程中使用.Find()会干扰主例程中的.FindNext()。

I agree with Gary, there's some interference going on. Instead of using Find() in your subroutine, see if this will work for you.

Public Sub Find2()
    Dim shtSheet1 As Worksheet
    Dim C As Range

    Set shtSheet1 = Sheets("Sheet1")
    Set C = shtSheet1.Range("A1:A10")

    If Not IsError(Application.Match(X, C, 0)) Then
        MsgBox X
    End If

End Sub

Use

private Function Find2()
end function

insted of sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM