用excel处理数据的时候,无论是使用VBA还是函数,查和引用都是两大主要的工作,VBA中的find系列的方法(find、findnext、Range.FindPrevious)返回range对象,可以同时实现查和引用,因此非常有用,下面列举一些常见的find的用法:
Sub Find1() '在某列查
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
vba排序函数sort用法================================================== 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() '查方向(从后向前)
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"
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()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论