【问题标题】:FindNext of vba not workingvba的FindNext不起作用
【发布时间】:2013-08-08 20:59:07
【问题描述】:

这是一个非常基本的问题,不要对我大喊大叫,因为我不是 vba 专家。

所以我们开始吧,我在下面创建了 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

以下是我的 excel 值

一个

1 个试验

2 美国

3 可以

4 工业

5 罐

6 美国

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

例如GetDuplicateCount("IND")

【问题讨论】:

  • 这很奇怪 .FindNextFunction 中不起作用,但在 Sub 中起作用。您需要再次使用.FindHere is similar Q&A
  • 按照建议,我将 .FindNext 更改为 Set c = .Find(What:=value, _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ after:=c, _ MatchCase:=False) 但它进入了无限循环。

标签: excel vba


【解决方案1】:

明白了……终于

两件事 FindNext 不是 workign,所以正如 @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

【讨论】:

  • 如果c实际上是Nothing,那么如果我们尝试获取c.Address就会得到一个错误......
【解决方案2】:

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

=COUNTIF(A:A,"IND")

【讨论】:

  • 感谢您的建议,但我感兴趣的是搜索一行并从同一行但不同列中获取值。我知道我给你的例子是坏的。很抱歉。
  • 如果需要对不同列的值求和,可以使用 Sumif:=SUMIF(A:A,"IND",B:B)
  • 这与总和无关。我想处理所有搜索到的行。顺便说一句,非常感谢您的努力。
【解决方案3】:

这是一个老问题,但在 O365 中它不起作用,因为第一个地址没有退出条件,所以它会循环直到它崩溃。请注意,Is Nothing.Address 必须单独测试,否则如果范围为空,则会出现错误。

下面添加了退出条件:

Public Function GetDuplicateCount(value As String) As Integer

    Dim counter As Integer
    Dim c As Range
    Dim firstAddress As String

    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
                Debug.Print c.Address
                counter = counter + 1
                Set c = .FindNext(c)
                If c Is Nothing Then Exit Do
            Loop While Not c.Address = firstAddress

        End If
    End With

    GetDuplicateCount = counter
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2013-12-19
    相关资源
    最近更新 更多