VBA中find的一些使用方法
2020-11-16 00:54
标签: 用excel处理数据的时候,无论是使用VBA还是函数,查找和引用都是两大主要的工作,VBA中的find系列的方法(find、findnext、Range.FindPrevious)返回range对象,可以同时实现查找和引用,因此非常有用,下面列举一些常见的find的用法: Sub Find1() ‘在某列查找 VBA中find的一些使用方法 标签: 原文地址:http://www.cnblogs.com/xmdata-analysis/p/4670823.html
Dim k
k = Range("A:A").Find("A").Row
MsgBox k
End Sub
==================================================
Sub Find11() ‘在多列查找
Dim k
k = Range("A:B").Find("BCD").Row
MsgBox k
End Sub
==================================================
Sub Find2() ‘查找的起始位置
Dim k
k = Range("A:B").Find("A", AFTER:=Range("A5")).Row
MsgBox k
End Sub
==================================================
Sub Find3() ‘在值中查找
Dim k
k = Range("B:B").Find("SE", LookIn:=xlValues).Row
MsgBox k
End Sub
==================================================
Sub Find31() ‘在公式中查找
Dim k
k = Range("B:B").Find("C2", LookIn:=xlFormulas).Address
MsgBox k
End Sub
==================================================
Sub Find32() ‘在备注中查找
Dim k
k = Range("B:C").Find("AB", LookIn:=xlComments).Address
MsgBox k
End Sub
==================================================
Sub Find41() ‘按模糊查找
Dim k
k = Range("B:C").Find("A", LookIn:=xlValues, LOOKAT:=xlPart).Address
MsgBox k
End Sub
==================================================
Sub Find42() ‘匹配查找
Dim k
k = Range("B:C").Find("A", LookIn:=xlValues, LOOKAT:=xlWhole).Address
MsgBox k
End Sub
==================================================
Sub Find5() ‘按先行后列的方式查找
Dim k
k = Range("A:B").Find("AB", LookIn:=xlValues, LOOKAT:=xlWhole, SEARCHORDER:=xlByRows).Address
MsgBox k
End Sub
==================================================
Sub Find51() ‘按先列后行的方式查找
Dim k
k = Range("A:B").Find("AB", LookIn:=xlValues, LOOKAT:=xlWhole, SEARCHORDER:=xlByColumns).Address
MsgBox k
End Sub
==================================================
Sub Find6() ‘查找方向(从后向前)
Dim k
k = Range("A:A").Find("A", , xlValues, xlWhole, xlByColumns, xlPrevious).Address
MsgBox k
End Sub
==================================================
Sub Find61() ‘查找方向(从前向后)
Dim k
k = Range("A:A").Find("A", , xlValues, xlWhole, xlByColumns, xlNext).Address
MsgBox k
End Sub
==================================================
Sub Find7() ‘字母大小写
Dim k
k = Range("a:b").Find("a", , xlValues, xlWhole, xlByColumns, xlNext, False).Address
MsgBox k
End Sub
==================================================
Sub f7() ‘查找不到的情况
Dim MRG As Range
Set MRG = Range("A:A").Find("D")
If MRG Is Nothing Then
MsgBox "查找不到字母D"
Else
MsgBox "查找成功,单元格地址为:" & MRG.Address
End If
End Sub
==================================================
Sub f8() ‘二次查找
Dim MRG As Range
Set MRG = Range("A:A").Find("A")
Set mrg1 = Range("A:A").FindNext(MRG)
MsgBox mrg1.Address
End Sub
==================================================
Sub F9() ‘区域查找
Dim MRG As Range, AAA As String
Set MRG = Range("A1:F16").Find("A")
AAA = MRG.Address
Do
Set MRG = Range("A1:F16").FindNext(MRG)
MsgBox MRG.Address
Loop Until MRG.Address = AAAEnd Sub
==================================================
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