vba FindNext不工作
这是一个非常基本的问题,不要尖叫,因为我不是一个vba专家。
所以我们在这里,我创build了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 IND
2美国
3 CAN
4 IND
5 CAN
6美国
每次我用任何价值search它返回一个不知道wny。 任何错误的function?
例如GetDuplicateCount(“IND”)
知道了…终于
有两件事FindNext不工作,如@kazjawbuild议我试过.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
为什么不使用本机Countiffunction?
=COUNTIF(A:A,"IND")