【发布时间】: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")
【问题讨论】:
-
这很奇怪
.FindNext在Function中不起作用,但在Sub中起作用。您需要再次使用.Find。 Here is similar Q&A -
按照建议,我将 .FindNext 更改为 Set c = .Find(What:=value, _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ after:=c, _ MatchCase:=False) 但它进入了无限循环。