您的位置:首页 > 编程语言 > VB

全民一起VBA实战篇 专题1 第二回 可选参数灵活搭配 格式内容一应俱全

2019-06-16 13:07 1111 查看

 

相关知识点:

Application.FindFormat属性:用于指定后面Find等方法中需要查找的格式,与Range的方法一致

                     Application.FindFormat.Font.Bold=True

Range.Find(What,After,LookIn,Lookat,SearchOrder,SeachDirection,MatchCase,MatchByte,SeachFormat)

           9个参数:What 必填,后面8个参数,定义为可选参数(提高18回)。

What:含义,需要查找的内容;特点,为变体类型,可以接受数字、字符串、日期等各种VBA数据类型,支持通配符(正则表达式,?*-),从而实现模糊查找。(25),(”熊猫”),(#3/18/2019#

MatchCase:含义,匹配大小写;取值,True代表大小写不同;False,代表大小写相同

LookAt:   含义,匹配单元格;取值,单元格的内容必须与欲查找内容长度相同,不能多出字符;False,单元格中的内容只需包含欲查找的字符串即可。1或xlwhole,完全匹配;2或xlpart,包含即可

LookIn:     含义,查找范围;取值,xlValues在单元格内容(值)中查找;xlFormulas在公式(或值)中查找;xlcomments在单元格批注中查找。Xlvalues和xlformulas查找相互重叠。

Searchformat:   含义,按格式查找;取值,True,查找结果须符合指定格式;False,查找结果可以是任意格式。

例1 大小写查找

Sub matchcasedemo()

       Dim r as range

       Set r=cells.find(“abc”,MatchCase:=True)

       If Not r Is Nothing Then

              Msgbox r.address  ‘返回单元格地址“$D$3”,匹配“abc”,不匹配“aBC”

       End If

End Sub

例2 匹配单元格

Sub lookatdemo()

       Dim r as range

       Set r=cells.find(“b”, MatchCase:=True ,lookat:=1)  ‘区分大小写,

‘1或xlwhole,精确匹配;2或xlpart,包含即可

       If Not r Is Nothing Then

              r.Interior.Color=vbred  ‘长度完全相同

       End If

End Sub

例3 查找范围

Sub lookIndemo()

       Dim r as range

       Set r=cells.find(“熊猫”, lookin:=xlvalues)  ‘在单元格内容中查找

       If Not r Is Nothing Then

              r.Interior.Color=vbred  ‘长度完全相同

       End If

End Sub

例4 特定格式查找

Sub formatdemo()

       Dim r as range

Application.FindFormat.Interior.Color=vbblack

Application.FindFormat.Font.Color=vbwhite   ‘特定格式

       Set r=cells.find(“熊猫”,searchformat:=True)

       If Not r Is Nothing Then

              Msgbox r.address  ‘返回单元格地址“$D$3”,匹配“abc”,不匹配“aBC”

       End If

End Sub

例5 特定格式查找(简化代码With    End with

Sub formatdemo()

       Dim r as range

With Application.FindFormat

.Interior.Color=vbblack

.Font.Color=vbwhite   ‘特定格式

       End  With

       Set r=cells.find(“熊猫”,searchformat:=True)

       If Not r Is Nothing Then

              Msgbox r.address  ‘返回单元格地址“$D$3”,匹配“abc”,不匹配“aBC”

       End If

End Sub

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐