Excel VBA中Find和Findnext的用法分别是什么?
1个回答
展开全部
Sub Myfind()
Dim iRange As Range, iFined As Range
Dim iStr, iAddress As String, N As Integer
'以上是定义使用到的变量
Set iRange = Range("A2:A100") '给irange变量赋值为A2:A100区域
iStr = Range("A1").Value '给要查找的字符串变量赋值为A1单元格的值
Set iFined = iRange.Find(iStr, lookat:=xlWhole) '在irange区域内查找等于变量istr的单元格,并赋值给你ifined变量,如果要查找包含istr变量的单元格,更改参数lookat:=xlPart
If iFined Is Nothing Then '判断 ifined变量是空
MsgBox "在" & iRange.Address(0, 0) & "区域里,没有找到内容等于" & iStr & "的单元格!"
Exit Sub
Else
iAddress = iFined.Address(0, 0)
Do
N = N + 1
Set iFined = iRange.FindNext(iFined) '继续向下查找等于istr变量的单元格
Loop While Not iFined Is Nothing And iAddress <> iFined.Address(0, 0) 'do循环的条件为ifined变量非空,并且ifined变量的单元格地址不等于找到的第一个单元格地址
End If
MsgBox "在" & iRange.Address(0, 0) & "区域里,共找到内容等于" & iStr & "的单元格有:" & N & "个!"
End Sub
Dim iRange As Range, iFined As Range
Dim iStr, iAddress As String, N As Integer
'以上是定义使用到的变量
Set iRange = Range("A2:A100") '给irange变量赋值为A2:A100区域
iStr = Range("A1").Value '给要查找的字符串变量赋值为A1单元格的值
Set iFined = iRange.Find(iStr, lookat:=xlWhole) '在irange区域内查找等于变量istr的单元格,并赋值给你ifined变量,如果要查找包含istr变量的单元格,更改参数lookat:=xlPart
If iFined Is Nothing Then '判断 ifined变量是空
MsgBox "在" & iRange.Address(0, 0) & "区域里,没有找到内容等于" & iStr & "的单元格!"
Exit Sub
Else
iAddress = iFined.Address(0, 0)
Do
N = N + 1
Set iFined = iRange.FindNext(iFined) '继续向下查找等于istr变量的单元格
Loop While Not iFined Is Nothing And iAddress <> iFined.Address(0, 0) 'do循环的条件为ifined变量非空,并且ifined变量的单元格地址不等于找到的第一个单元格地址
End If
MsgBox "在" & iRange.Address(0, 0) & "区域里,共找到内容等于" & iStr & "的单元格有:" & N & "个!"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询