Excel_VBA实例教程_查找单元格(最新整理)_第1页
Excel_VBA实例教程_查找单元格(最新整理)_第2页
Excel_VBA实例教程_查找单元格(最新整理)_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1、使用 find 方法excel vba 实例教程 查找单元格在 excel 中使用查找对话框可以查找工作表中特定内容的单元格,而在 vba 中则使用 find 方法, 如下面的代码所示。01. sub rngfind()02. dim strfind as string03. dim rng as range04. strfind = inputbox(请输入要查找的值:)05. if trim(strfind) then06. with sheet1.range(a:a)07. set rng = .find(what:=strfind, _08. after:=.cells(.cell

2、s.count), _09. lookin:=xlvalues, _10. lookat:=xlwhole, _11. searchorder:=xlbyrows, _12. searchdirection:=xlnext, _13. matchcase:=false)14. if not rng is nothing then15. application.goto rng, true16. else17. msgbox 没有找到该单元格!18. end if19. end with20. end if 21.end sub代码解析:rngfind 过程使用 find 方法在工作表 shee

3、t1 的 a 列中查找 inputbox 函数对话框中所输入的值, 并查找该值所在的第一个单元格。第 6 到第 13 行代码在工作表 sheet1 的 a 列中查找 inputbox 函数对话框中所输入的值。应用于range 对象的 find 方法在区域中查找特定信息,并返回 range 对象,该对象代表用于查找信息的第一个单元格。如果未发现匹配单元格,就返回 nothing,语法如下:01.expression.find(what,after,lookin,lookat,searchorder,searchdirection,matchcase, matchbyte, serchformat

4、)复制代码参数 expression 是必需的,该表达式返回一个 range 对象。参数 what 是必需的,要搜索的数据,可为字符串或任意数据类型。参数 after 是可选的,表示搜索过程将从其之后开始进行的单元格,必须是区域中的单个单元格。查找时是从该单元格之后开始的,直到本方法绕回到指定的单元格时,才对其进行搜索。如果未指定本参数,搜索将从区域的左上角单元格之后开始。在本例中将 after 参数设置为 a 列的最后一个单元格,所以查找时从 a1 单元格开始搜索。参数 lookin 是可选的,信息类型。参数 lookat 是可选的,可为 xllookat 常量的 xlwhole 或 xlp

5、art 之一。参数 searchorder 是可选的,可为 xlsearchorder 常量的 xlbyrows 或 xlbycolumns 之一。参数 searchdirection 是可选的,搜索的方向,可为 xlsearchdirection 常量的 xlnext 或 xlprevious之一。参数 matchcase 是可选的,若为 true,则进行区分大小写的查找。默认值为 false。参数 matchbyte 是可选的,仅在选择或安装了双字节语言支持时使用。若为 true,则双字节字符仅匹配双字节字符。若为 false,则双字节字符可匹配其等价的单字节字符。参数 serchform

6、at 是可选的,搜索的格式。每次使用 find 方法后,参数 lookin、lookat、searchorder 和 matchbyte 的设置将保存。如果下次调用 find 方法时不指定这些参数的值,就使用保存的值。因此每次使用该方法时请明确设置这些参数。如果工作表的 a 列中存在重复的数值,那么需要使用 findnext 方法或 findprevious 方法进行重复搜索,如下面的代码所示。01. sub rngfindnext()02. dim strfind as string03. dim rng as range04. dim findaddress as string05. st

7、rfind = inputbox(请输入要查找的值:)06. if trim(strfind) then07. with sheet1.range(a:a)08. set rng = .find(what:=strfind, _09. after:=.cells(.cells.count), _10. lookin:=xlvalues, _11. lookat:=xlwhole, _12. searchorder:=xlbyrows, _13. searchdirection:=xlnext, _14. matchcase:=false)15. if not rng is nothing th

8、en16. findaddress = rng.address17. do18. rng.interior.colorindex = 619. set rng = .findnext(rng)20. loop while not rng is nothing and rng.address findaddress21. end if22. end with23. end if 24.end sub代码解析:rngfindnext 过程在工作表 sheet1 的 a 列中查找 inputbox 函数对话框中所输入的值,并将查到单元格底色设置成黄色。第 8 行到第 17 行代码使用 find 方法

9、在工作表 sheet1 的 a 列中查找。第 16 行代码将查找到的第一个单元格地址赋给字符串变量 findaddress。第 18 行代码将查找到的单元格底色设置成黄色。第 19 行代码使用 findnext 方法进行重复搜索。findnext 方法继续执行用 find 方法启动的搜索。查找下一个匹配相同条件的单元格并返回代表单元格的 range 对象,语法如下:01.expression.findnext(after)复制代码参数 expression 是必需的,返回一个 range 对象。参数 after 是可选的,指定一个单元格,查找将从该单元格之后开始。第 20 行代码如果查找到的单

10、元格地址等于字符串变量 findaddress 所记录的地址,说明 a 列已搜索完毕,结束查找过程。运行 rngfindnext 过程,在 inputbox 函数输入框中输入“196.01”后结果如图 1 所示。还可以使用 findprevious 方法进行重复搜索,findprevious 方法的语法如下: expression.findprevious(after)findprevious 方法和 findnext 方法唯一的区别是 findprevious 方法查找匹配相同条件的前一个单元格而 findnext 方法是查找匹配相同条件的下一个单元格。2、使用 like 运算符使用 lik

11、e 运算符可以进行更为复杂的模式匹配查找,如下面的代码所示。01. sub rnglike()02. dim rng as range03. dim a as integer04.a = 105. with sheet206. .range(a:a).clearcontents07. for each rng in .range(b1:e1000)08. if rng.text like *a* then09. .range(a & a) = rng.text10.a = a + 111. end if12. next13. end with 14.end sub代码解析:rnglike 过程

12、使用 foreach.next 语句和 like 运算符在单元格区域 b1:e10000 中搜索含有“a” 字符的单元格,找到匹配单元格以后将单元格的值写入到 a 列中。第 6 行代码使用 clearcontents 方法清除 a 列区域的数据。第 7 行代码使用 for each.next 语句在单元格区域 b1:e10000 中循环。第 8 行代码使用 like 运算符在单元格区域 b1:e10000 中搜索含有“a”字符的单元格。like 运算符用来比较两个字符串,语法如下:01.result = string like pattern复制代码参数 string 是必需的,字符串表达式。

13、参数 pattern 是必需的,字符串表达式。如果 string 与 pattern 匹配,则 result 为 true;如果不匹配,则 result 为 false。但是如果 string 或pattern 中有一个为 null,则 result 为 null。参数 pattern 可以使用通配符、字符串列表或字符区间的任何组合来匹配字符串。表格 5 1 列出pattern 中允许的字符以及它们与什么进行匹配。pattern 中的字符 符合 string 中的字符? 任何单一字符* 零个或多个字符# 任何一个数字 (09)charlist charlist 中的任何单一字符!charlis

14、t 不在 charlist 中的任何单一字符表格 1 pattern 中的匹配字符串第 9 行代码将找到的匹配单元格的值写入到 a 列中。运行 rnglike 过程结果如图 2 所示。integerli_retoleobjectole_1,lole_sheet,lole_sheet1 ole_1=createoleobjectli_ret=1li_ret=ole_1.connecttoobject( excel.application ) ifli_ret0thenli_ret=ole_1.connecttonewobject( excel.application ) ifli_ret 0th

15、enmessagebox( 提示 , excel 连接失败: +string(li_ret)return endifendifole_1.workbooks.open( e:part.xls )lole_sheet=ole_1.application.activeworkbook.worksheets1 ole_1.application.visible=true lole_sheet1=lole_sheet.range( a1:a100 ).find( 5003 )ifisvalid(lole_sheet1)then lole_sheet1.select()elsemessagebox( 提

16、示 , 没有找到数据 ) endifxlsub1.range(ls_ran).copy() /把所选内容复制到内存中“”“”at the end, xiao bian gives you a passage. minand once said, people who learn to learn are very happy people. in every wonderful life, learning is an eternal theme. as a professional clerical and teaching position, i understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. only by constantly learning and mastering the latest relevant knowledge, can emplo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论