不知道上一篇的示例把大家绕晕没,绕晕了的就再晕一次,没晕的也再试试定力。 示例: 数据截图:
Dim rg As Range With Range("a1").CurrentRegion 'SearchOrder参数演示,是先行还是先列 Set rg = .Find(what:="张三",LookIn:=xlValues, _ lookat:=xlPart, MatchCase:=False, _ MatchByte:=False, searchorder:=xlByColumns) If Not rg Is Nothing Then MsgBox "找张三,部分匹配,查找值,按列:" & rg.Address End If Set rg = .Find(what:="张三",LookIn:=xlValues, _ lookat:=xlPart, MatchCase:=False, _ MatchByte:=False, searchorder:=xlByRows) If Not rg Is Nothing Then MsgBox "找张三,部分匹配,查找值,按行:" & rg.Address End If
'SearchDirection参数,搜索方向 Set rg = .Find(what:="张三峰",LookIn:=xlFormulas, _ lookat:=xlPart, _ MatchCase:=False, MatchByte:=False, _ searchorder:=xlByRows, SearchDirection:=xlNext) If Not rg Is Nothing Then MsgBox "找张三,按行,顺序查找:" & rg.Address End If Set rg = .Find(what:="张三峰",LookIn:=xlFormulas, _ lookat:=xlPart, MatchCase:=False, MatchByte:=False, _ searchorder:=xlByColumns, SearchDirection:=xlPrevious) If Not rg Is Nothing Then MsgBox "找张三,按列,逆序查找:" & rg.Address End If '注意结果为什么不是C7 Set rg = .Find(what:="张三峰",LookIn:=xlFormulas, _ lookat:=xlPart, searchorder:=xlByRows, _ SearchDirection:=xlPrevious) If Not rg Is Nothing Then MsgBox "找张三,逆序,按行:" & rg.Address End If '与上面按行查找对比
'matchcase参数,大小写问题 Set rg = .Find(what:="ab", LookIn:=xlFormulas, _ lookat:=xlPart, MatchCase:=False, MatchByte:=True, _ searchorder:=xlByRows, SearchDirection:=xlNext) If Not rg Is Nothing Then MsgBox "找ab,不区分大小写,区分全半角:" & rg.Address End If Set rg = .Find(what:="ab", LookIn:=xlFormulas, _ lookat:=xlPart, MatchCase:=True, MatchByte:=True, _ searchorder:=xlByColumns, SearchDirection:=xlNext) If Not rg Is Nothing Then MsgBox "找ab,区分大小写:" & rg.Address End If
'MatchByte参数,区分全角半角 Set rg = .Find(what:="AB", LookIn:=xlFormulas, lookat:=xlPart,_ MatchCase:=False, MatchByte:=False, _ searchorder:=xlColumns, SearchDirection:=xlNext) If Not rg Is Nothing Then MsgBox "找AB,不区分大小写与全半角:" & rg.Address End If Set rg = .Find(what:="ab", LookIn:=xlFormulas, lookat:=xlPart,_ MatchCase:=False, MatchByte:=True, _ searchorder:=xlByColumns, SearchDirection:=xlNext) If Not rg Is Nothing Then MsgBox "找ab,区分全角半角:" & rg.Address End If End With
|