简体   繁体   English

此代码有什么问题? .VBA查找

[英]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. 我有两个子项,第一个子项“ Find”在G列中找到所有值,范围(G1:G10),我有三个值,所以它循环3次并给我值。

Now i am calling a second sub "Find2" to find that value in column A, range (A1:A10). 现在,我正在调用第二个子“ Find2”以在A列范围(A1:A10)中找到该值。 the problem is it runs only once.. it is not looping three times to get 3 values. 问题是它只运行一次..它不循环三次以获取3个值。 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. 我同意加里(Gary)的说法,这有一些干扰。 Instead of using Find() in your subroutine, see if this will work for you. 而不是在您的子例程中使用Find(),请看这是否对您有用。

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 子安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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