FindNext 的 vba 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18135996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:39:26  来源:igfitidea点击:

FindNext of vba not working

excelexcel-vbavba

提问by Ravi Khambhati

This is very basic question and don't scream at me coz i am not a vba expert.

这是非常基本的问题,不要冲我尖叫,因为我不是 vba 专家。

So here we go, I created below vba function

所以我们开始了,我在 vba 函数下面创建了

Public Function GetDuplicateCount(value As String) As Integer

    Dim counter As Integer

    counter = 0

    With Worksheets(1).Range("A:A")
        Set c = .Find(value, _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)

        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                counter = counter + 1
                Set c = .FindNext(c)
            Loop While Not c Is Nothing

        End If
    End With

    GetDuplicateCount = counter
End Function

Below is my excel values

以下是我的excel值

A

一种

1 IND

1 IND

2 USA

2 美国

3 CAN

3 CAN

4 IND

4 IND

5 CAN

5 CAN

6 USA

6 美国

Every time i search with any value it returns one don't know wny. Anything wrong with the function?

每次我搜索任何值时,它都会返回一个不知道的值。功能有什么问题吗?

e.g. GetDuplicateCount("IND")

例如 GetDuplicateCount("IND")

回答by Ravi Khambhati

Got it... finally

明白了……终于

Two things FindNext is not workign so as suggested by @kazjaw i tried .find and here is the working code. Dont forget to give additional condition and that is "firstAddress <> c.Address"

有两件事 FindNext 不起作用,所以正如@kazjaw 所建议的那样,我尝试了 .find,这是工作代码。不要忘记提供附加条件,即“firstAddress <> c.Address”

Public Function GetDuplicateCount(value As String) As Integer

    Dim counter As Integer
    counter = 0

    With Worksheets(1).Range("A:A")
        Set c = .Find(value, _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    after:=Cells(1, 1), _
                    MatchCase:=False)

        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                counter = counter + 1
                Set c = .Find(value, _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    after:=c, _
                    MatchCase:=False)
            Loop While Not c Is Nothing And firstAddress <> c.Address

        End If
    End With

    GetDuplicateCount = counter
End Function

回答by tigeravatar

Why not use the native Countif function?

为什么不使用原生的 Countif 函数?

=COUNTIF(A:A,"IND")